-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
SearchExtension.php
153 lines (127 loc) 路 4.41 KB
/
SearchExtension.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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\DI\Extensions;
use Nette;
use Nette\Loaders\RobotLoader;
use Nette\Schema\Expect;
use Nette\Utils\Arrays;
/**
* Services auto-discovery.
*/
final class SearchExtension extends Nette\DI\CompilerExtension
{
private array $classes = [];
private string $tempDir;
public function __construct(string $tempDir)
{
$this->tempDir = $tempDir;
}
public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::arrayOf(
Expect::structure([
'in' => Expect::string()->required(),
'files' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
'classes' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
'extends' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
'implements' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
'exclude' => Expect::structure([
'classes' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
'extends' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
'implements' => Expect::anyOf(Expect::listOf('string'), Expect::string()->castTo('array'))->default([]),
]),
'tags' => Expect::array(),
]),
)->before(fn($val) => is_string($val['in'] ?? null)
? ['default' => $val]
: $val);
}
public function loadConfiguration()
{
foreach (array_filter($this->config) as $name => $batch) {
if (!is_dir($batch->in)) {
throw new Nette\DI\InvalidConfigurationException(sprintf(
"Option '%s\u{a0}鈥篭u{a0}%s\u{a0}鈥篭u{a0}in' must be valid directory name, '%s' given.",
$this->name,
$name,
$batch->in,
));
}
foreach ($this->findClasses($batch) as $class) {
$this->classes[$class] = array_merge($this->classes[$class] ?? [], $batch->tags);
}
}
}
public function findClasses(\stdClass $config): array
{
$robot = new RobotLoader;
$robot->setTempDirectory($this->tempDir);
$robot->addDirectory($config->in);
$robot->acceptFiles = $config->files ?: ['*.php'];
$robot->reportParseErrors(false);
$robot->refresh();
$classes = array_unique(array_keys($robot->getIndexedClasses()));
$exclude = $config->exclude;
$acceptRE = self::buildNameRegexp($config->classes);
$rejectRE = self::buildNameRegexp($exclude->classes);
$acceptParent = array_merge($config->extends, $config->implements);
$rejectParent = array_merge($exclude->extends, $exclude->implements);
$found = [];
foreach ($classes as $class) {
if (!class_exists($class) && !interface_exists($class) && !trait_exists($class)) {
throw new Nette\InvalidStateException(sprintf(
'Class %s was found, but it cannot be loaded by autoloading.',
$class,
));
}
$rc = new \ReflectionClass($class);
if (
($rc->isInstantiable()
||
($rc->isInterface()
&& count($methods = $rc->getMethods()) === 1
&& $methods[0]->name === 'create')
)
&& (!$acceptRE || preg_match($acceptRE, $rc->name))
&& (!$rejectRE || !preg_match($rejectRE, $rc->name))
&& (!$acceptParent || Arrays::some($acceptParent, fn($nm) => $rc->isSubclassOf($nm)))
&& (!$rejectParent || Arrays::every($rejectParent, fn($nm) => !$rc->isSubclassOf($nm)))
) {
$found[] = $rc->name;
}
}
return $found;
}
public function beforeCompile()
{
$builder = $this->getContainerBuilder();
foreach ($this->classes as $class => $foo) {
if ($builder->findByType($class)) {
unset($this->classes[$class]);
}
}
foreach ($this->classes as $class => $tags) {
$def = class_exists($class)
? $builder->addDefinition(null)->setType($class)
: $builder->addFactoryDefinition(null)->setImplement($class);
$def->setTags(Arrays::normalize($tags, true));
}
}
private static function buildNameRegexp(array $masks): ?string
{
$res = [];
foreach ((array) $masks as $mask) {
$mask = (str_contains($mask, '\\') ? '' : '**\\') . $mask;
$mask = preg_quote($mask, '#');
$mask = str_replace('\*\*\\\\', '(.*\\\\)?', $mask);
$mask = str_replace('\\\\\*\*', '(\\\\.*)?', $mask);
$mask = str_replace('\*', '\w*', $mask);
$res[] = $mask;
}
return $res ? '#^(' . implode('|', $res) . ')$#i' : null;
}
}