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