Kaloyan K. Tsvetkov


New Fukō Packages

Quick overview of the two new Fukō libraries, fuko-php/open and fuko-php/source

July 20, 2021

I did some more development work in June of 2021 on another one of my projects. I've published two new libraries, which have been in development on-and-off for a few years now.

Quick Look Back

Several years ago I started the fuko-php project to host few small libraries that are meant to assist other development packages. At the time I was disappointed how often very simple routines are never put into their only libraries, and you often had to either implement it yourself, or use a library where the functionality you need is less than 5% of its size.

I like to have the names of my projects end in "O", but it will be a challenge to come up with so many such names for the packages. The solution was to just use the "O" name for the project, and that's how Fukō started. And yes, my intention was indeed for this to be pronounced "Fuck-O".

The first package for this project was fuko-php/masked, which is used to mask sensitive data in text dumps. This is a common issues with a lot of solutions and components where data is dumped as we do not to want to expose any sensitive data such as credentials, passwords, tokens or anything else which we consider private.

fuko-php/open

The fuko-php/open was the second package I started working on soon after publishing the first one. It was supposed to be a quickie, but as it usually happens I got distracted (for several years) and that got buried with some of the other projects.

Rewind to last month, June 2021. I actually had both the time and the motivation to do some more open-source work again after restarting my website. The first resumed project was indeed this one, and after spending a weekend working on it, it was out.

Another open-source weekend, and I am able to finally push something that I've started in late 2018. Two and a half years, it's not bad =)

github.com/fuko-php/open

Kaloyan K. Tsvetkov June 8, 2021

This is a small library that helps you generate "open" links for editors and IDEs. You've probably seen those in a lot of different projects which reference source code and want to allow you a quick and easy way to open that code directly in your editor. The thing is that this feature was always bundled inside some other bigger library and what I needed was to have it separate and re-usable.

Here's an example of generating an Atom link:

use \Fuko\Open\Editor;
use \Fuko\Open\Link;

/* I have Atom installed locally, so this is what I want to use */
$editor = new Link(Editor::ATOM);

echo $editor->link('/var/www/html/index.html', 2);
// atom://core/open/file?filename=%2Fvar%2Fwww%2Fhtml%2Findex.html&line=2

You can also replace (translate) certain leading portions of the filenames for the source code, which is handy if you are working with a shared network mount or with Docker.

use \Fuko\Open\Link;

$link = new Link('source-code://%s#%05d');
$link->addPrefix(getcwd() . '/', '/upside/down/');
$href = $link->link(__FILE__, __LINE__);
// source-code://%2Fupside%2Fdown%2Fdemo.php#00023

You can add multiple prefixes, as usually there is more than one symlinked folder in most projects.

use \Fuko\Open\{Link, Editor};

$href = (new Link(Editor::ATOM))
    ->addPrefix(getcwd() . '/', '/upside/down/')
    ->addPrefix('/private/tmp', 'tmp')
    ->link(__FILE__, __LINE__);

The new feature which I added was the ability to "sniff" your editor. Instead of you having to use different configuration depending on what editor you are using, fuko-php/open can guess your editor with a series of very simple checks for what is installed, or what symlinks are declared.

use \Fuko\Open\{Link, Sniff};

/* I have Atom installed locally, so this is how you can detect it */
$format = (new Sniff)->detect();
if ($format)
{
    echo (new Link($format))->link('/var/www/html/index.html', 2);
    // atom://core/open/file?filename=%2Fvar%2Fwww%2Fhtml%2Findex.html&line=2
}

That's the whole library. Small and simple, as simple things usually work very well.

fuko-php/source

Another common thing when working with source-code references, is to extract a chunk of several lines of code. You've seen this when errors and exceptions are rendered, or when browsing through the frames of a backtrace. It is a simple task but so far I've only seen this implemented as part of various libraries, and there was nothing standalone. This is where fuko-php/source comes in.

Another day, another small open-source lib (just a single class actually)

github.com/fuko-php/source

Kaloyan K. Tsvetkov June 8, 2021

This is a simple task, with only few things to consider when implementing it. The most obvious one is to be aware of big source files with a lot of lines of code. These files are bad to be read whole into memory as they will take up a lot of space, and we are only going to be using up to 10 lines of code from them. Unfortunately, that's exactly what I've seen in few of the places where such code is implemented:

I decided to take a different approach and instead only read the lines of code that I need. This as you expect drastically reduces the footprint of the read data at the expense of few more file reads.

It is just one class and it is really very straight-forward to use. Here's an example of reading the chunk of code at /var/www/html/index.php:17

$source = new \Fuko\Source\Code('/var/www/html/index.php');
print_r($source->getLinesAt(17));

fuko-php/masked

In addition to the new libraries, I was also able to push some updates to fuko-php/masked

Weekends are the best (and perhaps the only) time to attend to your open-source projects. At least that the case with me, and today after almost 2.5 years I've pushed two new releases for Fuko\Masked

github.com/fuko-php/masked

Kaloyan K. Tsvetkov June 8, 2021

Now it will be probably 2.5 more years until I do something else with this project. Anything sooner than that is going to be a win!