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