1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Ruslan Molodyko. |
4
|
|
|
* Date: 10.09.2016 |
5
|
|
|
* Time: 15:33 |
6
|
|
|
*/ |
7
|
|
|
namespace samsonframework\container\definition\analyzer\annotation\annotation; |
8
|
|
|
|
9
|
|
|
use samsonframework\container\definition\analyzer\annotation\ResolveMethodInterface; |
10
|
|
|
use samsonframework\container\definition\analyzer\annotation\ResolvePropertyInterface; |
11
|
|
|
use samsonframework\container\definition\analyzer\DefinitionAnalyzer; |
12
|
|
|
use samsonframework\container\definition\analyzer\exception\WrongAnnotationConstructorException; |
13
|
|
|
use samsonframework\container\definition\ClassDefinition; |
14
|
|
|
use samsonframework\container\definition\exception\MethodDefinitionAlreadyExistsException; |
15
|
|
|
use samsonframework\container\definition\exception\MethodDefinitionNotFoundException; |
16
|
|
|
use samsonframework\container\definition\exception\ParameterDefinitionAlreadyExistsException; |
17
|
|
|
use samsonframework\container\definition\exception\PropertyDefinitionNotFoundException; |
18
|
|
|
use samsonframework\container\definition\MethodDefinition; |
19
|
|
|
use samsonframework\container\definition\PropertyDefinition; |
20
|
|
|
use samsonframework\container\definition\reference\ServiceReference; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Injection annotation service. |
24
|
|
|
* |
25
|
|
|
* @Annotation |
26
|
|
|
*/ |
27
|
|
|
class InjectService implements ResolvePropertyInterface, ResolveMethodInterface |
28
|
|
|
{ |
29
|
|
|
/** @var mixed */ |
30
|
|
|
protected $value; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Inject constructor. |
34
|
|
|
* |
35
|
|
|
* @param $value |
36
|
|
|
*/ |
37
|
|
|
public function __construct($value) |
38
|
|
|
{ |
39
|
|
|
$this->value = $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
* @throws PropertyDefinitionNotFoundException |
45
|
|
|
*/ |
46
|
|
|
public function resolveProperty( |
47
|
|
|
DefinitionAnalyzer $analyzer, |
48
|
|
|
ClassDefinition $classDefinition, |
49
|
|
|
\ReflectionProperty $reflectionProperty |
50
|
|
|
) { |
51
|
|
|
$propertyName = $reflectionProperty->getName(); |
52
|
|
|
if ($classDefinition->hasProperty($propertyName)) { |
53
|
|
|
$classDefinition->getProperty($propertyName) |
54
|
|
|
->defineDependency(new ServiceReference($this->value['value'])); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
* @throws WrongAnnotationConstructorException |
61
|
|
|
* @throws MethodDefinitionNotFoundException |
62
|
|
|
* @throws ParameterDefinitionAlreadyExistsException |
63
|
|
|
* @throws MethodDefinitionAlreadyExistsException |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function resolveMethod( |
|
|
|
|
66
|
|
|
DefinitionAnalyzer $analyzer, |
67
|
|
|
ClassDefinition $classDefinition, |
68
|
|
|
\ReflectionMethod $reflectionMethod |
69
|
|
|
) { |
70
|
|
|
// Get parameter key |
71
|
|
|
$key = array_keys($this->value)[0]; |
72
|
|
|
$classDefinition->setupMethod($reflectionMethod->getName()) |
|
|
|
|
73
|
|
|
->defineParameter($key) |
74
|
|
|
->defineDependency(new ServiceReference($this->value[$key]))->end(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.