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