Repository navigation
Expand file tree
/
Copy pathConditional.php
More file actions
59 lines (52 loc) · 2.09 KB
/
Copy pathConditional.php
File metadata and controls
59 lines (52 loc) · 2.09 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
<?php declare(strict_types = 1);
namespace PHPStan\DependencyInjection;
use Attribute;
use PHPStan\ShouldNotHappenException;
use function is_array;
/**
* Registers the class with the wrapped attribute, but adds the tags only when the condition is met:
* the rule tag for RegisteredRule, the collector tag for RegisteredCollector,
* and the tags of the implemented extension interfaces for AutowiredService.
* The service is always registered so that it can still be autowired.
*
* The condition has the same syntax as values in the conditionalTags config section:
* a parameter reference like '%featureToggles.foo%', its negation 'not(%featureToggles.foo%)',
* or a list of them that all must be met.
*
* Works thanks to https://github.com/ondrejmirtes/composer-attribute-collector
* and AutowiredAttributeServicesExtension.
*
* @api
*/
#[Attribute(flags: Attribute::TARGET_CLASS)]
final class Conditional
{
public AutowiredService|RegisteredCollector|RegisteredRule $attribute;
/**
* On PHP < 8.1, simple-downgrader writes `new` in attribute arguments as the array,
* and composer-attribute-collector turns it back into the object in vendor/attributes.php.
*
* @api
* @param AutowiredService|RegisteredCollector|RegisteredRule|array{
* 'olvlvl\ComposerAttributeCollector\NewExpression': array{
* class-string<AutowiredService>,
* array{0?: string|null, 1?: string|null, 2?: true|list<class-string>|class-string, 3?: bool}
* |array{name?: string|null, factory?: string|null, as?: true|list<class-string>|class-string, autoTag?: bool},
* }|array{
* class-string<RegisteredCollector>|class-string<RegisteredRule>,
* array{int}|array{level: int},
* },
* } $attribute
* @param string|non-empty-list<string> $if
*/
public function __construct(
AutowiredService|RegisteredCollector|RegisteredRule|array $attribute,
public string|array $if,
)
{
if (is_array($attribute)) {
throw new ShouldNotHappenException('The attribute should have been instantiated in vendor/attributes.php.');
}
$this->attribute = $attribute;
}
}