Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Create / TermsCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Create\TermsCommand.
6  */
7
8 namespace Drupal\Console\Command\Create;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Annotations\DrupalCommand;
17 use Drupal\Console\Utils\Create\TermData;
18 use Drupal\Console\Utils\DrupalApi;
19 use Drupal\Console\Core\Style\DrupalStyle;
20
21 /**
22  * Class TermsCommand
23  *
24  * @package Drupal\Console\Command\Generate
25  *
26  * @DrupalCommand(
27  *     extension = "features",
28  *     extensionType = "module",
29  *     dependencies={"taxonomy"}
30  * )
31  */
32 class TermsCommand extends Command
33 {
34     use CommandTrait;
35
36     /**
37      * @var DrupalApi
38      */
39     protected $drupalApi;
40     /**
41      * @var TermData
42      */
43     protected $createTermData;
44
45     /**
46      * TermsCommand constructor.
47      *
48      * @param DrupalApi $drupalApi
49      * @param TermData  $createTermData
50      */
51     public function __construct(
52         DrupalApi $drupalApi,
53         TermData $createTermData
54     ) {
55         $this->drupalApi = $drupalApi;
56         $this->createTermData = $createTermData;
57         parent::__construct();
58     }
59
60     /**
61      * {@inheritdoc}
62      */
63     protected function configure()
64     {
65         $this
66             ->setName('create:terms')
67             ->setDescription($this->trans('commands.create.terms.description'))
68             ->addArgument(
69                 'vocabularies',
70                 InputArgument::IS_ARRAY,
71                 $this->trans('commands.create.terms.arguments.vocabularies')
72             )
73             ->addOption(
74                 'limit',
75                 null,
76                 InputOption::VALUE_OPTIONAL,
77                 $this->trans('commands.create.terms.options.limit')
78             )
79             ->addOption(
80                 'name-words',
81                 null,
82                 InputOption::VALUE_OPTIONAL,
83                 $this->trans('commands.create.terms.options.name-words')
84             );
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     protected function interact(InputInterface $input, OutputInterface $output)
91     {
92         $io = new DrupalStyle($input, $output);
93
94         $vocabularies = $input->getArgument('vocabularies');
95         if (!$vocabularies) {
96             $vocabularies = $this->drupalApi->getVocabularies();
97             $vids = $io->choice(
98                 $this->trans('commands.create.terms.questions.vocabularies'),
99                 array_values($vocabularies),
100                 null,
101                 true
102             );
103
104             $vids = array_map(
105                 function ($vid) use ($vocabularies) {
106                     return array_search($vid, $vocabularies);
107                 },
108                 $vids
109             );
110
111             $input->setArgument('vocabularies', $vids);
112         }
113
114         $limit = $input->getOption('limit');
115         if (!$limit) {
116             $limit = $io->ask(
117                 $this->trans('commands.create.terms.questions.limit'),
118                 25
119             );
120             $input->setOption('limit', $limit);
121         }
122
123         $nameWords = $input->getOption('name-words');
124         if (!$nameWords) {
125             $nameWords = $io->ask(
126                 $this->trans('commands.create.terms.questions.name-words'),
127                 5
128             );
129
130             $input->setOption('name-words', $nameWords);
131         }
132     }
133
134     /**
135      * {@inheritdoc}
136      */
137     protected function execute(InputInterface $input, OutputInterface $output)
138     {
139         $io = new DrupalStyle($input, $output);
140
141         $vocabularies = $input->getArgument('vocabularies');
142         $limit = $input->getOption('limit')?:25;
143         $nameWords = $input->getOption('name-words')?:5;
144
145         if (!$vocabularies) {
146             $vocabularies = array_keys($this->drupalApi->getVocabularies());
147         }
148
149         $terms = $this->createTermData->create(
150             $vocabularies,
151             $limit,
152             $nameWords
153         );
154
155         $tableHeader = [
156             $this->trans('commands.create.terms.messages.term-id'),
157             $this->trans('commands.create.terms.messages.vocabulary'),
158             $this->trans('commands.create.terms.messages.name'),
159         ];
160
161         $io->table($tableHeader, $terms['success']);
162
163         $io->success(
164             sprintf(
165                 $this->trans('commands.create.terms.messages.created-terms'),
166                 $limit
167             )
168         );
169
170         return 0;
171     }
172 }