comparison vendor/chi-teck/drupal-code-generator/src/Command/Drupal_7/Hook.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace DrupalCodeGenerator\Command\Drupal_7;
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\Question;
10
11 /**
12 * Implements d7:hook command.
13 */
14 class Hook extends BaseGenerator {
15
16 protected $name = 'd7:hook';
17 protected $description = 'Generates a hook';
18
19 /**
20 * {@inheritdoc}
21 */
22 protected function interact(InputInterface $input, OutputInterface $output) {
23 $questions = Utils::defaultQuestions();
24 $questions['hook_name'] = new Question('Hook name');
25 $questions['hook_name']->setValidator(function ($value) {
26 if (!in_array($value, $this->getSupportedHooks())) {
27 throw new \UnexpectedValueException('The value is not correct class name.');
28 }
29 return $value;
30 });
31 $questions['hook_name']->setAutocompleterValues($this->getSupportedHooks());
32
33 $vars = $this->collectVars($input, $output, $questions);
34
35 // Most Drupal hooks are situated in a module file but some are not.
36 $special_hooks = [
37 'install' => [
38 'install',
39 'uninstall',
40 'enable',
41 'disable',
42 'schema',
43 'schema_alter',
44 'field_schema',
45 'requirements',
46 'update_N',
47 'update_last_removed',
48 ],
49 // See system_hook_info().
50 'tokens.inc' => [
51 'token_info',
52 'token_info_alter',
53 'tokens',
54 'tokens_alter',
55 ],
56 ];
57
58 $file_type = 'module';
59 foreach ($special_hooks as $group => $hooks) {
60 if (in_array($vars['hook_name'], $hooks)) {
61 $file_type = $group;
62 break;
63 }
64 }
65
66 $this->addFile()
67 ->path("{machine_name}.$file_type")
68 ->headerTemplate("d7/file-docs/$file_type.twig")
69 ->template('d7/hook/' . $vars['hook_name'] . '.twig')
70 ->action('append')
71 ->headerSize(7);
72 }
73
74 /**
75 * Gets list of supported hooks.
76 *
77 * @return array
78 * List of supported hooks.
79 */
80 protected function getSupportedHooks() {
81 return array_map(function ($file) {
82 return pathinfo($file, PATHINFO_FILENAME);
83 }, array_diff(scandir($this->templatePath . '/d7/hook'), ['.', '..']));
84 }
85
86 }