/**
* @var string
*/
- const VERSION = '1.0.0-rc21';
+ const VERSION = '1.6.1';
public function __construct(ContainerInterface $container)
{
$output .= sprintf('<info>%s</info>', $this->getName());
}
} else {
- $output .= '<info>Console Tool</info>';
+ $output .= '<info>Drupal Console</info>';
}
return $output;
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
- $this->registerGenerators();
- $this->registerCommands();
- $clear = $this->container->get('console.configuration_manager')
- ->getConfiguration()
- ->get('application.clear')?:false;
- if ($clear === true || $clear === 'true') {
- $output->write(sprintf("\033\143"));
- }
+ $this->validateCommands();
- $exitCode = parent::doRun($input, $output);
- return $exitCode;
+ return parent::doRun($input, $output);
}
- private function registerGenerators()
+ public function validateCommands()
{
- if ($this->container->hasParameter('drupal.generators')) {
- $consoleGenerators = $this->container->getParameter(
- 'drupal.generators'
- );
- } else {
- $consoleGenerators = array_keys(
- $this->container->findTaggedServiceIds('drupal.generator')
- );
- }
-
- foreach ($consoleGenerators as $name) {
- if (!$this->container->has($name)) {
- continue;
- }
-
- try {
- $generator = $this->container->get($name);
- } catch (\Exception $e) {
- echo $name . ' - ' . $e->getMessage() . PHP_EOL;
+ $consoleCommands = $this->container
+ ->findTaggedServiceIds('drupal.command');
- continue;
- }
-
- if (!$generator) {
- continue;
- }
+ if (!$consoleCommands) {
+ return;
+ }
- if (method_exists($generator, 'setRenderer')) {
- $generator->setRenderer(
- $this->container->get('console.renderer')
- );
- }
+ $serviceDefinitions = $this->container->getDefinitions();
- if (method_exists($generator, 'setFileQueue')) {
- $generator->setFileQueue(
- $this->container->get('console.file_queue')
- );
- }
+ if (!$serviceDefinitions) {
+ return;
}
- }
- private function registerCommands()
- {
- if ($this->container->hasParameter('drupal.commands')) {
- $consoleCommands = $this->container->getParameter(
- 'drupal.commands'
- );
- } else {
- $consoleCommands = array_keys(
- $this->container->findTaggedServiceIds('drupal.command')
- );
- $this->container->setParameter(
- 'console.warning',
- 'application.site.errors.settings'
- );
+ if (!$this->container->has('console.annotation_command_reader')) {
+ return;
}
- $serviceDefinitions = [];
- $annotationValidator = null;
- $annotationCommandReader = null;
- if ($this->container->hasParameter('console.service_definitions')) {
- $serviceDefinitions = $this->container
- ->getParameter('console.service_definitions');
+ /**
+ * @var DrupalCommandAnnotationReader $annotationCommandReader
+ */
+ $annotationCommandReader = $this->container
+ ->get('console.annotation_command_reader');
- /**
- * @var DrupalCommandAnnotationReader $annotationCommandReader
- */
- $annotationCommandReader = $this->container
- ->get('console.annotation_command_reader');
-
- /**
- * @var AnnotationValidator $annotationValidator
- */
- $annotationValidator = $this->container
- ->get('console.annotation_validator');
+ if (!$this->container->has('console.annotation_validator')) {
+ return;
}
- $aliases = $this->container->get('console.configuration_manager')
- ->getConfiguration()
- ->get('application.commands.aliases')?:[];
+ /**
+ * @var AnnotationValidator $annotationValidator
+ */
+ $annotationValidator = $this->container
+ ->get('console.annotation_validator');
+
+ $invalidCommands = [];
- foreach ($consoleCommands as $name) {
+ foreach ($consoleCommands as $name => $tags) {
AnnotationRegistry::reset();
AnnotationRegistry::registerLoader(
[
);
if (!$this->container->has($name)) {
+ $invalidCommands[] = $name;
continue;
}
- if ($annotationValidator && $annotationCommandReader) {
- if (!$serviceDefinition = $serviceDefinitions[$name]) {
- continue;
- }
-
- $annotation = $annotationCommandReader
- ->readAnnotation($serviceDefinition->getClass());
- if ($annotation) {
- $this->container->get('console.translator_manager')
- ->addResourceTranslationsByExtension(
- $annotation['extension'],
- $annotation['extensionType']
- );
- }
-
- if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) {
- continue;
- }
- }
-
- try {
- $command = $this->container->get($name);
- } catch (\Exception $e) {
- echo $name . ' - ' . $e->getMessage() . PHP_EOL;
-
+ if (!$serviceDefinition = $serviceDefinitions[$name]) {
+ $invalidCommands[] = $name;
continue;
}
- if (!$command) {
+ if (!$annotationValidator->isValidCommand(
+ $serviceDefinition->getClass()
+ )
+ ) {
+ $invalidCommands[] = $name;
continue;
}
- if (method_exists($command, 'setTranslator')) {
- $command->setTranslator(
- $this->container->get('console.translator_manager')
- );
- }
-
- if (method_exists($command, 'setContainer')) {
- $command->setContainer(
- $this->container->get('service_container')
- );
- }
-
- if (array_key_exists($command->getName(), $aliases)) {
- $commandAliases = $aliases[$command->getName()];
- if (!is_array($commandAliases)) {
- $commandAliases = [$commandAliases];
- }
- $command->setAliases($commandAliases);
- }
-
- $this->add($command);
- }
- }
-
- public function getData()
- {
- $singleCommands = [
- 'about',
- 'chain',
- 'check',
- 'exec',
- 'help',
- 'init',
- 'list',
- 'shell',
- 'server'
- ];
-
- $languages = $this->container->get('console.configuration_manager')
- ->getConfiguration()
- ->get('application.languages');
-
- $data = [];
- foreach ($singleCommands as $singleCommand) {
- $data['commands']['misc'][] = $this->commandData($singleCommand);
- }
-
- $namespaces = array_filter(
- $this->getNamespaces(), function ($item) {
- return (strpos($item, ':')<=0);
- }
- );
- sort($namespaces);
- array_unshift($namespaces, 'misc');
-
- foreach ($namespaces as $namespace) {
- $commands = $this->all($namespace);
- usort(
- $commands, function ($cmd1, $cmd2) {
- return strcmp($cmd1->getName(), $cmd2->getName());
- }
- );
-
- foreach ($commands as $command) {
- if (method_exists($command, 'getModule')) {
- if ($command->getModule() == 'Console') {
- $data['commands'][$namespace][] = $this->commandData(
- $command->getName()
- );
- }
- } else {
- $data['commands'][$namespace][] = $this->commandData(
- $command->getName()
+ $annotation = $annotationCommandReader
+ ->readAnnotation($serviceDefinition->getClass());
+ if ($annotation) {
+ $this->container->get('console.translator_manager')
+ ->addResourceTranslationsByExtension(
+ $annotation['extension'],
+ $annotation['extensionType']
);
- }
}
}
- $input = $this->getDefinition();
- $options = [];
- foreach ($input->getOptions() as $option) {
- $options[] = [
- 'name' => $option->getName(),
- 'description' => $this->trans('application.options.'.$option->getName())
- ];
- }
- $arguments = [];
- foreach ($input->getArguments() as $argument) {
- $arguments[] = [
- 'name' => $argument->getName(),
- 'description' => $this->trans('application.arguments.'.$argument->getName())
- ];
- }
-
- $data['application'] = [
- 'namespaces' => $namespaces,
- 'options' => $options,
- 'arguments' => $arguments,
- 'languages' => $languages,
- 'messages' => [
- 'title' => $this->trans('commands.generate.doc.gitbook.messages.title'),
- 'note' => $this->trans('commands.generate.doc.gitbook.messages.note'),
- 'note_description' => $this->trans('commands.generate.doc.gitbook.messages.note-description'),
- 'command' => $this->trans('commands.generate.doc.gitbook.messages.command'),
- 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
- 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
- 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
- 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
- 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
- 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
- ],
- 'examples' => []
- ];
+ $this->container
+ ->get('console.key_value_storage')
+ ->set('invalid_commands', $invalidCommands);
- return $data;
- }
-
- private function commandData($commandName)
- {
- if (!$this->has($commandName)) {
- return [];
- }
-
- $command = $this->find($commandName);
-
- $input = $command->getDefinition();
- $options = [];
- foreach ($input->getOptions() as $option) {
- $options[$option->getName()] = [
- 'name' => $option->getName(),
- 'description' => $this->trans($option->getDescription()),
- ];
- }
-
- $arguments = [];
- foreach ($input->getArguments() as $argument) {
- $arguments[$argument->getName()] = [
- 'name' => $argument->getName(),
- 'description' => $this->trans($argument->getDescription()),
- ];
- }
-
- $commandKey = str_replace(':', '.', $command->getName());
-
- $examples = [];
- for ($i = 0; $i < 5; $i++) {
- $description = sprintf(
- 'commands.%s.examples.%s.description',
- $commandKey,
- $i
- );
- $execution = sprintf(
- 'commands.%s.examples.%s.execution',
- $commandKey,
- $i
- );
-
- if ($description != $this->trans($description)) {
- $examples[] = [
- 'description' => $this->trans($description),
- 'execution' => $this->trans($execution)
- ];
- } else {
- break;
- }
- }
-
- $data = [
- 'name' => $command->getName(),
- 'description' => $command->getDescription(),
- 'options' => $options,
- 'arguments' => $arguments,
- 'examples' => $examples,
- 'aliases' => $command->getAliases(),
- 'key' => $commandKey,
- 'dashed' => str_replace(':', '-', $command->getName()),
- 'messages' => [
- 'usage' => $this->trans('commands.generate.doc.gitbook.messages.usage'),
- 'options' => $this->trans('commands.generate.doc.gitbook.messages.options'),
- 'option' => $this->trans('commands.generate.doc.gitbook.messages.option'),
- 'details' => $this->trans('commands.generate.doc.gitbook.messages.details'),
- 'arguments' => $this->trans('commands.generate.doc.gitbook.messages.arguments'),
- 'argument' => $this->trans('commands.generate.doc.gitbook.messages.argument'),
- 'examples' => $this->trans('commands.generate.doc.gitbook.messages.examples')
- ],
- ];
-
- return $data;
- }
-
- public function setContainer($container)
- {
- $this->container = $container;
- $this->registerGenerators();
- $this->registerCommands();
+ return;
}
}