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\containercollection; |
9
|
|
|
|
10
|
|
|
use samsonframework\container\configurator\MethodConfiguratorInterface; |
11
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
12
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
13
|
|
|
use samsonframework\container\resolver\MethodResolverTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Collection method resolver class. |
17
|
|
|
* @author Vitaly Iegorov <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class CollectionMethodResolver extends AbstractCollectionResolver implements CollectionResolverInterface |
20
|
|
|
{ |
21
|
|
|
use MethodResolverTrait; |
22
|
|
|
|
23
|
|
|
/** Collection method key */ |
24
|
|
|
const KEY = 'methods'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CollectionParameterResolver Parameter resolver |
28
|
|
|
*/ |
29
|
|
|
protected $parameterResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* CollectionMethodResolver constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $collectionConfigurators |
35
|
|
|
* @param CollectionParameterResolver $parameterResolver |
36
|
|
|
* |
37
|
|
|
* @throws \InvalidArgumentException |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $collectionConfigurators, CollectionParameterResolver $parameterResolver) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($collectionConfigurators); |
42
|
|
|
|
43
|
|
|
$this->parameterResolver = $parameterResolver; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function resolve(array $configurationArray, ClassMetadata $classMetadata) |
50
|
|
|
{ |
51
|
|
|
// Iterate collection |
52
|
|
|
if (array_key_exists(self::KEY, $configurationArray)) { |
53
|
|
|
$reflectionClass = new \ReflectionClass($classMetadata->className); |
54
|
|
|
|
55
|
|
|
// Iterate configured methods |
56
|
|
|
foreach ($configurationArray[self::KEY] as $methodName => $methodDataArray) { |
57
|
|
|
$this->resolveMethod( |
58
|
|
|
$this->resolveMethodMetadata( |
59
|
|
|
$reflectionClass->getMethod($methodName), |
60
|
|
|
$classMetadata |
61
|
|
|
), |
62
|
|
|
$methodDataArray |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $classMetadata; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Resolve method configuration. |
72
|
|
|
* |
73
|
|
|
* @param MethodMetadata $methodMetadata |
74
|
|
|
* @param array $methodDataArray |
75
|
|
|
*/ |
76
|
|
|
protected function resolveMethod(MethodMetadata $methodMetadata, array $methodDataArray) |
77
|
|
|
{ |
78
|
|
|
// Check if methods inject any instances |
79
|
|
|
if (array_key_exists(CollectionParameterResolver::KEY, $methodDataArray)) { |
80
|
|
|
foreach ($methodMetadata->parametersMetadata as $parameterMetadata) { |
81
|
|
|
// If config has parameter |
82
|
|
|
if (array_key_exists($parameterMetadata->name, $methodDataArray[CollectionParameterResolver::KEY])) { |
83
|
|
|
$this->parameterResolver->resolve( |
84
|
|
|
$methodDataArray[CollectionParameterResolver::KEY][$parameterMetadata->name], |
85
|
|
|
$parameterMetadata |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
// Store method dependencies |
89
|
|
|
$methodMetadata->dependencies[$parameterMetadata->name] = $parameterMetadata->dependency; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Process attributes |
95
|
|
|
foreach ($this->getAttributeConfigurator($methodDataArray) as $configurator) { |
96
|
|
|
/** @var MethodConfiguratorInterface $configurator Parse method metadata */ |
97
|
|
|
$configurator->toMethodMetadata($methodMetadata); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|