-
Notifications
You must be signed in to change notification settings - Fork 426
/
ConditionalTagsExtension.php
68 lines (61 loc) · 2.25 KB
/
ConditionalTagsExtension.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
<?php declare(strict_types = 1);
namespace PHPStan\DependencyInjection;
use Nette;
use Nette\DI\CompilerExtension;
use Nette\Schema\Expect;
use PHPStan\Analyser\TypeSpecifierFactory;
use PHPStan\Broker\BrokerFactory;
use PHPStan\Collectors\RegistryFactory as CollectorRegistryFactory;
use PHPStan\Parser\RichParser;
use PHPStan\PhpDoc\TypeNodeResolverExtension;
use PHPStan\Rules\LazyRegistry;
use PHPStan\ShouldNotHappenException;
use function array_reduce;
use function count;
use function is_array;
use function sprintf;
class ConditionalTagsExtension extends CompilerExtension
{
public function getConfigSchema(): Nette\Schema\Schema
{
$bool = Expect::anyOf(Expect::bool(), Expect::listOf(Expect::bool()));
return Expect::arrayOf(Expect::structure([
BrokerFactory::PROPERTIES_CLASS_REFLECTION_EXTENSION_TAG => $bool,
BrokerFactory::METHODS_CLASS_REFLECTION_EXTENSION_TAG => $bool,
BrokerFactory::DYNAMIC_METHOD_RETURN_TYPE_EXTENSION_TAG => $bool,
BrokerFactory::DYNAMIC_STATIC_METHOD_RETURN_TYPE_EXTENSION_TAG => $bool,
BrokerFactory::DYNAMIC_FUNCTION_RETURN_TYPE_EXTENSION_TAG => $bool,
BrokerFactory::OPERATOR_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
LazyRegistry::RULE_TAG => $bool,
TypeNodeResolverExtension::EXTENSION_TAG => $bool,
TypeSpecifierFactory::FUNCTION_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
TypeSpecifierFactory::METHOD_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
TypeSpecifierFactory::STATIC_METHOD_TYPE_SPECIFYING_EXTENSION_TAG => $bool,
RichParser::VISITOR_SERVICE_TAG => $bool,
CollectorRegistryFactory::COLLECTOR_TAG => $bool,
])->min(1));
}
public function beforeCompile(): void
{
/** @var mixed[] $config */
$config = $this->config;
$builder = $this->getContainerBuilder();
foreach ($config as $type => $tags) {
$services = $builder->findByType($type);
if (count($services) === 0) {
throw new ShouldNotHappenException(sprintf('No services of type "%s" found.', $type));
}
foreach ($services as $service) {
foreach ($tags as $tag => $parameter) {
if (is_array($parameter)) {
$parameter = array_reduce($parameter, static fn ($carry, $item) => $carry && (bool) $item, true);
}
if ((bool) $parameter) {
$service->addTag($tag);
continue;
}
}
}
}
}
}