Mercurial > hg > cmmr2012-drupal-site
comparison vendor/chi-teck/drupal-code-generator/src/Command/BaseGenerator.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children | 12f9dff5fda9 |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
7 use DrupalCodeGenerator\Utils; | 7 use DrupalCodeGenerator\Utils; |
8 use Symfony\Component\Console\Command\Command; | 8 use Symfony\Component\Console\Command\Command; |
9 use Symfony\Component\Console\Input\InputInterface; | 9 use Symfony\Component\Console\Input\InputInterface; |
10 use Symfony\Component\Console\Input\InputOption; | 10 use Symfony\Component\Console\Input\InputOption; |
11 use Symfony\Component\Console\Output\OutputInterface; | 11 use Symfony\Component\Console\Output\OutputInterface; |
12 use Symfony\Component\Console\Question\Question; | |
12 | 13 |
13 /** | 14 /** |
14 * Base class for all generators. | 15 * Base class for all generators. |
15 */ | 16 */ |
16 abstract class BaseGenerator extends Command implements GeneratorInterface { | 17 abstract class BaseGenerator extends Command implements GeneratorInterface { |
275 * Array of predefined template variables. | 276 * Array of predefined template variables. |
276 * | 277 * |
277 * @return array | 278 * @return array |
278 * Template variables. | 279 * Template variables. |
279 * | 280 * |
280 * @see \DrupalCodeGenerator\InputHandler | 281 * @see \DrupalCodeGenerator\InputHandler::collectVars() |
281 */ | 282 */ |
282 protected function &collectVars(InputInterface $input, OutputInterface $output, array $questions, array $vars = []) { | 283 protected function &collectVars(InputInterface $input, OutputInterface $output, array $questions, array $vars = []) { |
283 $this->vars += $this->getHelper('dcg_input_handler')->collectVars($input, $output, $questions, $vars); | 284 $this->vars += $this->getHelper('dcg_input_handler')->collectVars($input, $output, $questions, $vars); |
284 return $this->vars; | 285 return $this->vars; |
286 } | |
287 | |
288 /** | |
289 * Asks the user a single question and returns the answer. | |
290 * | |
291 * @param \Symfony\Component\Console\Input\InputInterface $input | |
292 * Input instance. | |
293 * @param \Symfony\Component\Console\Output\OutputInterface $output | |
294 * Output instance. | |
295 * @param \Symfony\Component\Console\Question\Question $question | |
296 * A question to ask. | |
297 * @param array $vars | |
298 * Array of predefined template variables. | |
299 * | |
300 * @return string | |
301 * The answer. | |
302 * | |
303 * @see \DrupalCodeGenerator\InputHandler::collectVars() | |
304 */ | |
305 protected function ask(InputInterface $input, OutputInterface $output, Question $question, array $vars = []) { | |
306 $answers = $this->getHelper('dcg_input_handler')->collectVars($input, $output, ['key' => $question], $vars); | |
307 return $answers['key']; | |
285 } | 308 } |
286 | 309 |
287 /** | 310 /** |
288 * Creates an asset. | 311 * Creates an asset. |
289 * | 312 * |
368 ->path($path) | 391 ->path($path) |
369 ->template($template) | 392 ->template($template) |
370 ->vars($vars); | 393 ->vars($vars); |
371 } | 394 } |
372 | 395 |
396 /** | |
397 * Collects services. | |
398 * | |
399 * @param \Symfony\Component\Console\Input\InputInterface $input | |
400 * Input instance. | |
401 * @param \Symfony\Component\Console\Output\OutputInterface $output | |
402 * Output instance. | |
403 * | |
404 * @return array | |
405 * List of collected services. | |
406 */ | |
407 protected function collectServices(InputInterface $input, OutputInterface $output) { | |
408 | |
409 $service_definitions = self::getServiceDefinitions(); | |
410 $service_ids = array_keys($service_definitions); | |
411 | |
412 $services = []; | |
413 while (TRUE) { | |
414 $question = new Question('Type the service name or use arrows up/down. Press enter to continue'); | |
415 $question->setValidator([Utils::class, 'validateServiceName']); | |
416 $question->setAutocompleterValues($service_ids); | |
417 $service = $this->ask($input, $output, $question); | |
418 if (!$service) { | |
419 break; | |
420 } | |
421 $services[] = $service; | |
422 } | |
423 | |
424 $this->vars['services'] = []; | |
425 foreach (array_unique($services) as $service_id) { | |
426 if (isset($service_definitions[$service_id])) { | |
427 $definition = $service_definitions[$service_id]; | |
428 } | |
429 else { | |
430 // Build the definition if the service is unknown. | |
431 $definition = [ | |
432 'type' => 'Drupal\example\ExampleInterface', | |
433 'name' => str_replace('.', '_', $service_id), | |
434 'description' => "The $service_id service.", | |
435 ]; | |
436 } | |
437 $type_parts = explode('\\', $definition['type']); | |
438 $definition['short_type'] = end($type_parts); | |
439 $this->vars['services'][$service_id] = $definition; | |
440 } | |
441 return $this->vars['services']; | |
442 } | |
443 | |
444 /** | |
445 * Gets service definitions. | |
446 * | |
447 * @return array | |
448 * List of service definitions keyed by service ID. | |
449 */ | |
450 protected static function getServiceDefinitions() { | |
451 $data_encoded = file_get_contents(ApplicationFactory::getRoot() . '/resources/service-definitions.json'); | |
452 return json_decode($data_encoded, TRUE); | |
453 } | |
454 | |
373 } | 455 } |