Skip to content

Commit 30d37c6

Browse files
authored
Relocate autowire command registration (hechoendrupal#33)
* [console] Relocate autowire command registration. * [console] Relocate autowire command registration.
1 parent 53c9783 commit 30d37c6

File tree

13 files changed

+539
-0
lines changed

13 files changed

+539
-0
lines changed

config.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
application:
2+
autowire:
3+
commands:
4+
forced:
5+
_completion:
6+
class: '\Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand'
7+
name:
8+
drush:
9+
class: '\Drupal\Console\Command\Exclude\DrushCommand'
10+
arguments: ['@console.configuration_manager', '@console.chain_queue']
11+
elephpant:
12+
class: '\Drupal\Console\Command\Exclude\ElephpantCommand'
13+
arguments: ['@console.root', '@console.renderer']
14+
druplicon:
15+
class: '\Drupal\Console\Command\Exclude\DrupliconCommand'
16+
arguments: ['@console.root', '@console.renderer']
17+
'develop:example':
18+
class: '\Drupal\Console\Command\Develop\ExampleCommand'
19+
'develop:example:container:aware':
20+
class: '\Drupal\Console\Command\Develop\ExampleContainerAwareCommand'
221
languages:
322
en: 'English'
423
es: 'Español'

config/dist/drush.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
commands:
2+
core:
3+
archive-dump: ~
4+
config-debug: config:debug
5+
config-export: config:export
6+
config-import: config:import
7+
archive-dump: ~
8+
archive-restore: ~
9+
browse: ~
10+
core-cli: ~
11+
core-config: ~
12+
core-cron: ~
13+
core-execute: ~
14+
core-init: init
15+
core-quick-drupal: ~
16+
core-requirements: site:status
17+
core-rsync: ~
18+
core-status: site:status
19+
core-topic: ~
20+
drupal-directory: ~
21+
entity-updates: update:entities
22+
help: help
23+
site-install: site:install
24+
make:
25+
make: ~
26+
make-convert: ~

src/ConsoleApplication.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function trans($key)
6363
public function doRun(InputInterface $input, OutputInterface $output)
6464
{
6565
$this->registerEvents();
66+
$this->registerCommandsFromAutoWireConfiguration();
6667
return parent::doRun(
6768
$input,
6869
$output
@@ -164,4 +165,79 @@ private function addOptions()
164165
)
165166
);
166167
}
168+
169+
private function registerCommandsFromAutoWireConfiguration()
170+
{
171+
$configuration = $this->container->get('console.configuration_manager')
172+
->getConfiguration();
173+
174+
$autoWireForcedCommands = $configuration->get(
175+
sprintf(
176+
'application.autowire.commands.forced'
177+
)
178+
);
179+
180+
foreach ($autoWireForcedCommands as $autoWireForcedCommand) {
181+
try {
182+
$reflectionClass = new \ReflectionClass(
183+
$autoWireForcedCommand['class']
184+
);
185+
186+
$command = $reflectionClass->newInstance();
187+
188+
if (method_exists($command, 'setTranslator')) {
189+
$command->setTranslator(
190+
$this->container->get('console.translator_manager')
191+
);
192+
}
193+
if (method_exists($command, 'setContainer')) {
194+
$command->setContainer(
195+
$this->container->get('service_container')
196+
);
197+
}
198+
199+
$this->add($command);
200+
} catch (\Exception $e) {
201+
continue;
202+
}
203+
}
204+
205+
$autoWireNameCommand = $configuration->get(
206+
sprintf(
207+
'application.autowire.commands.name.%s',
208+
$this->commandName
209+
)
210+
);
211+
212+
if ($autoWireNameCommand) {
213+
try {
214+
$arguments = [];
215+
if (array_key_exists('arguments', $autoWireNameCommand)) {
216+
foreach ($autoWireNameCommand['arguments'] as $argument) {
217+
$argument = substr($argument, 1);
218+
$arguments[] = $this->container->get($argument);
219+
}
220+
}
221+
222+
$reflectionClass = new \ReflectionClass(
223+
$autoWireNameCommand['class']
224+
);
225+
$command = $reflectionClass->newInstanceArgs($arguments);
226+
227+
if (method_exists($command, 'setTranslator')) {
228+
$command->setTranslator(
229+
$this->container->get('console.translator_manager')
230+
);
231+
}
232+
if (method_exists($command, 'setContainer')) {
233+
$command->setContainer(
234+
$this->container->get('service_container')
235+
);
236+
}
237+
238+
$this->add($command);
239+
} catch (\Exception $e) {
240+
}
241+
}
242+
}
167243
}

src/Exclude/DrupliconCommand.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Console\Command\Exclude\DrupliconCommand.
6+
*/
7+
8+
namespace Drupal\Console\Command\Exclude;
9+
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Finder\Finder;
14+
use Drupal\Console\Command\Shared\CommandTrait;
15+
use Drupal\Console\Utils\TwigRenderer;
16+
use Drupal\Console\Style\DrupalStyle;
17+
18+
class DrupliconCommand extends Command
19+
{
20+
use CommandTrait;
21+
22+
protected $appRoot;
23+
/**
24+
* @var TwigRenderer
25+
*/
26+
protected $renderer;
27+
28+
/**
29+
* DrupliconCommand constructor.
30+
* @param string $appRoot
31+
* @param TwigRenderer $renderer
32+
*/
33+
public function __construct(
34+
$appRoot,
35+
TwigRenderer $renderer
36+
) {
37+
$this->appRoot = $appRoot;
38+
$this->renderer = $renderer;
39+
parent::__construct();
40+
}
41+
42+
protected function configure()
43+
{
44+
$this
45+
->setName('druplicon')
46+
->setDescription($this->trans('application.commands.druplicon.description'));
47+
}
48+
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$io = new DrupalStyle($input, $output);
52+
53+
$directory = sprintf(
54+
'%s/templates/core/druplicon/',
55+
$this->appRoot . DRUPAL_CONSOLE
56+
);
57+
58+
$finder = new Finder();
59+
$finder->files()
60+
->name('*.twig')
61+
->in($directory);
62+
63+
$templates = [];
64+
65+
foreach ($finder as $template) {
66+
$templates[] = $template->getRelativePathname();
67+
}
68+
69+
$druplicon = $this->renderer->render(
70+
sprintf(
71+
'core/druplicon/%s',
72+
$templates[array_rand($templates)]
73+
)
74+
);
75+
76+
$io->writeln($druplicon);
77+
}
78+
}

src/Exclude/DrushCommand.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Console\Command\Exclude\DrushCommand.
6+
*/
7+
8+
namespace Drupal\Console\Command\Exclude;
9+
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Command\Command;
14+
use Drupal\Console\Command\Shared\CommandTrait;
15+
use Drupal\Console\Utils\ConfigurationManager;
16+
use Drupal\Console\Utils\ChainQueue;
17+
use Drupal\Console\Style\DrupalStyle;
18+
19+
class DrushCommand extends Command
20+
{
21+
use CommandTrait;
22+
23+
/**
24+
* @var ConfigurationManager
25+
*/
26+
protected $configurationManager;
27+
28+
/**
29+
* @var ChainQueue
30+
*/
31+
protected $chainQueue;
32+
33+
/**
34+
* DrushCommand constructor.
35+
* @param ConfigurationManager $configurationManager
36+
* @param ChainQueue $chainQueue
37+
*/
38+
public function __construct(
39+
ConfigurationManager $configurationManager,
40+
ChainQueue $chainQueue
41+
) {
42+
$this->configurationManager = $configurationManager;
43+
$this->chainQueue = $chainQueue;
44+
parent::__construct();
45+
}
46+
47+
protected function configure()
48+
{
49+
$this
50+
->setName('drush')
51+
->setDescription($this->trans('application.commands.drush.description'))
52+
->addArgument(
53+
'command-name',
54+
InputArgument::OPTIONAL,
55+
$this->trans('commands.drush.arguments.command-name'),
56+
null
57+
);
58+
}
59+
60+
protected function execute(InputInterface $input, OutputInterface $output)
61+
{
62+
$io = new DrupalStyle($input, $output);
63+
$commandName = $input->getArgument('command-name');
64+
65+
$alternative = $this->configurationManager->readDrushEquivalents($commandName);
66+
67+
$io->info('Drupal Console equivalents');
68+
$tableHeader = ['drush','drupal console'];
69+
70+
if (!$commandName && is_array($alternative)) {
71+
$io->table(
72+
$tableHeader,
73+
$alternative
74+
);
75+
76+
return 0;
77+
}
78+
79+
$io->table(
80+
$tableHeader,
81+
[[$commandName, $alternative]]
82+
);
83+
84+
if ($this->getApplication()->has($alternative)) {
85+
$this->chainQueue->addCommand(
86+
'help',
87+
['command_name' => $alternative]
88+
);
89+
90+
return 0;
91+
}
92+
93+
return 0;
94+
}
95+
}

src/Exclude/ElephpantCommand.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Console\Command\Exclude\ElephpantCommand.
6+
*/
7+
8+
namespace Drupal\Console\Command\Exclude;
9+
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Finder\Finder;
14+
use Drupal\Console\Command\Shared\CommandTrait;
15+
use Drupal\Console\Utils\TwigRenderer;
16+
use Drupal\Console\Style\DrupalStyle;
17+
18+
class ElephpantCommand extends Command
19+
{
20+
use CommandTrait;
21+
22+
protected $appRoot;
23+
/**
24+
* @var TwigRenderer
25+
*/
26+
protected $renderer;
27+
28+
/**
29+
* DrupliconCommand constructor.
30+
* @param string $appRoot
31+
* @param TwigRenderer $renderer
32+
*/
33+
public function __construct(
34+
$appRoot,
35+
TwigRenderer $renderer
36+
) {
37+
$this->appRoot = $appRoot;
38+
$this->renderer = $renderer;
39+
parent::__construct();
40+
}
41+
42+
protected function configure()
43+
{
44+
$this
45+
->setName('elephpant')
46+
->setDescription($this->trans('application.commands.elephpant.description'));
47+
}
48+
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$io = new DrupalStyle($input, $output);
52+
53+
$directory = sprintf(
54+
'%stemplates/core/elephpant/',
55+
$this->appRoot . DRUPAL_CONSOLE
56+
);
57+
58+
$finder = new Finder();
59+
$finder->files()
60+
->name('*.twig')
61+
->in($directory);
62+
63+
$templates = [];
64+
65+
foreach ($finder as $template) {
66+
$templates[] = $template->getRelativePathname();
67+
}
68+
69+
$elephpant = $this->renderer->render(
70+
sprintf(
71+
'core/elephpant/%s',
72+
$templates[array_rand($templates)]
73+
)
74+
);
75+
76+
$io->writeln($elephpant);
77+
}
78+
}

0 commit comments

Comments
 (0)