1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: root |
5
|
|
|
* Date: 29.07.2016 |
6
|
|
|
* Time: 21:38. |
7
|
|
|
*/ |
8
|
|
|
namespace samsonframework\container\collection; |
9
|
|
|
|
10
|
|
|
use samsonframework\container\configurator\MethodConfiguratorInterface; |
11
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
12
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
13
|
|
|
use samsonframework\container\metadata\ParameterMetadata; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Collection method resolver class. |
17
|
|
|
* @author Vitaly Iegorov <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class CollectionMethodResolver extends AbstractCollectionResolver implements CollectionResolverInterface |
20
|
|
|
{ |
21
|
|
|
/** Collection method key */ |
22
|
|
|
const KEY = 'methods'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var CollectionParameterResolver Parameter resolver |
26
|
|
|
*/ |
27
|
|
|
protected $parameterResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* CollectionMethodResolver constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $collectionConfigurators |
33
|
|
|
* @param CollectionParameterResolver $parameterResolver |
34
|
|
|
* |
35
|
|
|
* @throws \InvalidArgumentException |
36
|
|
|
*/ |
37
|
1 |
|
public function __construct(array $collectionConfigurators, CollectionParameterResolver $parameterResolver) |
38
|
|
|
{ |
39
|
1 |
|
parent::__construct($collectionConfigurators); |
40
|
|
|
|
41
|
1 |
|
$this->parameterResolver = $parameterResolver; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function resolve(array $classDataArray, ClassMetadata $classMetadata) |
48
|
|
|
{ |
49
|
|
|
// Iterate collection |
50
|
1 |
|
if (array_key_exists(self::KEY, $classDataArray)) { |
51
|
1 |
|
$reflectionClass = new \ReflectionClass($classMetadata->className); |
52
|
|
|
// Iterate configured methods |
53
|
1 |
|
foreach ($classDataArray[self::KEY] as $methodName => $methodDataArray) { |
54
|
1 |
|
$methodReflection = $reflectionClass->getMethod($methodName); |
55
|
|
|
|
56
|
|
|
// Create method metadata instance |
57
|
1 |
|
$methodMetadata = new MethodMetadata($classMetadata); |
58
|
1 |
|
$methodMetadata->name = $methodReflection->name; |
59
|
1 |
|
$methodMetadata->modifiers = $methodReflection->getModifiers(); |
60
|
1 |
|
$methodMetadata->isPublic = $methodReflection->isPublic(); |
61
|
|
|
|
62
|
|
|
// Check if methods inject any instances |
63
|
1 |
|
if (array_key_exists(CollectionParameterResolver::KEY, $methodDataArray)) { |
64
|
|
|
|
65
|
|
|
/** @var \ReflectionParameter $parameter */ |
66
|
1 |
|
$parameterMetadata = new ParameterMetadata($methodMetadata->classMetadata, $methodMetadata); |
67
|
|
|
// Iterate and create properties metadata form each parameter in method |
68
|
1 |
|
foreach ($methodReflection->getParameters() as $parameter) { |
69
|
1 |
|
$parameterMetadata = clone $parameterMetadata; |
70
|
1 |
|
$parameterMetadata->name = $parameter->name; |
71
|
1 |
|
$parameterMetadata->typeHint = (string)$parameter->getType(); |
72
|
|
|
|
73
|
|
|
// If config has such parameter |
74
|
|
|
if ( |
75
|
1 |
|
array_key_exists(CollectionParameterResolver::KEY, $methodDataArray) && |
76
|
1 |
|
array_key_exists($parameter->name, $methodDataArray[CollectionParameterResolver::KEY]) |
77
|
|
|
) { |
78
|
1 |
|
$parameterDataArray = $methodDataArray[CollectionParameterResolver::KEY][$parameter->name]; |
79
|
|
|
// Resolve parameter |
80
|
1 |
|
$this->parameterResolver->resolve($parameterDataArray, $parameterMetadata); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Store parameter metadata |
84
|
1 |
|
$methodMetadata->parametersMetadata[$parameterMetadata->name] = $parameterMetadata; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Iterate collection and resolve method configurator |
89
|
1 |
View Code Duplication |
if (array_key_exists('@attributes', $methodDataArray)) { |
|
|
|
|
90
|
|
|
// Iterate collection attribute configurators |
91
|
|
|
foreach ($this->collectionConfigurators as $key => $collectionConfigurator) { |
92
|
|
|
// If this is supported collection configurator |
93
|
|
|
if (array_key_exists($key, $methodDataArray['@attributes'])) { |
94
|
|
|
/** @var MethodConfiguratorInterface $configurator Create instance */ |
95
|
|
|
$configurator = new $collectionConfigurator($methodDataArray['@attributes'][$key]); |
96
|
|
|
// Fill in class metadata |
97
|
|
|
$configurator->toMethodMetadata($methodDataArray); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Save method metadata |
103
|
1 |
|
$classMetadata->methodsMetadata[$methodMetadata->name] = $methodMetadata; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return $classMetadata; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.