Completed
Pull Request — master (#240)
by Alexander
05:40
created

TraitProxy::getJoinPoint()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.3142
cc 3
eloc 11
nc 4
nop 4
crap 12
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\Proxy;
12
13
use Go\Aop\Features;
14
use Go\Core\AspectContainer;
15
use Go\Core\AspectKernel;
16
use Go\Core\LazyAdvisorAccessor;
17
use ReflectionMethod;
18
use ReflectionParameter;
19
20
/**
21
 * Trait proxy builder that is used to generate a trait from the list of joinpoints
22
 */
23
class TraitProxy extends ClassProxy
24
{
25
26
    /**
27
     * List of advices for traits
28
     *
29
     * @var array
30
     */
31
    protected static $traitAdvices = [];
32
33
    /**
34
     * Overridden static property for TraitProxy
35
     *
36
     * {@inheritDoc}
37
     */
38
    protected static $invocationClassMap = [];
39
40
    /**
41
     * Inject advices for given trait
42
     *
43
     * NB This method will be used as a callback during source code evaluation to inject joinpoints
44
     *
45
     * @param string $className Aop child proxy class
46
     * @param array|\Go\Aop\Advice[] $traitAdvices List of advices to inject into class
47
     *
48
     * @return void
49
     */
50
    public static function injectJoinPoints($className, array $traitAdvices = [])
51
    {
52
        self::$traitAdvices[$className] = $traitAdvices;
53
    }
54
55
    public static function getJoinPoint($traitName, $className, $joinPointType, $pointName)
56
    {
57
        /** @var LazyAdvisorAccessor $accessor */
58
        static $accessor = null;
59
60
        if (!isset($accessor)) {
61
            $aspectKernel = AspectKernel::getInstance();
62
            $accessor     = $aspectKernel->getContainer()->get('aspect.advisor.accessor');
63
        }
64
65
        $advices = self::$traitAdvices[$traitName][$joinPointType][$pointName];
66
67
        $filledAdvices = [];
68
        foreach ($advices as $advisorName) {
69
            $filledAdvices[] = $accessor->$advisorName;
70
        }
71
72
        $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $pointName . '➩', $filledAdvices);
73
74
        return $joinpoint;
75
    }
76
77
    /**
78
     * Creates definition for trait method body
79
     *
80
     * @param ReflectionMethod $method Method reflection
81
     *
82
     * @return string new method body
83
     */
84
    protected function getJoinpointInvocationBody(ReflectionMethod $method)
85
    {
86
        $isStatic = $method->isStatic();
87
        $class    = '\\' . __CLASS__;
88
        $scope    = $isStatic ? self::$staticLsbExpression : '$this';
89
        $prefix   = $isStatic ? AspectContainer::STATIC_METHOD_PREFIX : AspectContainer::METHOD_PREFIX;
90
91
        $args = $this->prepareArgsLine($method);
92
        $args = $scope . ($args ? ", [$args]" : '');
93
94
        return <<<BODY
95
static \$__joinPoint = null;
96
if (!\$__joinPoint) {
97
    \$__joinPoint = {$class}::getJoinPoint(__TRAIT__, __CLASS__, '{$prefix}', '{$method->name}');
98
}
99
return \$__joinPoint->__invoke($args);
100
BODY;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    public function __toString()
107
    {
108
        $classCode = (
109
            $this->class->getDocComment() . "\n" . // Original doc-block
110
            'trait ' . // 'trait' keyword
111
            $this->name . "\n" . // Name of the trait
112
            "{\n" . // Start of trait body
113
            $this->indent(
114
                'use ' . join(', ', array(-1 => $this->parentClassName) + $this->traits) .
115
                $this->getMethodAliasesCode()
116
            ) . "\n" . // Use traits and aliases section
117
            $this->indent(join("\n", $this->methodsCode)) . "\n" . // Method definitions
118
            "}" // End of trait body
119
        );
120
121
        return $classCode
122
            // Inject advices on call
123
            . PHP_EOL
124
            . '\\' . __CLASS__ . "::injectJoinPoints('"
125
                . $this->class->name . "',"
126
                . var_export($this->advices, true) . ");";
127
    }
128
129
    private function getMethodAliasesCode()
130
    {
131
        $aliasesLines = [];
132
        foreach (array_keys($this->methodsCode) as $methodName) {
133
            $aliasesLines[] = "{$this->parentClassName}::{$methodName} as protected {$methodName}➩;";
134
        }
135
136
        return "{\n " . $this->indent(join("\n", $aliasesLines)) . "\n}";
137
    }
138
}
139