|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace FRZB\Component\DependencyInjection\Compiler; |
|
17
|
|
|
|
|
18
|
|
|
use FRZB\Component\DependencyInjection\Attribute\AsService; |
|
19
|
|
|
use FRZB\Component\DependencyInjection\Helper\DefinitionHelper; |
|
20
|
|
|
use FRZB\Component\DependencyInjection\Helper\EnvironmentHelper; |
|
21
|
|
|
use FRZB\Component\DependencyInjection\Helper\TagHelper; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @internal |
|
27
|
|
|
* |
|
28
|
|
|
* Register #[AsService] attribute on definition that is autoconfigured |
|
29
|
|
|
* |
|
30
|
|
|
* @author Mykhailo Shtanko <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
final class RegisterAsServiceAttributePass extends AbstractRegisterAttributePass |
|
33
|
|
|
{ |
|
34
|
|
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct(AsService::class); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function register(ContainerBuilder $container, \ReflectionClass $reflectionClass, AsService $attribute): void |
|
40
|
|
|
{ |
|
41
|
|
|
if (!EnvironmentHelper::isPermittedEnvironment($container, $reflectionClass)) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$definition = ($container->hasDefinition($id = $attribute->id ?? $reflectionClass->getName())) |
|
46
|
|
|
? $container->getDefinition($id)->setClass($reflectionClass->getName()) |
|
47
|
|
|
: $container->setDefinition($id, new Definition())->setClass($reflectionClass->getName()); |
|
48
|
|
|
|
|
49
|
|
|
foreach (self::mapArguments($container, $definition, $reflectionClass, $attribute) as $method => $arguments) { |
|
50
|
|
|
$definition->{$method}($arguments); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function mapArguments( |
|
55
|
|
|
ContainerBuilder $container, |
|
56
|
|
|
Definition $definition, |
|
57
|
|
|
\ReflectionClass $reflectionClass, |
|
58
|
|
|
AsService $attribute |
|
59
|
|
|
): array { |
|
60
|
|
|
return [ |
|
61
|
|
|
'setFile' => $attribute->file ?? $definition->getFile(), |
|
62
|
|
|
'setShared' => $attribute->isShared ?? $definition->isShared(), |
|
63
|
|
|
'setPublic' => $attribute->isPublic ?? $definition->isPublic(), |
|
64
|
|
|
'setFactory' => $attribute->factory ?? $definition->getFactory(), |
|
65
|
|
|
'setAutowired' => $attribute->isAutowired ?? $definition->isAutowired(), |
|
66
|
|
|
'setSynthetic' => $attribute->isSynthetic ?? $definition->isSynthetic(), |
|
67
|
|
|
'setBindings' => [...$definition->getBindings(), ...$attribute->bindings], |
|
68
|
|
|
'setConfigurator' => $attribute->configurator ?? $definition->getConfigurator(), |
|
69
|
|
|
'setProperties' => [...$definition->getProperties(), ...$attribute->properties], |
|
70
|
|
|
'setLazy' => $attribute->isLazy ? !$reflectionClass->isFinal() : $attribute->isLazy, |
|
71
|
|
|
'setTags' => [...$definition->getTags(), ...TagHelper::mapTags(...$attribute->tags)], |
|
72
|
|
|
'setAutoconfigured' => $attribute->isAutoconfigured ?? $definition->isAutoconfigured(), |
|
73
|
|
|
'setAbstract' => $attribute->isAbstract ? $reflectionClass->isAbstract() : $attribute->isAbstract, |
|
74
|
|
|
'setArguments' => [ |
|
75
|
|
|
...$definition->getArguments(), |
|
76
|
|
|
...$attribute->arguments, |
|
77
|
|
|
...DefinitionHelper::mapDefinitionArguments($container, $attribute->arguments), |
|
78
|
|
|
], |
|
79
|
|
|
'setMethodCalls' => [ |
|
80
|
|
|
...$definition->getMethodCalls(), |
|
81
|
|
|
...DefinitionHelper::mapDefinitionMethodCalls($container, $attribute->calls), |
|
82
|
|
|
], |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|