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-rc21';
30 public function __construct(ContainerInterface $container)
32 parent::__construct($container, $this::NAME, $this::VERSION);
36 * Returns the long version of the application.
38 * @return string The long application version
40 public function getLongVersion()
44 if ('UNKNOWN' !== $this->getName()) {
45 if ('UNKNOWN' !== $this->getVersion()) {
46 $output .= sprintf('<info>%s</info> version <comment>%s</comment>', $this->getName(), $this->getVersion());
48 $output .= sprintf('<info>%s</info>', $this->getName());
51 $output .= '<info>Console Tool</info>';
60 public function doRun(InputInterface $input, OutputInterface $output)
62 $this->registerGenerators();
63 $this->registerCommands();
64 $clear = $this->container->get('console.configuration_manager')
66 ->get('application.clear')?:false;
67 if ($clear === true || $clear === 'true') {
68 $output->write(sprintf("\033\143"));
71 $exitCode = parent::doRun($input, $output);
75 private function registerGenerators()
77 if ($this->container->hasParameter('drupal.generators')) {
78 $consoleGenerators = $this->container->getParameter(
82 $consoleGenerators = array_keys(
83 $this->container->findTaggedServiceIds('drupal.generator')
87 foreach ($consoleGenerators as $name) {
88 if (!$this->container->has($name)) {
93 $generator = $this->container->get($name);
94 } catch (\Exception $e) {
95 echo $name . ' - ' . $e->getMessage() . PHP_EOL;
104 if (method_exists($generator, 'setRenderer')) {
105 $generator->setRenderer(
106 $this->container->get('console.renderer')
110 if (method_exists($generator, 'setFileQueue')) {
111 $generator->setFileQueue(
112 $this->container->get('console.file_queue')
118 private function registerCommands()
120 if ($this->container->hasParameter('drupal.commands')) {
121 $consoleCommands = $this->container->getParameter(
125 $consoleCommands = array_keys(
126 $this->container->findTaggedServiceIds('drupal.command')
128 $this->container->setParameter(
130 'application.site.errors.settings'
134 $serviceDefinitions = [];
135 $annotationValidator = null;
136 $annotationCommandReader = null;
137 if ($this->container->hasParameter('console.service_definitions')) {
138 $serviceDefinitions = $this->container
139 ->getParameter('console.service_definitions');
142 * @var DrupalCommandAnnotationReader $annotationCommandReader
144 $annotationCommandReader = $this->container
145 ->get('console.annotation_command_reader');
148 * @var AnnotationValidator $annotationValidator
150 $annotationValidator = $this->container
151 ->get('console.annotation_validator');
154 $aliases = $this->container->get('console.configuration_manager')
156 ->get('application.commands.aliases')?:[];
158 foreach ($consoleCommands as $name) {
159 AnnotationRegistry::reset();
160 AnnotationRegistry::registerLoader(
162 $this->container->get('class_loader'),
167 if (!$this->container->has($name)) {
171 if ($annotationValidator && $annotationCommandReader) {
172 if (!$serviceDefinition = $serviceDefinitions[$name]) {
176 $annotation = $annotationCommandReader
177 ->readAnnotation($serviceDefinition->getClass());
179 $this->container->get('console.translator_manager')
180 ->addResourceTranslationsByExtension(
181 $annotation['extension'],
182 $annotation['extensionType']
186 if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) {
192 $command = $this->container->get($name);
193 } catch (\Exception $e) {
194 echo $name . ' - ' . $e->getMessage() . PHP_EOL;
203 if (method_exists($command, 'setTranslator')) {
204 $command->setTranslator(
205 $this->container->get('console.translator_manager')
209 if (method_exists($command, 'setContainer')) {
210 $command->setContainer(
211 $this->container->get('service_container')
215 if (array_key_exists($command->getName(), $aliases)) {
216 $commandAliases = $aliases[$command->getName()];
217 if (!is_array($commandAliases)) {
218 $commandAliases = [$commandAliases];
220 $command->setAliases($commandAliases);
223 $this->add($command);
227 public function getData()
241 $languages = $this->container->get('console.configuration_manager')
243 ->get('application.languages');
246 foreach ($singleCommands as $singleCommand) {
247 $data['commands']['misc'][] = $this->commandData($singleCommand);
250 $namespaces = array_filter(
251 $this->getNamespaces(), function ($item) {
252 return (strpos($item, ':')<=0);
256 array_unshift($namespaces, 'misc');
258 foreach ($namespaces as $namespace) {
259 $commands = $this->all($namespace);
261 $commands, function ($cmd1, $cmd2) {
262 return strcmp($cmd1->getName(), $cmd2->getName());
266 foreach ($commands as $command) {
267 if (method_exists($command, 'getModule')) {
268 if ($command->getModule() == 'Console') {
269 $data['commands'][$namespace][] = $this->commandData(
274 $data['commands'][$namespace][] = $this->commandData(
281 $input = $this->getDefinition();
283 foreach ($input->getOptions() as $option) {
285 'name' => $option->getName(),
286 'description' => $this->trans('application.options.'.$option->getName())
290 foreach ($input->getArguments() as $argument) {
292 'name' => $argument->getName(),
293 'description' => $this->trans('application.arguments.'.$argument->getName())
297 $data['application'] = [
298 'namespaces' => $namespaces,
299 'options' => $options,
300 'arguments' => $arguments,
301 'languages' => $languages,
303 'title' => $this->trans('commands.generate.doc.gitbook.messages.title'),
304 'note' => $this->trans('commands.generate.doc.gitbook.messages.note'),
305 'note_description' => $this->trans('commands.generate.doc.gitbook.messages.note-description'),
306 'command' => $this->trans('commands.generate.doc.gitbook.messages.command'),
307 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
308 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
309 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
310 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
311 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
312 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
320 private function commandData($commandName)
322 if (!$this->has($commandName)) {
326 $command = $this->find($commandName);
328 $input = $command->getDefinition();
330 foreach ($input->getOptions() as $option) {
331 $options[$option->getName()] = [
332 'name' => $option->getName(),
333 'description' => $this->trans($option->getDescription()),
338 foreach ($input->getArguments() as $argument) {
339 $arguments[$argument->getName()] = [
340 'name' => $argument->getName(),
341 'description' => $this->trans($argument->getDescription()),
345 $commandKey = str_replace(':', '.', $command->getName());
348 for ($i = 0; $i < 5; $i++) {
349 $description = sprintf(
350 'commands.%s.examples.%s.description',
354 $execution = sprintf(
355 'commands.%s.examples.%s.execution',
360 if ($description != $this->trans($description)) {
362 'description' => $this->trans($description),
363 'execution' => $this->trans($execution)
371 'name' => $command->getName(),
372 'description' => $command->getDescription(),
373 'options' => $options,
374 'arguments' => $arguments,
375 'examples' => $examples,
376 'aliases' => $command->getAliases(),
377 'key' => $commandKey,
378 'dashed' => str_replace(':', '-', $command->getName()),
380 'usage' => $this->trans('commands.generate.doc.gitbook.messages.usage'),
381 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
382 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
383 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
384 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
385 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
386 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
393 public function setContainer($container)
395 $this->container = $container;
396 $this->registerGenerators();
397 $this->registerCommands();