Meandering Soul

This day is done, I'm going home.
eFranes Penguin Avatar

Symfony Console Additions v0.6.0

21
Sep 2019

I released [efrane/console-additions](https://packagist.org/packages/efrane/console-additions) v0.6.0 today. More than a year after the last bigger release this probably could have been a v1.0. However, I still feel like there’s some fundamentals I do with Symfony’s Console component in my everyday work which I would like to extract into a reusable form in this library.

So, what’s changed?

For one, this development episode was focused on reorganising the code. There’s still very few files in the repository but that doesn’t mean that there wasn’t room for optimisation. The result of this is, that the tests folder now resemble the folder structure of the src folder, thus making it easier to find the corresponding test file.

The bigger change is a partial rewrite of command batches. Before, the Batch class internally managed every possible action as a combination of strings and weird arrays. This type-conglomerate lead to a quite a few ugly places in the code which felt like they might break at a moment’s notice. Since this version, Batch internally relies on Action classes which know how to handle themselves. This also means, that Batch just became that much more powerful since all Batch wants to know about them is how to represent them as a string and a way to start their execution:

1
2
3
4
5
interface Action
{
    public function execute(OutputInterface $output): int;
    public function __toString(): string;
}

Usable actions are now:

  • StringCommandAction - This handles executing console commands given a full command signature
  • InstanceCommandAction - No need to do string parsing of a signature? Pass a command instance to this one!
  • MessageAction - This is your basic echo action. Especially when PHP-izing former shell scripts, this can come in quite handy
  • ProcessAction - Pass a configured Process from the Symfony Process component to this one
  • ShellAction - Just want to execute a shell command and don’t feel like configuring the process yourself? This one is there for your convenience!

All of the built-in actions have convenience methods on Batch, e.g. $batch->addProcess() or $batch->addMessage().

Along with these internal updates came the jump to PHP 7.0 or newer as a language dependency and Symfony Components 3.4 or newer are required to work with this library now. The latter also means that string parsing for commands in $batch->addShell() has been deprecated and will be removed in the next release. I highly encourage any user of this library to switch to the array-Syntax.

  • Published on September 21, 2019
  • 403 words