5 * Contains \Drupal\Console\Develop\ExampleContainerAwareCommand.
8 namespace Drupal\Console\Command\Develop;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Console\Core\Command\Shared\CommandTrait;
14 use Drupal\Console\Core\Style\DrupalStyle;
17 * Class ExampleContainerAwareCommand
19 * @package Drupal\Console\Command\Develop
21 class ExampleContainerAwareCommand extends Command
26 * ExampleContainerAwareCommand constructor.
28 public function __construct()
30 parent::__construct();
36 protected function configure()
38 $this->setName('develop:example:container:aware');
44 protected function interact(InputInterface $input, OutputInterface $output)
51 protected function execute(InputInterface $input, OutputInterface $output)
53 /* Register your command as a service
55 * Make sure you register your command class at
56 * config/services/namespace.yml file and add the `drupal.command` tag.
58 * develop_example_container_aware:
59 * class: Drupal\Console\Command\Develop\ExampleContainerAwareCommand
61 * - { name: drupal.command }
63 * NOTE: Make the proper changes on the namespace and class
64 * according your new command.
66 * DrupalConsole extends the SymfonyStyle class to provide
67 * an standardized Output Formatting Style.
69 * Drupal Console provides the DrupalStyle helper class:
71 $io = new DrupalStyle($input, $output);
72 $io->simple('This text could be translatable by');
73 $io->simple('adding a YAML file at "config/translations/LANGUAGE/command.name.yml"');
76 * By using ContainerAwareCommandTrait on your class for the command
77 * (instead of the more basic CommandTrait), you have access to
78 * the service container.
80 * In other words, you can access to any configured Drupal service
81 * using the provided get method.
83 * $this->get('entity_type.manager');
85 * Reading user input argument
86 * $input->getArgument('ARGUMENT_NAME');
88 * Reading user input option
89 * $input->getOption('OPTION_NAME');