1 | <?php |
||
22 | class TraitProxy extends ClassProxy |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * List of advices for traits |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected static $traitAdvices = []; |
||
31 | |||
32 | /** |
||
33 | * Overridden static property for TraitProxy |
||
34 | * |
||
35 | * {@inheritDoc} |
||
36 | */ |
||
37 | protected static $invocationClassMap = []; |
||
38 | |||
39 | /** |
||
40 | * Inject advices for given trait |
||
41 | * |
||
42 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
43 | * |
||
44 | * @param string $className Aop child proxy class |
||
45 | * @param array|\Go\Aop\Advice[] $traitAdvices List of advices to inject into class |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public static function injectJoinPoints($className, array $traitAdvices = []) |
||
53 | |||
54 | public static function getJoinPoint($traitName, $className, $joinPointType, $pointName) |
||
55 | { |
||
56 | /** @var LazyAdvisorAccessor $accessor */ |
||
57 | static $accessor = null; |
||
58 | |||
59 | if (empty(self::$invocationClassMap)) { |
||
60 | $aspectKernel = AspectKernel::getInstance(); |
||
61 | $accessor = $aspectKernel->getContainer()->get('aspect.advisor.accessor'); |
||
62 | self::setMappings( |
||
63 | $aspectKernel->hasFeature(Features::USE_SPLAT_OPERATOR) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | $advices = self::$traitAdvices[$traitName][$joinPointType][$pointName]; |
||
68 | |||
69 | $filledAdvices = []; |
||
70 | foreach ($advices as $advisorName) { |
||
71 | $filledAdvices[] = $accessor->$advisorName; |
||
72 | } |
||
73 | |||
74 | $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $pointName . '➩', $filledAdvices); |
||
75 | |||
76 | return $joinpoint; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Creates definition for trait method body |
||
81 | * |
||
82 | * @param ParsedMethod $method Method reflection |
||
83 | * |
||
84 | * @return string new method body |
||
85 | */ |
||
86 | protected function getJoinpointInvocationBody(ParsedMethod $method) |
||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | public function __toString() |
||
109 | { |
||
110 | $classCode = ( |
||
111 | $this->class->getDocComment() . "\n" . // Original doc-block |
||
112 | 'trait ' . // 'trait' keyword |
||
113 | $this->name . "\n" . // Name of the trait |
||
114 | "{\n" . // Start of trait body |
||
115 | $this->indent( |
||
116 | 'use ' . join(', ', array(-1 => $this->parentClassName) + $this->traits) . |
||
117 | $this->getMethodAliasesCode() |
||
118 | ) . "\n" . // Use traits and aliases section |
||
119 | $this->indent(join("\n", $this->methodsCode)) . "\n" . // Method definitions |
||
120 | "}" // End of trait body |
||
121 | ); |
||
122 | |||
123 | return $classCode |
||
124 | // Inject advices on call |
||
125 | . PHP_EOL |
||
126 | . '\\' . __CLASS__ . "::injectJoinPoints('" |
||
127 | . $this->class->name . "'," |
||
128 | . var_export($this->advices, true) . ");"; |
||
129 | } |
||
130 | |||
131 | private function getMethodAliasesCode() |
||
140 | } |
||
141 |