Skip to content

Commit

Permalink
Chain register (#2961)
Browse files Browse the repository at this point in the history
* Temporarily changing package name

* Added template option

* Implemented template option

* Added file

* Added ChainRegister

* Check if file exists

* Added example of usage of ChainRegister

* Use chain.yml with ChainRegister

* No need to check

* Do not check if file exists

* Removing these changes

* Prep pull request

* Chain for site:new

* Removed chain.yml

* Removed get config

* Removed get config

* Temporarily changing the namespace

* reset namespace
  • Loading branch information
marcelovani authored and jmolivas committed Dec 1, 2016
1 parent da62fd5 commit d83d11e
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Console\Utils\AnnotationValidator;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Console\Command\Chain\ChainRegister;

/**
* Class Application
Expand Down Expand Up @@ -107,6 +108,12 @@ private function registerCommands()
$logger->writeln($this->trans('application.site.errors.settings'));
}

foreach ($chainCommands as $name => $chainCommand) {
$file = $chainCommand['file'];
$command = new ChainRegister($name, $file);
$this->add($command);
}

$serviceDefinitions = [];
$annotationValidator = null;
if ($this->container->hasParameter('console.service_definitions')) {
Expand Down
55 changes: 40 additions & 15 deletions src/Command/Chain/ChainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class ChainCommand extends Command
use ChainFilesTrait;
use InputTrait;

/**
* @var string
*/
protected $file = null;

/**
* @var ChainQueue
*/
Expand Down Expand Up @@ -78,21 +83,31 @@ public function __construct(
*/
protected function configure()
{
if (is_null($this->getName())) {
$this
->setName('chain')
->setDescription($this->trans('commands.chain.description'));
}
else {
// ChainRegister passes name and file in the constructor.
$this
->setName(sprintf('chain:%s', $this->getName()))
->setDescription(sprintf('Custom chain: %s', $this->getName()));
}

$this
->setName('chain')
->setDescription($this->trans('commands.chain.description'))
->addOption(
'file',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.chain.options.file')
)
->addOption(
'placeholder',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
$this->trans('commands.chain.options.placeholder')
);
->addOption(
'file',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.chain.options.file')
)
->addOption(
'placeholder',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
$this->trans('commands.chain.options.placeholder')
);
}

/**
Expand All @@ -101,7 +116,8 @@ protected function configure()
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$file = $input->getOption('file');
// Check if the constructor passed a value for file.
$file = !is_null($this->file) ? $this->file : $input->getOption('file');

if (!$file) {
$files = $this->getChainFiles(true);
Expand Down Expand Up @@ -314,6 +330,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

/**
* Setter for $file.
*
* @param $file
*/
public function setFile($file) {
$this->file = $file;
}

/**
* Helper to load and clean up the chain file.
*
Expand Down
93 changes: 93 additions & 0 deletions src/Command/Chain/ChainRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* @file
* Contains Drupal\Console\Command\ChainRegister.
*
* ChainRegister is a wrapper for Chain commands.
* It will register the classes so you don't have to specify --file when calling
* chain commands. i.e. drupal chain --file=/some-folder/chain-magic.yml will be
* called: drupal chain:magic.
*
* To register custom chains, edit the ~/.console/chain.yml and add:
* chain:
* name:
* 'site:new:example':
* file: '/path-to-folder/chain-site-new.yml'
*/

namespace Drupal\Console\Command\Chain;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Console\Application;
use Drupal\Console\Utils\ConfigurationManager;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ChainFilesTrait;
use Drupal\Console\Style\DrupalStyle;
/**
* Class ChainRegister
*
* @package Drupal\Console\Command\ChainRegister
*/
class ChainRegister extends ChainCommand {
use ChainFilesTrait;

/**
* ChainRegister constructor.
*
* @param $name Chain name
* @param $file File name
*/
public function __construct($name, $file) {
$this->setName($name);
$this->setFile($file);

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure() {
parent::configure();
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output) {
parent::interact($input, $output);

$io = new DrupalStyle($input, $output);

// Populate placeholders.
$placeholders = '';
foreach ($input->getOption('placeholder') as $placeholder) {
$placeholders .= sprintf('--placeholder="%s" ',
$placeholder
);
}

$command = sprintf('drupal chain --file %s %s',
$this->file,
$placeholders
);

// Run.
$shellProcess = $this->get('shell_process');

if (!$shellProcess->exec($command, TRUE)) {
$io->error(
sprintf(
$this->trans('commands.exec.messages.invalid-bin')
)
);

return 1;
}
}
}

0 comments on commit d83d11e

Please sign in to comment.