/
AheadOfTimeFactoryCreatorCommand.php
159 lines (135 loc) · 6.11 KB
/
AheadOfTimeFactoryCreatorCommand.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
<?php
declare(strict_types=1);
namespace Laminas\ServiceManager\Command;
use Brick\VarExporter\VarExporter;
use Laminas\ServiceManager\ConfigProvider;
use Laminas\ServiceManager\Exception\RuntimeException;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\ServiceManager\ServiceManager;
use Laminas\ServiceManager\Tool\AheadOfTimeFactoryCompiler\AheadOfTimeFactoryCompilerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function assert;
use function class_exists;
use function count;
use function dirname;
use function file_put_contents;
use function is_dir;
use function is_string;
use function is_writable;
use function mkdir;
use function preg_replace;
use function sprintf;
use function str_replace;
/**
* @internal CLI commands are not meant to be used in any upstream projects other than via `laminas-cli`.
*
* @psalm-import-type ServiceManagerConfiguration from ServiceManager
*/
final class AheadOfTimeFactoryCreatorCommand extends Command
{
public const NAME = 'servicemanager:generate-aot-factories';
public function __construct(
private readonly array $config,
private readonly string $factoryTargetPath,
private readonly AheadOfTimeFactoryCompilerInterface $factoryCompiler,
) {
parent::__construct(self::NAME);
}
protected function configure(): void
{
$this->setDescription(
'Creates factories which replace the runtime overhead for `ReflectionBasedAbstractFactory`.'
);
$this->addArgument(
'localConfigFilename',
InputArgument::OPTIONAL,
'Should be a path targeting a filename which will be created so that the config autoloading'
. ' will pick it up. Using a `.local.php` suffix should verify that the file is overriding existing'
. ' configuration.',
'config/autoload/generated-factories.local.php',
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->factoryTargetPath === '' || ! is_writable($this->factoryTargetPath)) {
$output->writeln(sprintf(
'<error>Please configure the `%s` configuration key in your projects config and ensure that the'
. ' directory is registered to the composer autoloader using `classmap` and writable by the executing'
. ' user. In case you are targeting a nonexistent directory, please create the appropriate directory'
. ' structure before executing this command.</error>',
ConfigProvider::CONFIGURATION_KEY_FACTORY_TARGET_PATH,
));
return self::FAILURE;
}
$localConfigFilename = $input->getArgument('localConfigFilename');
assert(is_string($localConfigFilename));
if (! is_writable(dirname($localConfigFilename))) {
$output->writeln(sprintf(
'<error>Provided `localConfigFilename` argument "%s" is not writable. In case you are targeting a'
. ' nonexistent directory, please create the appropriate directory structure before executing this'
. ' command.</error>',
$localConfigFilename,
));
return self::FAILURE;
}
$compiledFactories = $this->factoryCompiler->compile($this->config);
if ($compiledFactories === []) {
$output->writeln(
'<comment>There is no (more) service registered to use the `ReflectionBasedAbstractFactory`.</comment>'
);
return self::SUCCESS;
}
$containerConfigurations = [];
foreach ($compiledFactories as $factory) {
$targetDirectory = sprintf(
'%s/%s',
$this->factoryTargetPath,
preg_replace('/\W/', '', $factory->containerConfigurationKey)
);
/** @var class-string<FactoryInterface> $factoryClassName */
$factoryClassName = sprintf('%sFactory', $factory->fullyQualifiedClassName);
if (class_exists($factoryClassName)) {
$output->writeln(sprintf(
'<error>There is already an existing factory class registered for "%s": %s</error>',
$factory->fullyQualifiedClassName,
$factoryClassName,
));
return self::FAILURE;
}
if (! is_dir($targetDirectory)) {
if (! mkdir($targetDirectory, recursive: true) && ! is_dir($targetDirectory)) {
throw new RuntimeException(sprintf('Unable to create directory "%s".', $targetDirectory));
}
}
$factoryFileName = sprintf(
'%s/%s.php',
$targetDirectory,
str_replace('\\', '_', $factoryClassName)
);
file_put_contents($factoryFileName, $factory->generatedFactory);
if (! isset($containerConfigurations[$factory->containerConfigurationKey])) {
$containerConfigurations[$factory->containerConfigurationKey] = ['factories' => []];
}
$containerConfigurations[$factory->containerConfigurationKey]['factories'] += [
$factory->fullyQualifiedClassName => $factoryClassName,
];
}
file_put_contents($localConfigFilename, $this->createLocalAotContainerConfigContent($containerConfigurations));
$output->writeln(sprintf('<info>Successfully created %d factories.</info>', count($compiledFactories)));
return self::SUCCESS;
}
/**
* @param non-empty-array<non-empty-string,ServiceManagerConfiguration> $containerConfigurations
* @return non-empty-string
*/
private function createLocalAotContainerConfigContent(array $containerConfigurations): string
{
return sprintf('<?php %s', VarExporter::export(
$containerConfigurations,
VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES
));
}
}