1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Created by PhpStorm. |
6
|
|
|
* User: root |
7
|
|
|
* Date: 27.07.2016 |
8
|
|
|
* Time: 1:55. |
9
|
|
|
*/ |
10
|
|
|
namespace samsonframework\container\annotation; |
11
|
|
|
|
12
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
13
|
|
|
use samsonframework\container\metadata\PropertyMetadata; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Injection annotation class. |
17
|
|
|
* |
18
|
|
|
* @Annotation |
19
|
|
|
*/ |
20
|
|
|
class Inject extends CollectionValue implements MethodInterface, PropertyInterface |
21
|
|
|
{ |
22
|
|
|
/** {@inheritdoc} */ |
23
|
1 |
|
public function toMethodMetadata(MethodMetadata $metadata) |
24
|
|
|
{ |
25
|
1 |
|
$metadata->dependencies = $this->collection; |
|
|
|
|
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
/** {@inheritdoc} */ |
29
|
5 |
|
public function toPropertyMetadata(PropertyMetadata $propertyMetadata) |
30
|
|
|
{ |
31
|
|
|
// Get @Inject("value") |
32
|
5 |
|
$propertyMetadata->injectable = array_shift($this->collection); |
33
|
|
|
|
34
|
|
|
// Check if we need to append namespace to injectable |
35
|
5 |
View Code Duplication |
if ($propertyMetadata->injectable !== null && strpos($propertyMetadata->injectable, '\\') === false) { |
|
|
|
|
36
|
1 |
|
$propertyMetadata->injectable = $propertyMetadata->classMetadata->nameSpace . '\\' . $propertyMetadata->injectable; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Check if we need to append namespace to type hint |
40
|
5 |
View Code Duplication |
if ($propertyMetadata->typeHint !== null && strpos($propertyMetadata->typeHint, '\\') === false) { |
|
|
|
|
41
|
1 |
|
$propertyMetadata->typeHint = $propertyMetadata->classMetadata->nameSpace . '\\' . $propertyMetadata->typeHint; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Check for inheritance violation |
45
|
5 |
|
if ($this->checkInheritanceViolation($propertyMetadata)) { |
46
|
1 |
|
throw new \InvalidArgumentException('@Inject dependency violates ' . $propertyMetadata->typeHint . ' inheritance'); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
if ($this->checkInterfaceWithoutClassName($propertyMetadata)) { |
50
|
1 |
|
throw new \InvalidArgumentException('Cannot @Inject interface, inherited class name should be specified'); |
51
|
|
|
} |
52
|
3 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if @Inject violates inheritance. |
56
|
|
|
* |
57
|
|
|
* @param PropertyMetadata $propertyMetadata |
58
|
|
|
* |
59
|
|
|
* @return bool True if @Inject violates inheritance |
60
|
|
|
*/ |
61
|
5 |
|
protected function checkInheritanceViolation(PropertyMetadata $propertyMetadata) : bool |
62
|
|
|
{ |
63
|
|
|
// Check for inheritance violation |
64
|
5 |
|
if ($propertyMetadata->injectable !== null && $propertyMetadata->typeHint !== null) { |
65
|
3 |
|
$inheritance = array_merge([$propertyMetadata->injectable], class_parents($propertyMetadata->injectable)); |
66
|
3 |
|
return !in_array($propertyMetadata->typeHint, $inheritance, true); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Check if @Inject has no class name and type hint is interface. |
74
|
|
|
* |
75
|
|
|
* @param PropertyMetadata $propertyMetadata |
76
|
|
|
* |
77
|
|
|
* @return bool True if @Inject has no class name and type hint is interface. |
78
|
|
|
*/ |
79
|
4 |
|
protected function checkInterfaceWithoutClassName(PropertyMetadata $propertyMetadata) : bool |
80
|
|
|
{ |
81
|
4 |
|
return $propertyMetadata->typeHint !== null |
82
|
4 |
|
&& $propertyMetadata->injectable === null |
83
|
4 |
|
&& (new \ReflectionClass($propertyMetadata->typeHint))->isInterface(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.