GeneratorFactory.php
1.96 KB
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
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use DrupalCodeGenerator\Command\BaseGenerator;
/**
* Defines generator factory.
*
* This factory only supports DCG core generators.
*/
final class GeneratorFactory {
private const string DIRECTORY = Application::ROOT . '/src/Command';
private const string NAMESPACE = '\DrupalCodeGenerator\Command';
/**
* Constructs the object.
*/
public function __construct(
private readonly ClassResolverInterface $classResolver,
) {}
/**
* Finds and instantiates DCG core generators.
*
* @psalm-return list<\DrupalCodeGenerator\Command\BaseGenerator>
* Array of generators.
*/
public function getGenerators(): array {
$commands = [];
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(self::DIRECTORY, \FilesystemIterator::SKIP_DOTS),
);
foreach ($iterator as $file) {
if ($file->getExtension() !== 'php') {
continue;
}
/** @var \RecursiveDirectoryIterator $directory_iterator */
$directory_iterator = $iterator->getInnerIterator();
$sub_path = $directory_iterator->getSubPath();
$sub_namespace = $sub_path ? \str_replace(\DIRECTORY_SEPARATOR, '\\', $sub_path) . '\\' : '';
/** @psalm-var class-string $class */
$class = self::NAMESPACE . '\\' . $sub_namespace . $file->getBasename('.php');
$reflected_class = new \ReflectionClass($class);
// @todo Is it needed?
if ($reflected_class->isInterface() || $reflected_class->isAbstract() || $reflected_class->isTrait()) {
continue;
}
if (!$reflected_class->isSubclassOf(BaseGenerator::class)) {
continue;
}
$commands[] = $this->classResolver->getInstanceFromDefinition($class);
}
/** @psalm-suppress LessSpecificReturnStatement */
/** @psalm-var list<\DrupalCodeGenerator\Command\BaseGenerator> */
return $commands;
}
}