3 namespace DrupalCodeGenerator\Command\Drupal_8;
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;
13 * Implements d8:plugin-manager command.
15 class PluginManager extends BaseGenerator {
17 protected $name = 'd8:plugin-manager';
18 protected $description = 'Generates plugin manager';
19 protected $alias = 'plugin-manager';
24 protected function interact(InputInterface $input, OutputInterface $output) {
25 $questions = Utils::defaultQuestions();
26 $default_plugin_type = function ($vars) {
27 return $vars['machine_name'];
29 $questions['plugin_type'] = new Question('Plugin type', $default_plugin_type);
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.');
41 'annotation' => 'Annotation',
45 $choices = Utils::prepareChoices($discovery_types);
46 $questions['discovery'] = new ChoiceQuestion('Discovery type', $choices, 'Annotation');
48 $vars = &$this->collectVars($input, $output, $questions);
50 $vars['class_prefix'] = Utils::camelize($vars['plugin_type']);
51 $vars['discovery'] = array_search($vars['discovery'], $discovery_types);
55 'src/ExampleInterface.php',
56 'src/ExamplePluginManager.php',
60 switch ($vars['discovery']) {
63 'src/Annotation/Example.php',
64 'src/ExamplePluginBase.php',
65 'src/Plugin/Example/Foo.php',
72 'src/ExampleDefault.php',
79 'src/ExampleDefault.php',
84 $files = array_merge($common_files, $files);
86 $templates_path = 'd8/plugin-manager/' . $vars['discovery'] . '/';
88 $path_placeholders = ['model', 'Example', 'examples'];
89 $path_replacements = [
90 $vars['machine_name'],
91 $vars['class_prefix'],
92 Utils::pluralize($vars['plugin_type']),
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);
102 elseif ($file == 'model.module') {
105 ->headerTemplate('d8/file-docs/module.twig')