Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drush / drush / src / Commands / generate / GenerateCommands.php
1 <?php
2
3 namespace Drush\Commands\generate;
4
5 use DrupalCodeGenerator\GeneratorDiscovery;
6 use DrupalCodeGenerator\Helper\Dumper;
7 use DrupalCodeGenerator\Helper\Renderer;
8 use DrupalCodeGenerator\TwigEnvironment;
9 use Drush\Commands\DrushCommands;
10 use Drush\Commands\generate\Helper\InputHandler;
11 use Drush\Commands\generate\Helper\OutputHandler;
12 use Drush\Commands\help\ListCommands;
13 use Drush\Drush;
14 use Drush\Drupal\DrushServiceModifier;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Input\StringInput;
17 use Symfony\Component\Filesystem\Filesystem;
18
19 /**
20  * Drush generate command.
21  */
22 class GenerateCommands extends DrushCommands
23 {
24
25     /**
26      * Generate boilerplate code for modules/plugins/services etc.
27      *
28      * Drush asks questions so that the generated code is as polished as possible. After
29      * generating, Drush lists the files that were created.
30      *
31      * @command generate
32      * @aliases gen
33      * @param string $generator A generator name. Omit to pick from available Generators.
34      * @option answers A JSON string containing pairs of question and answers.
35      * @option directory Absolute path to a base directory for file writing.
36      * @usage drush generate
37      *  Pick from available generators and then run it.
38      * @usage drush generate controller
39      *  Generate a controller class for your module.
40      * @usage drush generate drush-command-file
41      *  Generate a Drush commandfile for your module.
42      * @topics docs:generators
43      * @bootstrap max
44      */
45     public function generate($generator = '', $options = ['answers' => self::REQ, 'directory' => self::REQ])
46     {
47
48         // Disallow default Symfony console commands.
49         if ($generator == 'help' || $generator == 'list') {
50             $generator = null;
51         }
52
53         $application = $this->createApplication();
54         if (!$generator) {
55             $all = $application->all();
56             unset($all['help'], $all['list']);
57             $namespaced = ListCommands::categorize($all, '-');
58             $preamble = dt('Run `drush generate [command]` and answer a few questions in order to write starter code to your project.');
59             ListCommands::renderListCLI($application, $namespaced, $this->output(), $preamble);
60             return null;
61         } else {
62             // Symfony console cannot recognize the command by alias when
63             // multiple commands have the same prefix.
64             if ($generator == 'module') {
65                 $generator = 'module-standard';
66             }
67
68             // Create an isolated input.
69             $argv = [
70                 $generator,
71                 '--answers=' . escapeshellarg($options['answers']),
72                 '--directory=' . $options['directory']
73             ];
74             if ($options['ansi']) {
75                 $argv[] = '--ansi';
76             }
77             if ($options['no-ansi']) {
78                 $argv[] = '--no-ansi';
79             }
80             return $application->run(new StringInput(implode(' ', $argv)));
81         }
82     }
83
84     /**
85      * Creates Drush generate application.
86      *
87      * @return \Symfony\Component\Console\Application
88      *   Symfony console application.
89      */
90     protected function createApplication()
91     {
92         $application = new Application('Drush generate', Drush::getVersion());
93         $helperSet = $application->getHelperSet();
94
95         $override = null;
96         if (Drush::affirmative()) {
97             $override = true;
98         } elseif (Drush::negative()) {
99             $override = false;
100         }
101         $dumper = new Dumper(new Filesystem(), $override);
102         $helperSet->set($dumper);
103
104         $twig_loader = new \Twig_Loader_Filesystem();
105         $renderer = new Renderer(new TwigEnvironment($twig_loader));
106         $helperSet->set($renderer);
107
108         $helperSet->set(new InputHandler());
109         $helperSet->set(new OutputHandler());
110
111         // Discover generators.
112         $discovery = new GeneratorDiscovery(new Filesystem());
113
114         /**
115          * Discover generators.
116          */
117         $dcg_generators = $discovery->getGenerators([DCG_ROOT . '/src/Command/Drupal_8'], '\DrupalCodeGenerator\Command\Drupal_8');
118         $drush_generators = $discovery->getGenerators([__DIR__ . '/Generators'], '\Drush\Commands\generate\Generators');
119         $config_paths = $this->getConfig()->get('runtime.commandfile.paths', []);
120
121         $global_paths = [];
122
123         foreach ($config_paths as $path) {
124             $global_paths[] = $path . '/Generators';
125             $global_paths[] = $path . '/src/Generators';
126         }
127
128         $global_paths = array_filter($global_paths, 'file_exists');
129         $global_generators =  $discovery->getGenerators($global_paths, '\Drush\Generators');
130
131         $module_generators = [];
132
133         if (Drush::bootstrapManager()->hasBootstrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
134             $container = \Drupal::getContainer();
135             if ($container->has(DrushServiceModifier::DRUSH_GENERATOR_SERVICES)) {
136                 $module_generators = $container->get(DrushServiceModifier::DRUSH_GENERATOR_SERVICES)->getCommandList();
137             }
138         }
139
140         /** @var \Symfony\Component\Console\Command\Command[] $generators */
141         $generators = array_merge($dcg_generators, $drush_generators, $global_generators, $module_generators);
142
143         foreach ($generators as $generator) {
144             $sub_names = explode(':', $generator->getName());
145             if ($sub_names[0] == 'd8') {
146                 // Remove d8 namespace.
147                 array_shift($sub_names);
148             }
149             $new_name = implode('-', $sub_names);
150             $generator->setName($new_name);
151             // Remove alias if it is same as new name.
152             if ($aliases = $generator->getAliases()) {
153                 $generator->setAliases(array_diff($aliases, [$new_name]));
154             }
155         }
156
157         $application->addCommands($generators);
158
159         $application->setAutoExit(false);
160         return $application;
161     }
162 }