Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / Drupal_8 / PluginManager.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_8;
4
5 use DrupalCodeGenerator\Command\BaseGenerator;
6 use DrupalCodeGenerator\Utils;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Output\OutputInterface;
9 use Symfony\Component\Console\Question\ChoiceQuestion;
10 use Symfony\Component\Console\Question\Question;
11
12 /**
13  * Implements d8:plugin-manager command.
14  */
15 class PluginManager extends BaseGenerator {
16
17   protected $name = 'd8:plugin-manager';
18   protected $description = 'Generates plugin manager';
19   protected $alias = 'plugin-manager';
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function interact(InputInterface $input, OutputInterface $output) {
25     $questions = Utils::defaultQuestions();
26     $default_plugin_type = function ($vars) {
27       return $vars['machine_name'];
28     };
29     $questions['plugin_type'] = new Question('Plugin type', $default_plugin_type);
30
31     // Utils::validateMachineName does not allow dots. But they can appear in
32     // plugin types (field.widget, views.argument, etc).
33     $questions['plugin_type']->setValidator(function ($value) {
34       if (!preg_match('/^[a-z][a-z0-9_\.]*[a-z0-9]$/', $value)) {
35         throw new \UnexpectedValueException('The value is not correct machine name.');
36       }
37       return $value;
38     });
39
40     $discovery_types = [
41       'annotation' => 'Annotation',
42       'yaml' => 'YAML',
43       'hook' => 'Hook',
44     ];
45     $choices = Utils::prepareChoices($discovery_types);
46     $questions['discovery'] = new ChoiceQuestion('Discovery type', $choices, 'Annotation');
47
48     $vars = &$this->collectVars($input, $output, $questions);
49
50     $vars['class_prefix'] = Utils::camelize($vars['plugin_type']);
51     $vars['discovery'] = array_search($vars['discovery'], $discovery_types);
52
53     $common_files = [
54       'model.services.yml',
55       'src/ExampleInterface.php',
56       'src/ExamplePluginManager.php',
57     ];
58
59     $files = [];
60     switch ($vars['discovery']) {
61       case 'annotation':
62         $files = [
63           'src/Annotation/Example.php',
64           'src/ExamplePluginBase.php',
65           'src/Plugin/Example/Foo.php',
66         ];
67         break;
68
69       case 'yaml':
70         $files = [
71           'model.examples.yml',
72           'src/ExampleDefault.php',
73         ];
74         break;
75
76       case 'hook':
77         $files = [
78           'model.module',
79           'src/ExampleDefault.php',
80         ];
81         break;
82     }
83
84     $files = array_merge($common_files, $files);
85
86     $templates_path = 'd8/plugin-manager/' . $vars['discovery'] . '/';
87
88     $path_placeholders = ['model', 'Example', 'examples'];
89     $path_replacements = [
90       $vars['machine_name'],
91       $vars['class_prefix'],
92       Utils::pluralize($vars['plugin_type']),
93     ];
94
95     foreach ($files as $file) {
96       $asset = $this->addFile()
97         ->path(str_replace($path_placeholders, $path_replacements, $file))
98         ->template($templates_path . $file . '.twig');
99       if ($file === 'model.services.yml') {
100         $asset->action('append')->headerSize(1);
101       }
102       elseif ($file == 'model.module') {
103         $asset
104           ->action('append')
105           ->headerTemplate('d8/file-docs/module.twig')
106           ->headerSize(7);
107       }
108     }
109   }
110
111 }