1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Go! AOP framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2012, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\Core; |
12
|
|
|
|
13
|
|
|
use Go\Aop\Advisor; |
14
|
|
|
use Go\Aop\Aspect; |
15
|
|
|
use Go\Aop\Framework; |
16
|
|
|
use Go\Aop\Pointcut; |
17
|
|
|
use Go\Aop\Support; |
18
|
|
|
use Go\Lang\Annotation; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Introduction aspect extension |
22
|
|
|
*/ |
23
|
|
|
class IntroductionAspectExtension extends AbstractAspectLoaderExtension |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Introduction aspect loader works with annotations from aspect |
28
|
|
|
* |
29
|
|
|
* For extension that works with annotations additional metaInformation will be passed |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function getKind() |
34
|
|
|
{ |
35
|
|
|
return self::KIND_ANNOTATION; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Introduction aspect loader works only with properties of aspect |
40
|
|
|
* |
41
|
|
|
* @return string|array |
42
|
|
|
*/ |
43
|
2 |
|
public function getTarget() |
44
|
|
|
{ |
45
|
2 |
|
return self::TARGET_PROPERTY; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Checks if loader is able to handle specific point of aspect |
50
|
|
|
* |
51
|
|
|
* @param Aspect $aspect Instance of aspect |
52
|
|
|
* @param mixed|\ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection Reflection of point |
53
|
|
|
* @param mixed|null $metaInformation Additional meta-information, e.g. annotation for method |
54
|
|
|
* |
55
|
|
|
* @return boolean true if extension is able to create an advisor from reflection and metaInformation |
56
|
|
|
*/ |
57
|
|
|
public function supports(Aspect $aspect, $reflection, $metaInformation = null) |
58
|
|
|
{ |
59
|
|
|
return |
60
|
|
|
($metaInformation instanceof Annotation\DeclareParents) || |
61
|
|
|
($metaInformation instanceof Annotation\DeclareError); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Loads definition from specific point of aspect into the container |
66
|
|
|
* |
67
|
|
|
* @param Aspect $aspect Instance of aspect |
68
|
|
|
* @param mixed|\ReflectionClass|\ReflectionMethod|\ReflectionProperty $reflection Reflection of point |
69
|
|
|
* @param mixed|null $metaInformation Additional meta-information |
70
|
|
|
* |
71
|
|
|
* @throws \UnexpectedValueException |
72
|
|
|
* |
73
|
|
|
* @return array|Pointcut[]|Advisor[] |
74
|
|
|
*/ |
75
|
|
|
public function load(Aspect $aspect, $reflection, $metaInformation = null) |
76
|
|
|
{ |
77
|
|
|
$loadedItems = []; |
78
|
|
|
$pointcut = $this->parsePointcut($aspect, $reflection, $metaInformation); |
79
|
|
|
$propertyId = $reflection->class . '->' . $reflection->name; |
80
|
|
|
|
81
|
|
|
switch (true) { |
82
|
|
|
case ($metaInformation instanceof Annotation\DeclareParents): |
83
|
|
|
$interface = $metaInformation->interface; |
84
|
|
|
$implement = $metaInformation->defaultImpl; |
85
|
|
|
$advice = new Framework\TraitIntroductionInfo($interface, $implement); |
86
|
|
|
$advisor = new Support\DeclareParentsAdvisor($pointcut->getClassFilter(), $advice); |
87
|
|
|
$loadedItems[$propertyId] = $advisor; |
88
|
|
|
break; |
89
|
|
|
|
90
|
|
|
case ($metaInformation instanceof Annotation\DeclareError): |
91
|
|
|
$reflection->setAccessible(true); |
92
|
|
|
$message = $reflection->getValue($aspect); |
93
|
|
|
$level = $metaInformation->level; |
94
|
|
|
$advice = new Framework\DeclareErrorInterceptor($message, $level, $metaInformation->value); |
95
|
|
|
$loadedItems[$propertyId] = new Support\DefaultPointcutAdvisor($pointcut, $advice); |
|
|
|
|
96
|
|
|
break; |
97
|
|
|
|
98
|
|
|
default: |
99
|
|
|
throw new \UnexpectedValueException("Unsupported pointcut class: " . get_class($pointcut)); |
100
|
|
|
|
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $loadedItems; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.