Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Create / NodesCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Create\NodesCommand.
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\Command\Shared\CreateTrait;
17 use Drupal\Console\Utils\Create\NodeData;
18 use Drupal\Console\Utils\DrupalApi;
19 use Drupal\Core\Language\LanguageInterface;
20
21 /**
22  * Class NodesCommand
23  *
24  * @package Drupal\Console\Command\Generate
25  *
26  * @DrupalCommand(
27  *     extension = "node",
28  *     extensionType = "module"
29  * )
30  */
31 class NodesCommand extends Command
32 {
33     use CreateTrait;
34
35     /**
36      * @var DrupalApi
37      */
38     protected $drupalApi;
39     /**
40      * @var NodeData
41      */
42     protected $createNodeData;
43
44     /**
45      * NodesCommand constructor.
46      *
47      * @param DrupalApi $drupalApi
48      * @param NodeData  $createNodeData
49      */
50     public function __construct(
51         DrupalApi $drupalApi,
52         NodeData $createNodeData
53     ) {
54         $this->drupalApi = $drupalApi;
55         $this->createNodeData = $createNodeData;
56         parent::__construct();
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     protected function configure()
63     {
64         $this
65             ->setName('create:nodes')
66             ->setDescription($this->trans('commands.create.nodes.description'))
67             ->addArgument(
68                 'content-types',
69                 InputArgument::IS_ARRAY,
70                 $this->trans('commands.create.nodes.arguments.content-types')
71             )
72             ->addOption(
73                 'limit',
74                 null,
75                 InputOption::VALUE_OPTIONAL,
76                 $this->trans('commands.create.nodes.options.limit')
77             )
78             ->addOption(
79                 'title-words',
80                 null,
81                 InputOption::VALUE_OPTIONAL,
82                 $this->trans('commands.create.nodes.options.title-words')
83             )
84             ->addOption(
85                 'time-range',
86                 null,
87                 InputOption::VALUE_OPTIONAL,
88                 $this->trans('commands.create.nodes.options.time-range')
89             )
90             ->addOption(
91                 'language',
92                 null,
93                 InputOption::VALUE_OPTIONAL,
94                 $this->trans('commands.create.nodes.options.language')
95             )->setAliases(['crn']);
96     }
97
98     /**
99      * {@inheritdoc}
100      */
101     protected function interact(InputInterface $input, OutputInterface $output)
102     {
103         $contentTypes = $input->getArgument('content-types');
104         if (!$contentTypes) {
105             $bundles = $this->drupalApi->getBundles();
106             $contentTypes = $this->getIo()->choice(
107                 $this->trans('commands.create.nodes.questions.content-type'),
108                 array_values($bundles),
109                 null,
110                 true
111             );
112
113             $contentTypes = array_map(
114                 function ($contentType) use ($bundles) {
115                     return array_search($contentType, $bundles);
116                 },
117                 $contentTypes
118             );
119
120             $input->setArgument('content-types', $contentTypes);
121         }
122
123         $limit = $input->getOption('limit');
124         if (!$limit) {
125             $limit = $this->getIo()->ask(
126                 $this->trans('commands.create.nodes.questions.limit'),
127                 25
128             );
129             $input->setOption('limit', $limit);
130         }
131
132         $titleWords = $input->getOption('title-words');
133         if (!$titleWords) {
134             $titleWords = $this->getIo()->ask(
135                 $this->trans('commands.create.nodes.questions.title-words'),
136                 5
137             );
138
139             $input->setOption('title-words', $titleWords);
140         }
141
142         $timeRange = $input->getOption('time-range');
143         if (!$timeRange) {
144             $timeRanges = $this->getTimeRange();
145
146             $timeRange = $this->getIo()->choice(
147                 $this->trans('commands.create.nodes.questions.time-range'),
148                 array_values($timeRanges)
149             );
150
151             $input->setOption('time-range', array_search($timeRange, $timeRanges));
152         }
153
154         // Language module is enabled or not.
155         $languageModuleEnabled = \Drupal::moduleHandler()
156             ->moduleExists('language');
157
158         // If language module is enabled.
159         if ($languageModuleEnabled) {
160             // Get available languages on site.
161             $languages = \Drupal::languageManager()->getLanguages();
162             // Holds the available languages.
163             $language_list = [];
164
165             foreach ($languages as $lang) {
166                 $language_list[$lang->getId()] = $lang->getName();
167             }
168
169             $language = $input->getOption('language');
170             // If no language option or invalid language code in option.
171             if (!$language || !array_key_exists($language, $language_list)) {
172                 $language = $this->getIo()->choice(
173                     $this->trans('commands.create.nodes.questions.language'),
174                     $language_list
175                 );
176             }
177             $input->setOption('language', $language);
178         } else {
179             // If 'language' module is not enabled.
180             $input->setOption('language', LanguageInterface::LANGCODE_NOT_SPECIFIED);
181         }
182     }
183
184     /**
185      * {@inheritdoc}
186      */
187     protected function execute(InputInterface $input, OutputInterface $output)
188     {
189         $contentTypes = $input->getArgument('content-types');
190         $limit = $input->getOption('limit')?:25;
191         $titleWords = $input->getOption('title-words')?:5;
192         $timeRange = $input->getOption('time-range')?:31536000;
193         $available_types = array_keys($this->drupalApi->getBundles());
194         $language = $input->getOption('language')?:'und';
195
196         foreach ($contentTypes as $type) {
197             if (!in_array($type, $available_types)) {
198                 throw new \Exception('Invalid content type name given.');
199             }
200         }
201
202         if (!$contentTypes) {
203             $contentTypes = $available_types;
204         }
205
206         $result = $this->createNodeData->create(
207             $contentTypes,
208             $limit,
209             $titleWords,
210             $timeRange,
211             $language
212         );
213
214         if ($result['success']) {
215             $tableHeader = [
216                 $this->trans('commands.create.nodes.messages.node-id'),
217                 $this->trans('commands.create.nodes.messages.content-type'),
218                 $this->trans('commands.create.nodes.messages.title'),
219                 $this->trans('commands.create.nodes.messages.created'),
220             ];
221
222             $this->getIo()->table($tableHeader, $result['success']);
223
224             $this->getIo()->success(
225                 sprintf(
226                     $this->trans('commands.create.nodes.messages.created-nodes'),
227                     count($result['success'])
228                 )
229             );
230         }
231
232         if (isset($result['error'])) {
233             foreach ($result['error'] as $error) {
234                 $this->getIo()->error(
235                     sprintf(
236                         $this->trans('commands.create.nodes.messages.error'),
237                         $error
238                     )
239                 );
240             }
241         }
242
243         return 0;
244     }
245 }