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