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 |
|
$methodMetadata = new MethodMetadata($classMetadata); |
54
|
1 |
|
foreach ($classDataArray[self::KEY] as $methodName => $methodDataArray) { |
55
|
1 |
|
$methodReflection = $reflectionClass->getMethod($methodName); |
56
|
|
|
|
57
|
|
|
// Create method metadata instance |
58
|
1 |
|
$methodMetadata = clone $methodMetadata; |
59
|
1 |
|
$methodMetadata->name = $methodReflection->name; |
60
|
1 |
|
$methodMetadata->modifiers = $methodReflection->getModifiers(); |
61
|
1 |
|
$methodMetadata->isPublic = $methodReflection->isPublic(); |
62
|
|
|
|
63
|
|
|
// Check if methods inject any instances |
64
|
1 |
|
if (array_key_exists(CollectionParameterResolver::KEY, $methodDataArray)) { |
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
|
1 |
|
if (array_key_exists($parameter->name, $methodDataArray[CollectionParameterResolver::KEY])) { |
75
|
1 |
|
$parameterDataArray = $methodDataArray[CollectionParameterResolver::KEY][$parameter->name]; |
76
|
|
|
// Resolve parameter |
77
|
1 |
|
$this->parameterResolver->resolve($parameterDataArray, $parameterMetadata); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Store parameter metadata |
81
|
1 |
|
$methodMetadata->parametersMetadata[$parameterMetadata->name] = $parameterMetadata; |
82
|
1 |
|
$methodMetadata->dependencies[$parameterMetadata->name] = $parameterMetadata->dependency; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Process attributes |
87
|
1 |
|
foreach ($this->getAttributeConfigurator($methodDataArray) as $configurator) { |
88
|
|
|
/** @var MethodConfiguratorInterface $configurator Parse method metadata */ |
89
|
|
|
$configurator->toMethodMetadata($methodMetadata); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Save method metadata to class metadata |
93
|
1 |
|
$classMetadata->methodsMetadata[$methodMetadata->name] = $methodMetadata; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return $classMetadata; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|