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

Config Split treats all the cli the same.Drupal 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 Drupal 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 Drupal 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 Drupal 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 Drupal maintenance support plans console. It is all about separating the command discovery from the command logic. Drush 8, 9 and Drupal 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 Drupal maintenance support plansconfig_splitConfigSplitCliService::ioExport and Drupal 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 Drupal 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 Drupal maintenance support plans translates strings. Because Drupal 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.  Drupal 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 Drupal 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 module and does not depend on Drush or the Drupal 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: Drupal maintenance support plans 8Drupal maintenance support plans PlanetDrush
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

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

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.