|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
|
4
|
|
|
* on 07.08.16 at 15:46 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace samsonframework\container\configurator; |
|
7
|
|
|
|
|
8
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
|
9
|
|
|
use samsonframework\container\metadata\ParameterMetadata; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Method argument injection configurator. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Vitaly Egorov <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class InjectableArgumentConfigurator extends InjectableAbstractConfigurator implements MethodConfiguratorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
* |
|
21
|
|
|
* @throws \InvalidArgumentException |
|
22
|
|
|
*/ |
|
23
|
|
|
public function toMethodMetadata(MethodMetadata $methodMetadata) |
|
24
|
|
|
{ |
|
25
|
|
|
// Check for argument name input and validity |
|
26
|
|
|
if (!$this->checkArgumentExists($this->argumentName, $methodMetadata)) { |
|
27
|
|
|
throw new \InvalidArgumentException( |
|
28
|
|
|
'@InjectArgument argument "' |
|
29
|
|
|
. $methodMetadata->classMetadata->className . '::' |
|
30
|
|
|
. $methodMetadata->name . ' ' |
|
31
|
|
|
. $this->argumentName . '" does not exists' |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Check for type input |
|
36
|
|
|
if ($this->argumentType === null || $this->argumentType === '') { |
|
37
|
|
|
throw new \InvalidArgumentException( |
|
38
|
|
|
'@InjectArgument argument "' |
|
39
|
|
|
. $methodMetadata->classMetadata->className . '::' |
|
40
|
|
|
. $methodMetadata->name . ' ' |
|
41
|
|
|
. $this->argumentName . '" type not specified' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Store dependency with fully qualified type name |
|
46
|
|
|
$methodMetadata->dependencies[$this->argumentName] = $this->buildFullClassName( |
|
47
|
|
|
$this->argumentType, |
|
48
|
|
|
$methodMetadata->classMetadata->nameSpace |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Check method argument existence. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $argument |
|
56
|
|
|
* @param MethodMetadata $methodMetadata |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool True if @InjectArgument argument name is valid |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function checkArgumentExists(string $argument, MethodMetadata $methodMetadata) : bool |
|
61
|
|
|
{ |
|
62
|
|
|
return $argument !== null && array_key_exists($argument, $methodMetadata->parametersMetadata); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|