|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/* |
|
4
|
|
|
* Go! AOP framework |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright 2011, Lisachenko Alexander <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Go\Aop\Framework; |
|
13
|
|
|
|
|
14
|
|
|
use Go\Aop\Intercept\ClassJoinpoint; |
|
15
|
|
|
use Go\Core\AspectContainer; |
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
use function strlen; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Static initialization joinpoint is invoked after class is loaded into memory |
|
21
|
|
|
*/ |
|
22
|
|
|
class StaticInitializationJoinpoint extends AbstractJoinpoint implements ClassJoinpoint |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ReflectionClass |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $reflectionClass; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor for the class static initialization joinpoint |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $advices List of advices for this invocation |
|
34
|
1 |
|
*/ |
|
35
|
|
|
public function __construct(string $className, string $unusedType, array $advices) |
|
36
|
1 |
|
{ |
|
37
|
1 |
|
$originalClass = $className; |
|
38
|
1 |
|
if (strpos($originalClass, AspectContainer::AOP_PROXIED_SUFFIX)) { |
|
39
|
|
|
$originalClass = substr($originalClass, 0, -strlen(AspectContainer::AOP_PROXIED_SUFFIX)); |
|
40
|
1 |
|
} |
|
41
|
1 |
|
$this->reflectionClass = new \ReflectionClass($originalClass); |
|
42
|
1 |
|
parent::__construct($advices); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Proceeds to the next interceptor in the chain. |
|
47
|
|
|
* |
|
48
|
|
|
* @return void Static initializtion could not return anything |
|
49
|
1 |
|
*/ |
|
50
|
|
|
public function proceed() |
|
51
|
1 |
|
{ |
|
52
|
1 |
|
if (isset($this->advices[$this->current])) { |
|
53
|
1 |
|
$currentInterceptor = $this->advices[$this->current++]; |
|
54
|
|
|
$currentInterceptor->invoke($this); |
|
55
|
1 |
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Invokes current joinpoint with all interceptors |
|
60
|
1 |
|
*/ |
|
61
|
|
|
final public function __invoke(): void |
|
62
|
1 |
|
{ |
|
63
|
1 |
|
$this->current = 0; |
|
64
|
1 |
|
$this->proceed(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the object for which current joinpoint is invoked |
|
69
|
|
|
* |
|
70
|
|
|
* @return object|null Instance of object or null for static call/unavailable context |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getThis(): ?object |
|
73
|
|
|
{ |
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Checks if the current joinpoint is dynamic or static |
|
79
|
|
|
* |
|
80
|
|
|
* Dynamic joinpoint contains a reference to an object that can be received via getThis() method call |
|
81
|
|
|
* |
|
82
|
|
|
* @see ClassJoinpoint::getThis() |
|
83
|
|
|
*/ |
|
84
|
|
|
public function isDynamic(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns the static scope name (class name) of this joinpoint. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getScope(): string |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->reflectionClass->getName(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns a friendly description of current joinpoint |
|
99
|
|
|
*/ |
|
100
|
|
|
final public function __toString(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return sprintf( |
|
103
|
|
|
'staticinitialization(%s)', |
|
104
|
|
|
$this->getScope() |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|