|
1 | | -#!/usr/bin/env php |
2 | | -<?php declare(strict_types=1); |
3 | | - |
4 | | -/** |
5 | | - * @file |
6 | | - * Generates hook templates from API documentation. |
7 | | - * |
8 | | - * phpcs:ignoreFile Drupal.Commenting.FileComment.Missing |
9 | | - */ |
10 | | - |
11 | | -use Symfony\Component\Console\Application; |
12 | | -use Symfony\Component\Console\Exception\RuntimeException; |
13 | | -use Symfony\Component\Console\Input\InputArgument; |
14 | | -use Symfony\Component\Console\Input\InputInterface; |
15 | | -use Symfony\Component\Console\Output\OutputInterface; |
16 | | -use Symfony\Component\Filesystem\Filesystem; |
17 | | - |
18 | | -require __DIR__ . '/../vendor/autoload.php'; |
19 | | - |
20 | | -/** |
21 | | - * Dumps hooks. |
22 | | - */ |
23 | | -function dump_hooks(InputInterface $input, OutputInterface $output): int { |
24 | | - |
25 | | - $file_system = new Filesystem(); |
26 | | - |
27 | | - $input_directory = $input->getArgument('input_directory'); |
28 | | - if (!$file_system->exists($input_directory)) { |
29 | | - throw new RuntimeException('Input directory does not exist.'); |
30 | | - } |
31 | | - |
32 | | - $output_directory = $input->getArgument('output_directory'); |
33 | | - if (!$file_system->exists($output_directory)) { |
34 | | - throw new RuntimeException('Output directory does not exist.'); |
35 | | - } |
36 | | - |
37 | | - $iterator = new RecursiveIteratorIterator( |
38 | | - new RecursiveDirectoryIterator($input_directory, RecursiveDirectoryIterator::SKIP_DOTS), |
39 | | - ); |
40 | | - |
41 | | - $total = 0; |
42 | | - foreach ($iterator as $path => $file) { |
43 | | - $file_name = $file->getFileName(); |
44 | | - if (\str_ends_with($file_name, 'api.php')) { |
45 | | - $output->writeln("<comment>$file_name</comment>"); |
46 | | - $hooks = \parse_hooks($path); |
47 | | - foreach ($hooks as $hook_name => $hook) { |
48 | | - $output->writeln(' - ' . $hook_name); |
49 | | - \file_put_contents("$output_directory/$hook_name.twig", $hook); |
50 | | - $total++; |
51 | | - } |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - $output->writeln('-----------------'); |
56 | | - $output->writeln('Dumped hooks: ' . $total); |
57 | | - |
58 | | - return 0; |
59 | | -} |
60 | | - |
61 | | -/** |
62 | | - * Extracts hooks from a singe PHP file. |
63 | | - * |
64 | | - * @param string $file |
65 | | - * File to parse. |
66 | | - * |
67 | | - * @return array |
68 | | - * Array of parsed hooks keyed by hook name. |
69 | | - */ |
70 | | -function parse_hooks(string $file): array { |
71 | | - $code = file_get_contents($file); |
72 | | - |
73 | | - \preg_match_all("/function hook_(.*)\(.*\n\}\n/Us", $code, $matches); |
74 | | - |
75 | | - $results = []; |
76 | | - foreach ($matches[0] as $index => $hook) { |
77 | | - $hook_name = $matches[1][$index]; |
78 | | - $output = "/**\n * Implements hook_$hook_name().\n */\n"; |
79 | | - $output .= \str_replace('function hook_', 'function {{ machine_name }}_', $hook); |
80 | | - $results[$hook_name] = $output; |
81 | | - } |
82 | | - |
83 | | - return $results; |
84 | | -} |
85 | | - |
86 | | -(new Application('Hooks dumper')) |
87 | | - ->register('dump-hooks') |
88 | | - ->addArgument('input_directory', InputArgument::REQUIRED, 'Input directory') |
89 | | - ->addArgument('output_directory', InputArgument::REQUIRED, 'Output directory') |
90 | | - ->setCode('dump_hooks') |
91 | | - ->getApplication() |
92 | | - ->setDefaultCommand('dump-hooks', TRUE) |
93 | | - ->run(); |
0 commit comments