Category Archives: Programming

Laravel Telescope

Today I tried out Laravel Telescope. From the official Laravel Telescope page, it is designed to:

Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps, and more.

https://laravel.com/docs/9.x/telescope

Installation

I was adding Telescope to in-progress app that I was developing in VSCode on Github.

These are the commands I followed to add to this existing project:

Firstly, I brought my local development Docker environment up, using Laravel Sail:

./vendor/bin/sail up -d

I followed the Local Only Installation instructions, so I added the Telescope dependency, for the dev environment, using Composer within Sail:

./vendor/bin/sail composer require laravel/telescope --dev

After installing Telescope, I published its assets using the telescope:install Artisan command. After installing Telescope, I also run the migrate command in order to create the tables needed to store Telescope’s data:

./vendor/bin/sail artisan telescope:install
./vendor/bin/sail artisan migrate

After running telescope:install, you need to remove the TelescopeServiceProvider service provider registration from your application’s config/app.php configuration file. Instead, manually register Telescope’s service providers in the register method of your App\Providers\AppServiceProvider class. We will ensure the current environment is local before registering the providers:

public function register()
    {
        //
        if ($this->app->environment('local')) {
            $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
            $this->app->register(TelescopeServiceProvider::class);
        }
    }

Finally, you need to also prevent the Telescope package from being auto-discovered by adding the following to your composer.json file:

"extra": {
    "laravel": {
        "dont-discover": [
            "laravel/telescope"
        ]
    }
},

Configuration

After publishing Telescope’s assets, its primary configuration file will be located at config/telescope.php. This configuration file allows you to configure your watcher options. Each configuration option includes a description of its purpose, so be sure to thoroughly explore this file – all very well documented.

Accessing the Telescope UI

The Telescope dashboard may be accessed at the /telescope route. By default, you will only be able to access this dashboard in the local environment.

http://localhost/telescope

I’ve not had chance to fully explore Telescope yet, but at least the installation was very straight forward. I’m hoping it will be a very useful framework-level debug tool.

Laravel Telescope UI

WordPress Plugins: PlatformInfo & Cachify

The sense of irony in writing about a WordPress plugin, on a WordPress blog, hosted on a system where I can’t install plugins (welcome to wordpress.com!) is not lost on me, however I shall persevere…

Over the winter break I’ve been writing and contributing to WordPress plugins, as a means of:

  • contributing to an Open Source Community,
  • learning something to prevent brain-rot, and
  • it’s PHP, so you’ve got to love it

Firstly, I’ve written a WordPress plugin called PlatformInfo. It’s fairly simple, so that I can learn the plugin ecosystem, refine my tooling, etc – but it’s designed to do one thing fairly well. PlatformInfo displays, ad-free and no-nag, important information to help you diagnose issues with your WordPress setup or plugin development.

It’s useful for issues related to common php.ini settings, environment variables, user constants, and OPcache.

I’ve even managed to get it published in the official WordPress Plugin Directory, so other WordPressers can install it straight from WordPress: https://wordpress.org/plugins/platforminfo/

Secondly, I’ve contributed Redis support to the well-known and popular caching WordPress plugin called Cachify. This plugin is one of many looked after by the pluginkollektiv.

Both have taken me about a week, in between Christmas, New Year, family Zooms, walking, trying to keep my mental health in a reasonable place, etc – but I’ve learned a lot (WP Coding Standards, SVN, PHP’s output buffering, Sniffs for phpcs, GitHub Actions, etc) and am already working on plugin contribution #3… watch this space.

In the mean time, for those of you interested, here are some links to code and documentation that you may find useful if you’re developing WordPress plugins:

PHP Associative Array – Does It Have All The Keys?

It’s not an uncommon programming task to want to check that an associative array (an array with keys) contains all the keys you expect. This is useful when passing an associative array to another function, which needs to check that everything is present before continuing (or at least fail/return fast if they don’t).

PHP doesn’t have a built-in function to compare keys in two array natively. array_key_exists() only looks for a single key – which isn’t quite what I needed here.

However you can build this function from a clever use of two other native functions:

<?php

function array_keys_exists(array $keys_to_be_present, array $arr) {
   return !array_diff_key(array_flip($keys_to_be_present), $arr);
}

This simple function takes a standard array of terms ($keys_to_be_present), which will need to be present as keys in the associative array ($arr) for it to return true.

This is how you’d use it:

<?php

$keys_needed = ['apples', 'pears'];

$array_under_test_1 = ['apples' => 24, 'pears' => 65];
$array_under_test_2 = ['apples' => 42, 'bananas' => 34];

// Will return bool(true)
var_dump(array_keys_exists($keys_needed, $array_under_test_1));

// Will return bool(false)
var_dump(array_keys_exists($keys_needed, $array_under_test_2));

// The function
function array_keys_exists(array $keys_to_be_present, array $arr) {
   return !array_diff_key(array_flip($keys_to_be_present), $arr);
}

So, how does this work? It utilises two functions:

The native function array_flip() converts the $keys_to_be_present array into an associative array:

<?php

$keys_needed = ['apples', 'pears'];

var_dump(array_flip($keys_needed));

// Prints
// array(2) {
//   ["apples"]=>
//   int(0)
//   ["pears"]=>
//   int(1)
// }

Then the array_diff_key() function compares the keys from two arrays and returns the difference. As the array_diff_key() function returns an array containing all the entries from array whose keys are absent from all of the other arrays – we need to negate (!) this value, so that a ‘no differernce’ (empty set) returns from our function as ‘true’.

Composition API in Vue

After listening to the good Views on Vue podcast, and specifically the episode on the Composition API (https://devchat.tv/views-on-vue/vov-126-vue-composition-api-and-nuxt-with-daniel-roe/) I was inspired (/mentally encouraged) to switch one of my small apps to use the Composition API.

For those that haven’t read up on it, the (brief) details are here (note this is Vue v3 documentation): https://v3.vuejs.org/guide/composition-api-introduction.html

I was implementing it in a Vue v2 app (as I use Vuetify and a few other bits that aren’t Vue v3 ready), so used the Composition API Plugin for Vue v2: https://github.com/vuejs/composition-api

It’s fairly simple to get your head around as a concept (a lot now lives in setup()). The two major points I learned were:

  • You have to watch out for those variables that you want to make ‘reactive’, using ref()
  • You have to watch out for those computed methods, using computed()

I found a really useful ‘cheatsheet’ here: https://www.vuemastery.com/pdf/Vue-3-Cheat-Sheet.pdf – it’s great as a quick reference.

Compiling PHP 8.0 on Ubuntu 20.04 Linux

PHP 8.0 was released yesterday (https://www.php.net/releases/8.0/en.php), and as PPAs aren’t ready yet, I thought I would try to compile it myself on Linux (Ubuntu 20.04 LTS).

First I downloaded and extracted the source from here: https://www.php.net/distributions/php-8.0.0.tar.gz

First, I tried to configure:

cd php-8.0.0 && ./configure

I soon found out that I was missing a couple of libraries from my system, which would be needed to compile the standard PHP extensions. I installed these by:

sudo apt install libxml2-dev libsqlite3-dev

Then I needed to configure again:

./configure

Once that has been sucessful, you can then compile and build:

make -j 6

Note the -j command switch incicates how many concurrent tasks that make can carry out – as I have a 8 core processessor, I went for 6.

Once it has compiled sucessfully, then you can run the tests. There are more than 10,000 tests in PHP 8.0.0, so this may take some time:

make test

If you’re happy that it compiled sucessfully, then you can install it (I didn’t do this step, as I only wanted to try it):

sudo make install

To see which modules got compiled:

./sapi/cli/php -m

To see the PHPINFO output:

./sapi/cli/php -i

If the modules you need / were expecting don’t appear, then you need to add then via –with statements when you ./configure.

ATmega32A Refused to ICSP

I purchased a couple of ATmega32A recently from eBay, in order to further develop the IO on the Z80 board. I made up a simple ICSP (In-Circuit Serial Programmer) board (see below) in order to program it using avrdude/Arduino.

The problem was, however many times I tried to use avrdude to program it, it always came back with an error.

After much re-soldering, checking connections, swapping ICs, checking the usbasp programmer etc – I got my trusty EEPROM TL866II Plus programmer out, and did a full erase on the ATmega32A.

Then it worked!!!

It turns out that there is a fuse (SPIEN) on the ATmega32A to disable serial program downloading! Once erased, this bit was cleared, and my ICSP circuit worked as expected.

I was able up burn the Arduino bootloder, Program via Programmer in Arduino, and subsequently see Serial output as my program ran.

Serial comms with Arduino using minicom for Linux

Whilst the Arduino Serial Monitor is good – I needed something that:

  • Would work in a Linux terminal (and hence I could use in VCScode)
  • Something that was bi-directional (as I wanted to take input from the keyboard)
  • Was easily available (i.e. I could apt-get install)

I started off using ‘screen’, but I felt this didn’t work well as it was difficult to navigate, and it would leave the serial connection hanging.

I can across ‘minicom‘ as a friendly serial communication program.

First, setup minicom, as you’ll want to turn Hardware flow control off:

sudo minicom -s

When I plug the Arduino in, it appears as /dev/ttyACM0, and I use 115200 baud:

minicom -c on -D /dev/ttyACM0 -b 115200

Ctrl-A x to exit – always handy to know, and the rest of the commands are:

Git – Remember Credentials

With Git 1.7.9 (released late 2012!) there is a great mechanism for Git to avoid having to ask you for your credentials all the time for HTTPS. It’s called a ‘credential helper’.

git config --global credential.helper cache

This tells Git to keep your password in memory for (by default) 15 minutes.

You can set a longer timeout with:

git config --global credential.helper "cache --timeout=3600"

Following the Linux kernel buffer with dmesg

dmesg (diagnostic message) is a command on most Unix-like operating systems that prints the message buffer of the kernel. The output includes messages produced by the device drivers.

It’s often useful to see these messages when diagnosing hardware issues (for example USB udev rules).

In a similar way you can use ‘tail’ to follow log files, there are a few command-line switches you can add to ‘dmesg’ to make things easier.

I’d recommend:

dmesg -wH

-w (or –follow) waits for new messages.
-H (or –human) enables human-readable output (e.g. –color, –reltime and
–nopager).