3 namespace Drupal\Console;
5 use Doctrine\Common\Annotations\AnnotationRegistry;
6 use Symfony\Component\Console\Input\InputInterface;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Drupal\Console\Annotations\DrupalCommandAnnotationReader;
10 use Drupal\Console\Utils\AnnotationValidator;
11 use Drupal\Console\Core\Application as BaseApplication;
16 * @package Drupal\Console
18 class Application extends BaseApplication
23 const NAME = 'Drupal Console';
28 const VERSION = '1.0.0-rc16';
30 public function __construct(ContainerInterface $container)
32 parent::__construct($container, $this::NAME, $this::VERSION);
38 public function doRun(InputInterface $input, OutputInterface $output)
40 $this->registerGenerators();
41 $this->registerCommands();
42 $clear = $this->container->get('console.configuration_manager')
44 ->get('application.clear')?:false;
45 if ($clear === true || $clear === 'true') {
46 $output->write(sprintf("\033\143"));
49 $exitCode = parent::doRun($input, $output);
53 private function registerGenerators()
55 if ($this->container->hasParameter('drupal.generators')) {
56 $consoleGenerators = $this->container->getParameter(
60 $consoleGenerators = array_keys(
61 $this->container->findTaggedServiceIds('drupal.generator')
65 foreach ($consoleGenerators as $name) {
66 if (!$this->container->has($name)) {
71 $generator = $this->container->get($name);
72 } catch (\Exception $e) {
73 echo $name . ' - ' . $e->getMessage() . PHP_EOL;
82 if (method_exists($generator, 'setRenderer')) {
83 $generator->setRenderer(
84 $this->container->get('console.renderer')
88 if (method_exists($generator, 'setFileQueue')) {
89 $generator->setFileQueue(
90 $this->container->get('console.file_queue')
96 private function registerCommands()
98 if ($this->container->hasParameter('drupal.commands')) {
99 $consoleCommands = $this->container->getParameter(
103 $consoleCommands = array_keys(
104 $this->container->findTaggedServiceIds('drupal.command')
106 $this->container->setParameter(
108 'application.site.errors.settings'
112 $serviceDefinitions = [];
113 $annotationValidator = null;
114 $annotationCommandReader = null;
115 if ($this->container->hasParameter('console.service_definitions')) {
116 $serviceDefinitions = $this->container
117 ->getParameter('console.service_definitions');
120 * @var DrupalCommandAnnotationReader $annotationCommandReader
122 $annotationCommandReader = $this->container
123 ->get('console.annotation_command_reader');
126 * @var AnnotationValidator $annotationValidator
128 $annotationValidator = $this->container
129 ->get('console.annotation_validator');
132 $aliases = $this->container->get('console.configuration_manager')
134 ->get('application.commands.aliases')?:[];
136 foreach ($consoleCommands as $name) {
137 AnnotationRegistry::reset();
138 AnnotationRegistry::registerLoader(
140 $this->container->get('class_loader'),
145 if (!$this->container->has($name)) {
149 if ($annotationValidator && $annotationCommandReader) {
150 if (!$serviceDefinition = $serviceDefinitions[$name]) {
154 $annotation = $annotationCommandReader
155 ->readAnnotation($serviceDefinition->getClass());
157 $this->container->get('console.translator_manager')
158 ->addResourceTranslationsByExtension(
159 $annotation['extension'],
160 $annotation['extensionType']
164 if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) {
170 $command = $this->container->get($name);
171 } catch (\Exception $e) {
172 echo $name . ' - ' . $e->getMessage() . PHP_EOL;
181 if (method_exists($command, 'setTranslator')) {
182 $command->setTranslator(
183 $this->container->get('console.translator_manager')
187 if (method_exists($command, 'setContainer')) {
188 $command->setContainer(
189 $this->container->get('service_container')
193 if (array_key_exists($command->getName(), $aliases)) {
194 $commandAliases = $aliases[$command->getName()];
195 if (!is_array($commandAliases)) {
196 $commandAliases = [$commandAliases];
198 $command->setAliases($commandAliases);
201 $this->add($command);
205 public function getData()
219 $languages = $this->container->get('console.configuration_manager')
221 ->get('application.languages');
224 foreach ($singleCommands as $singleCommand) {
225 $data['commands']['misc'][] = $this->commandData($singleCommand);
228 $namespaces = array_filter(
229 $this->getNamespaces(), function ($item) {
230 return (strpos($item, ':')<=0);
234 array_unshift($namespaces, 'misc');
236 foreach ($namespaces as $namespace) {
237 $commands = $this->all($namespace);
239 $commands, function ($cmd1, $cmd2) {
240 return strcmp($cmd1->getName(), $cmd2->getName());
244 foreach ($commands as $command) {
245 if (method_exists($command, 'getModule')) {
246 if ($command->getModule() == 'Console') {
247 $data['commands'][$namespace][] = $this->commandData(
252 $data['commands'][$namespace][] = $this->commandData(
259 $input = $this->getDefinition();
261 foreach ($input->getOptions() as $option) {
263 'name' => $option->getName(),
264 'description' => $this->trans('application.options.'.$option->getName())
268 foreach ($input->getArguments() as $argument) {
270 'name' => $argument->getName(),
271 'description' => $this->trans('application.arguments.'.$argument->getName())
275 $data['application'] = [
276 'namespaces' => $namespaces,
277 'options' => $options,
278 'arguments' => $arguments,
279 'languages' => $languages,
281 'title' => $this->trans('commands.generate.doc.gitbook.messages.title'),
282 'note' => $this->trans('commands.generate.doc.gitbook.messages.note'),
283 'note_description' => $this->trans('commands.generate.doc.gitbook.messages.note-description'),
284 'command' => $this->trans('commands.generate.doc.gitbook.messages.command'),
285 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
286 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
287 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
288 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
289 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
290 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
298 private function commandData($commandName)
300 if (!$this->has($commandName)) {
304 $command = $this->find($commandName);
306 $input = $command->getDefinition();
308 foreach ($input->getOptions() as $option) {
309 $options[$option->getName()] = [
310 'name' => $option->getName(),
311 'description' => $this->trans($option->getDescription()),
316 foreach ($input->getArguments() as $argument) {
317 $arguments[$argument->getName()] = [
318 'name' => $argument->getName(),
319 'description' => $this->trans($argument->getDescription()),
323 $commandKey = str_replace(':', '.', $command->getName());
326 for ($i = 0; $i < 5; $i++) {
327 $description = sprintf(
328 'commands.%s.examples.%s.description',
332 $execution = sprintf(
333 'commands.%s.examples.%s.execution',
338 if ($description != $this->trans($description)) {
340 'description' => $this->trans($description),
341 'execution' => $this->trans($execution)
349 'name' => $command->getName(),
350 'description' => $command->getDescription(),
351 'options' => $options,
352 'arguments' => $arguments,
353 'examples' => $examples,
354 'aliases' => $command->getAliases(),
355 'key' => $commandKey,
356 'dashed' => str_replace(':', '-', $command->getName()),
358 'usage' => $this->trans('commands.generate.doc.gitbook.messages.usage'),
359 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
360 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
361 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
362 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
363 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
364 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
371 public function setContainer($container)
373 $this->container = $container;
374 $this->registerGenerators();
375 $this->registerCommands();