b8ae26022506b481f3a05a543440c97e3b151011
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Command / BaseGenerator.php
1 <?php
2
3 namespace DrupalCodeGenerator\Command;
4
5 use DrupalCodeGenerator\ApplicationFactory;
6 use DrupalCodeGenerator\Asset;
7 use DrupalCodeGenerator\Utils;
8 use Symfony\Component\Console\Command\Command;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Output\OutputInterface;
12
13 /**
14  * Base class for all generators.
15  */
16 abstract class BaseGenerator extends Command implements GeneratorInterface {
17
18   /**
19    * The command name.
20    *
21    * @var string
22    */
23   protected $name;
24
25   /**
26    * The command description.
27    *
28    * @var string
29    */
30   protected $description;
31
32   /**
33    * The command alias.
34    *
35    * @var string
36    */
37   protected $alias;
38
39   /**
40    * Command label.
41    *
42    * @var string
43    */
44   protected $label;
45
46   /**
47    * A path where templates are stored.
48    *
49    * @var string
50    */
51   protected $templatePath;
52
53   /**
54    * The working directory.
55    *
56    * @var string
57    */
58   protected $directory;
59
60   /**
61    * The destination.
62    *
63    * @var mixed
64    */
65   protected $destination = 'modules/%';
66
67   /**
68    * Files to create.
69    *
70    * The key of the each item in the array is the path to the file and
71    * the value is the generated content of it.
72    *
73    * @var array
74    *
75    * @deprecated Use self::$assets.
76    */
77   protected $files = [];
78
79   /**
80    * Assets to create.
81    *
82    * @var \DrupalCodeGenerator\Asset[]
83    */
84   protected $assets = [];
85
86   /**
87    * Twig template variables.
88    *
89    * @var array
90    */
91   protected $vars = [];
92
93   /**
94    * {@inheritdoc}
95    */
96   protected function configure() {
97     $this
98       ->setName($this->name)
99       ->setDescription($this->description)
100       ->addOption(
101         'directory',
102         '-d',
103         InputOption::VALUE_OPTIONAL,
104         'Working directory'
105       )
106       ->addOption(
107         'answers',
108         '-a',
109         InputOption::VALUE_OPTIONAL,
110         'Default JSON formatted answers'
111       );
112
113     if ($this->alias) {
114       $this->setAliases([$this->alias]);
115     }
116
117     if (!$this->templatePath) {
118       $this->templatePath = ApplicationFactory::getRoot() . '/templates';
119     }
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   protected function initialize(InputInterface $input, OutputInterface $output) {
126     $this->getHelperSet()->setCommand($this);
127     $this->getHelper('dcg_renderer')->addPath($this->templatePath);
128
129     $directory = $input->getOption('directory') ?: getcwd();
130     // No need to look up for extension root when generating an extension.
131     $extension_destinations = ['modules', 'profiles', 'themes'];
132     $is_extension = in_array($this->destination, $extension_destinations);
133     $this->directory = $is_extension
134       ? $directory : (Utils::getExtensionRoot($directory) ?: $directory);
135
136     // Display welcome message.
137     $header = sprintf(
138       "\n Welcome to %s generator!",
139       $this->getName()
140     );
141     $output->writeln($header);
142     $header_length = strlen(trim(strip_tags($header)));
143     $output->writeln('<fg=cyan;options=bold>–' . str_repeat('–', $header_length) . '–</>');
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   protected function execute(InputInterface $input, OutputInterface $output) {
150
151     // Render all assets.
152     $renderer = $this->getHelper('dcg_renderer');
153     foreach ($this->getAssets() as $asset) {
154       // Supply the asset with all collected variables if it has no local ones.
155       if (!$asset->getVars()) {
156         $asset->vars($this->vars);
157       }
158       $asset->render($renderer);
159     }
160
161     $dumped_files = $this->getHelper('dcg_dumper')->dump($input, $output);
162     $this->getHelper('dcg_output_handler')->printSummary($output, $dumped_files);
163     return 0;
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function getLabel() {
170     return $this->label;
171   }
172
173   /**
174    * Returns list of rendered files.
175    *
176    * @return array
177    *   An associative array where each key is path to a file and value is
178    *   rendered content.
179    *
180    * @deprecated.
181    */
182   public function getFiles() {
183     return $this->files;
184   }
185
186   /**
187    * {@inheritdoc}
188    */
189   public function getAssets() {
190     if ($this->files) {
191       // Convert files into assets for legacy commands.
192       $assets = [];
193       foreach ($this->getFiles() as $path => $file) {
194         $asset = new Asset();
195         $asset->path($path);
196         if (!is_array($file)) {
197           $file = ['content' => $file];
198         }
199         if (isset($file['content'])) {
200           $asset->content($file['content']);
201         }
202         else {
203           $asset->type('directory');
204         }
205         if (isset($file['action'])) {
206           $asset->action($file['action']);
207         }
208         if (isset($file['header_size'])) {
209           $asset->headerSize($file['header_size']);
210         }
211         if (isset($file['mode'])) {
212           $asset->mode($file['mode']);
213         }
214         $assets[] = $asset;
215       }
216       return array_merge($assets, $this->assets);
217     }
218
219     return $this->assets;
220   }
221
222   /**
223    * {@inheritdoc}
224    */
225   public function setDirectory($directory) {
226     $this->directory = $directory;
227   }
228
229   /**
230    * {@inheritdoc}
231    */
232   public function getDirectory() {
233     return $this->directory;
234   }
235
236   /**
237    * {@inheritdoc}
238    */
239   public function setDestination($destination) {
240     $this->destination = $destination;
241   }
242
243   /**
244    * {@inheritdoc}
245    */
246   public function getDestination() {
247     return $this->destination;
248   }
249
250   /**
251    * Renders a template.
252    *
253    * @param string $template
254    *   Twig template.
255    * @param array $vars
256    *   Template variables.
257    *
258    * @return string
259    *   A string representing the rendered output.
260    */
261   protected function render($template, array $vars) {
262     return $this->getHelper('dcg_renderer')->render($template, $vars);
263   }
264
265   /**
266    * Asks the user for template variables.
267    *
268    * @param \Symfony\Component\Console\Input\InputInterface $input
269    *   Input instance.
270    * @param \Symfony\Component\Console\Output\OutputInterface $output
271    *   Output instance.
272    * @param array $questions
273    *   List of questions that the user should answer.
274    * @param array $vars
275    *   Array of predefined template variables.
276    *
277    * @return array
278    *   Template variables.
279    *
280    * @see \DrupalCodeGenerator\InputHandler
281    */
282   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     return $this->vars;
285   }
286
287   /**
288    * Creates an asset.
289    *
290    * @param string $type
291    *   Asset type.
292    *
293    * @return \DrupalCodeGenerator\Asset
294    *   The asset.
295    */
296   protected function addAsset($type) {
297     $asset = (new Asset())->type($type);
298     $this->assets[] = $asset;
299     return $asset;
300   }
301
302   /**
303    * Creates file asset.
304    *
305    * @return \DrupalCodeGenerator\Asset
306    *   The asset.
307    */
308   protected function addFile() {
309     return $this->addAsset('file');
310   }
311
312   /**
313    * Creates directory asset.
314    *
315    * @return \DrupalCodeGenerator\Asset
316    *   The asset.
317    */
318   protected function addDirectory() {
319     return $this->addAsset('directory');
320   }
321
322   /**
323    * Creates service file asset.
324    *
325    * @return \DrupalCodeGenerator\Asset
326    *   The asset.
327    */
328   protected function addServicesFile() {
329     return $this->addFile()
330       ->path('{machine_name}.services.yml')
331       ->action('append')
332       ->headerSize(1);
333   }
334
335   /**
336    * Creates file asset.
337    *
338    * @param string $path
339    *   Path to the file.
340    * @param string $template
341    *   Twig template to render.
342    * @param array $vars
343    *   Twig variables.
344    *
345    * @deprecated Use self::addFile() or self::addDirectory().
346    */
347   protected function setFile($path = NULL, $template = NULL, array $vars = []) {
348     $this->addFile()
349       ->path($path)
350       ->template($template)
351       ->vars($vars);
352   }
353
354   /**
355    * Creates service file asset.
356    *
357    * @param string $path
358    *   Path to the file.
359    * @param string $template
360    *   Twig template to render.
361    * @param array $vars
362    *   Twig variables.
363    *
364    * @deprecated Use self::addServiceFile().
365    */
366   protected function setServicesFile($path, $template, array $vars) {
367     $this->addServicesFile()
368       ->path($path)
369       ->template($template)
370       ->vars($vars);
371   }
372
373 }