-
Notifications
You must be signed in to change notification settings - Fork 46
/
BaseGenerator.php
299 lines (258 loc) · 9.61 KB
/
BaseGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Command;
use DrupalCodeGenerator\Asset\AssetCollection;
use DrupalCodeGenerator\Attribute\Generator as GeneratorDefinition;
use DrupalCodeGenerator\Event\AssetPostProcess;
use DrupalCodeGenerator\Event\AssetPreProcess;
use DrupalCodeGenerator\Exception\ExceptionInterface;
use DrupalCodeGenerator\Exception\SilentException;
use DrupalCodeGenerator\GeneratorType;
use DrupalCodeGenerator\Helper\Drupal\NullExtensionInfo;
use DrupalCodeGenerator\InputOutput\DefaultOptions;
use DrupalCodeGenerator\InputOutput\Interviewer;
use DrupalCodeGenerator\InputOutput\IO;
use DrupalCodeGenerator\InputOutput\IOAwareInterface;
use DrupalCodeGenerator\InputOutput\IOAwareTrait;
use DrupalCodeGenerator\Logger\ConsoleLogger;
use DrupalCodeGenerator\Utils;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Base class for code generators.
*
* @method \DrupalCodeGenerator\Application getApplication()
* @method string getName()
* @method \Symfony\Component\Console\Helper\HelperSet getHelperSet()
*/
abstract class BaseGenerator extends Command implements LabelInterface, IOAwareInterface, LoggerAwareInterface {
use IOAwareTrait;
use LoggerAwareTrait;
/**
* {@inheritdoc}
*/
public function __construct() {
parent::__construct();
$this->logger = new NullLogger();
}
/**
* {@inheritdoc}
*/
protected function configure(): void {
parent::configure();
$definition = $this->getGeneratorDefinition();
$this->setName($definition->name)
->setDescription($definition->description)
->setAliases($definition->aliases)
->setHidden($definition->hidden);
DefaultOptions::apply($this);
}
/**
* {@inheritdoc}
*
* @psalm-suppress PossiblyNullReference
*/
protected function initialize(InputInterface $input, OutputInterface $output): void {
parent::initialize($input, $output);
$logger = new ConsoleLogger($output);
$question_helper = $this->getHelper('question');
$io = new IO($input, $output, $question_helper);
$items = \iterator_to_array($this->getHelperSet());
$items[] = $this;
foreach ($items as $item) {
if ($item instanceof IOAwareInterface) {
$item->io($io);
}
if ($item instanceof LoggerAwareInterface) {
$item->setLogger($logger);
}
}
$template_path = $this->getTemplatePath();
if ($template_path !== NULL) {
$this->getHelper('renderer')->registerTemplatePath($template_path);
}
$this->logger->debug('PHP binary: {binary}', ['binary' => \PHP_BINARY]);
/** @psalm-var array{PHP_SELF: string} $_SERVER */
// @phpcs:ignore SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
$this->logger->debug('DCG executable: {dcg}', ['dcg' => \realpath($_SERVER['PHP_SELF'])]);
$this->logger->debug('Working directory: {directory}', ['directory' => $io->getWorkingDirectory()]);
}
/**
* {@inheritdoc}
*
* @noinspection PhpMissingParentCallCommonInspection
* @psalm-suppress PossiblyNullReference
*
* @psalm-return int<0, 1>
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->logger->debug('Command: {command}', ['command' => static::class]);
try {
$this->printHeader();
$vars = [];
$assets = new AssetCollection();
$this->generate($vars, $assets);
$this->generateInfoFile($vars, $assets);
$vars = Utils::processVars($vars);
$collected_vars = \preg_replace('/^Array/', '', \print_r($vars, TRUE));
$this->logger->debug('Collected variables: {vars}', ['vars' => $collected_vars]);
foreach ($assets as $asset) {
// Local asset variables take precedence over global ones.
$asset->vars(\array_merge($vars, Utils::processVars($asset->getVars())));
}
$this->render($assets);
// Destination passed through command line option takes precedence over
// destination defined in a generator.
$destination = $input->getOption('destination') ?: $this->getDestination($vars);
$this->logger->debug('Destination directory: {directory}', ['directory' => $destination]);
$dumped_assets = $this->dump($assets, $destination);
$full_path = $input->getOption('full-path');
$this->printSummary($dumped_assets, $full_path ? $destination . '/' : '');
}
catch (ExceptionInterface $exception) {
if (!$exception instanceof SilentException) {
$this->io()->getErrorStyle()->error($exception->getMessage());
}
return self::FAILURE;
}
$this->logger->debug('Memory usage: {memory}', ['memory' => Helper::formatMemory(\memory_get_peak_usage())]);
return self::SUCCESS;
}
/**
* Generates assets.
*/
abstract protected function generate(array &$vars, AssetCollection $assets): void;
/**
* Gets generator definition.
*/
protected function getGeneratorDefinition(): GeneratorDefinition {
$attributes = (new \ReflectionClass(static::class))->getAttributes(GeneratorDefinition::class);
if (\count($attributes) === 0) {
throw new \LogicException(\sprintf('Command %s does not have generator annotation.', static::class));
}
return $attributes[0]->newInstance();
}
/**
* Creates interviewer.
*/
protected function createInterviewer(array &$vars): Interviewer {
$extension_info = match ($this->getGeneratorDefinition()->type) {
GeneratorType::MODULE, GeneratorType::MODULE_COMPONENT => $this->getHelper('module_info'),
GeneratorType::THEME, GeneratorType::THEME_COMPONENT => $this->getHelper('theme_info'),
default => new NullExtensionInfo(),
};
return new Interviewer(
io: $this->io,
vars: $vars,
generatorDefinition: $this->getGeneratorDefinition(),
serviceInfo: $this->getHelper('service_info'),
extensionInfo: $extension_info,
permissionInfo: $this->getHelper('permission_info'),
);
}
/**
* Render assets.
*/
protected function render(AssetCollection $assets): void {
$renderer = $this->getHelper('renderer');
foreach ($assets->getFiles() as $file) {
/** @var \DrupalCodeGenerator\Asset\File $file */
$renderer->renderAsset($file);
}
}
/**
* Dumps assets.
*/
protected function dump(AssetCollection $assets, string $destination): AssetCollection {
$is_dry = $this->io()->getInput()->getOption('dry-run');
$pre_process_event = $this->getApplication()->dispatch(
new AssetPreProcess($assets, $destination, $this->getName(), $is_dry),
);
$assets = $pre_process_event->assets;
$destination = $pre_process_event->destination;
/** @var \DrupalCodeGenerator\Helper\Dumper\DumperInterface $dumper */
$dumper = $this->getHelper($is_dry ? 'dry_dumper' : 'filesystem_dumper');
$dumped_assets = $dumper->dump($assets, $destination);
$post_process_event = $this->getApplication()->dispatch(
new AssetPostProcess($dumped_assets, $destination, $this->getName(), $is_dry),
);
return $post_process_event->assets;
}
/**
* Prints header.
*/
protected function printHeader(): void {
$this->io()->title(\sprintf('Welcome to %s generator!', $this->getAliases()[0] ?? $this->getName()));
}
/**
* Prints summary.
*/
protected function printSummary(AssetCollection $dumped_assets, string $base_path): void {
$printer_name = $this->io()->isVerbose() ? 'assets_table_printer' : 'assets_list_printer';
/** @psalm-suppress UndefinedInterfaceMethod */
$this->getHelper($printer_name)->printAssets($dumped_assets, $base_path);
}
/**
* {@inheritdoc}
*/
public function getLabel(): ?string {
return $this->getGeneratorDefinition()->label;
}
/**
* Returns template path.
*/
final protected function getTemplatePath(): ?string {
return $this->getGeneratorDefinition()->templatePath;
}
/**
* Returns destination for generated files.
*
* @todo Test this.
*/
protected function getDestination(array $vars): string {
if (!isset($vars['machine_name'])) {
return $this->io()->getWorkingDirectory();
}
$definition = $this->getGeneratorDefinition();
$is_new = $definition->type->isNewExtension();
return match ($definition->type) {
GeneratorType::MODULE, GeneratorType::MODULE_COMPONENT =>
$this->getHelper('module_info')->getDestination($vars['machine_name'], $is_new),
GeneratorType::THEME, GeneratorType::THEME_COMPONENT =>
$this->getHelper('theme_info')->getDestination($vars['machine_name'], $is_new),
default => $this->io()->getWorkingDirectory(),
};
}
/**
* Generates info file.
*
* @todo Test this.
* @todo Generate info file for theme components.
*/
protected function generateInfoFile(array &$vars, AssetCollection $assets): void {
if (\count($assets) === 0) {
return;
}
if ($this->getGeneratorDefinition()->type !== GeneratorType::MODULE_COMPONENT) {
return;
}
// @todo Throw an exception if machine name was not provided.
/** @psalm-suppress PossiblyUndefinedStringArrayOffset */
$vars['name'] ??= Utils::machine2human($vars['machine_name']);
$info_template = <<< 'TWIG'
name: '{{ name }}'
type: module
description: '@todo Add description.'
package: '@todo Add package'
core_version_requirement: ^10
TWIG;
$assets->addFile('{machine_name}.info.yml')
->inlineTemplate($info_template)
->preserveIfExists();
}
}