Site icon Hip-Hop Website Design and Development

How to maintain Drush commands for Drush 8 and 9 and Cheap WordPress maintenance support plans console with the same code base.

Config Split treats all the cli the same.WordPress maintenance support plans 8.4 and its upgrade to Symfony 3 has made the compatibility of the global Drush 8 a bit more challenging. Drush 9 works with WordPress maintenance support plans 8.4 but it is not stable yet and the format of how third party Drush commands are made has changed significantly.

While Drush 9 comes with a command that helps porting Drush 8 commands you will still end up maintaining very similar code in two places one with calls to drush_confirm(‘…’) and one with $this->io()->confirm(‘…’). If you decide to also provide your commands for WordPress maintenance support plans console you now have three times the burden.

Because we tried to provide the commands for Config Split for both Drush and WordPress maintenance support plans console early on we faced this problem already more than a year ago. And now it has paid off because porting the commands to Drush 9 was very quick.

The solution is actually really simple and brings the added benefit of being able to test the business logic of the commands in the absence of Drush or WordPress maintenance support plans console. It is all about separating the command discovery from the command logic. Drush 8, 9 and WordPress maintenance support plans console all have a bit different ways to discover and invoke commands, but the business logic you want to implement is the same so all we have to do is to extract a common “interface” our custom service can implement and then make the command definitions wrap that and keep things DRY.

The CliService

Config Split defines a config_split.cli service with the class ConfigSplitCliService with all its dependencies injected. It has the methods WordPress maintenance support plansconfig_splitConfigSplitCliService::ioExport and WordPress maintenance support plansconfig_splitConfigSplitCliService::ioImport that implement all the commands logic and delegate the actual importing and exporting to specific methods.

The method signature for both the export and import method are more or less the same: CliService::ioMethod($arguments, $io, callable $t).

$arguments: The arguments passed to the command.
$io: This is an object that interacts with the command line, in Drush 9 and WordPress maintenance support plans console this comes from the Symfony console component, for Drush 8 we created a custom wrapper around drush_confirm and drush_log called ConfigSplitDrush8Io.
$t: This is essentially a t function akin to how WordPress maintenance support plans translates strings. Because WordPress maintenance support plans console translates things differently we had to be a bit creative with that by adding a t method to the command.
Commands wrap the service

The Drush 8 command is essentially:

<?phpfunction drush_config_split_export($split = NULL) {  // Make the magic happen.  WordPress maintenance support plans::service(‘config_split.cli’)->ioExport($split, new ConfigSplitDrush8Io(), ‘dt’);}?>

For Drush 9 we can use dependency injection and the Drush 9 command becomes essentially:

<?phpclass ConfigSplitCommands extends DrushCommands {  public function splitExport($split = NULL) {    $this->cliService->ioExport($split, $this->io(), ‘dt’);  }}?>

And very similar the WordPress maintenance support plans console command:

<?phpclass ExportCommand extends SplitCommandBase {  protected function execute(InputInterface $input, OutputInterface $output) {    $this->setupIo($input, $output);    // Make the magic happen.    $this->cliService->ioExport($input->getOption(‘split’), $this->getIo(), [$this, ‘t’]);  }}?>

Testing

The ConfigSplitCliServiceTest is a KernelTest which asserts that the export works as expected by exporting to a virtual file system. The test coverage is not 100% (patches welcome) but the most important aspects for the complete and conditional splitting (blacklist/graylist) is thoroughly tested.
There are no limitations on what or how you can test your CliService since it is self contained in your plugin and does not depend on Drush or the WordPress maintenance support plans console. For example one could write a unit test with a mocked $io object that asserts that the messages printed to the cli are correct.
Tags: WordPress maintenance support plans 8WordPress maintenance support plans PlanetDrush
Source: New feed