|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/* |
|
4
|
|
|
* Go! AOP framework |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright 2012, 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\Support; |
|
13
|
|
|
|
|
14
|
|
|
use Go\Aop\Advice; |
|
15
|
|
|
use Go\Aop\IntroductionAdvisor; |
|
16
|
|
|
use Go\Aop\IntroductionInfo; |
|
17
|
|
|
use Go\Aop\Pointcut\PointcutClassFilterTrait; |
|
18
|
|
|
use Go\Aop\PointFilter; |
|
19
|
|
|
use ReflectionClass; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Introduction advisor delegating to the given object. |
|
23
|
|
|
*/ |
|
24
|
|
|
class DeclareParentsAdvisor extends AbstractGenericAdvisor implements IntroductionAdvisor |
|
25
|
|
|
{ |
|
26
|
|
|
use PointcutClassFilterTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Introduction information |
|
30
|
|
|
* |
|
31
|
|
|
* @var IntroductionInfo |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $advice; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates an advisor for declaring mixins via traits and interfaces. |
|
37
|
|
|
* |
|
38
|
|
|
* @param PointFilter $classFilter Class filter |
|
39
|
|
|
* @param IntroductionInfo $info Introduction information |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(PointFilter $classFilter, IntroductionInfo $info) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->classFilter = $classFilter; |
|
44
|
|
|
parent::__construct($info); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Can the advised interfaces be implemented by the introduction advice? |
|
49
|
|
|
* |
|
50
|
|
|
* Invoked before adding an IntroductionAdvisor. |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
* @throws \InvalidArgumentException if the advised interfaces can't be implemented by the introduction advice |
|
54
|
|
|
*/ |
|
55
|
|
|
public function validateInterfaces() |
|
56
|
|
|
{ |
|
57
|
|
|
$refInterface = new ReflectionClass(reset($this->advice->getInterfaces())); |
|
|
|
|
|
|
58
|
|
|
$refImplementation = new ReflectionClass(reset($this->advice->getTraits())); |
|
|
|
|
|
|
59
|
|
|
if (!$refInterface->isInterface()) { |
|
60
|
|
|
throw new \InvalidArgumentException("Only interface can be introduced"); |
|
61
|
|
|
} |
|
62
|
|
|
if (!$refImplementation->isTrait()) { |
|
63
|
|
|
throw new \InvalidArgumentException("Only trait can be used as implementation"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($refInterface->getMethods() as $interfaceMethod) { |
|
67
|
|
|
if (!$refImplementation->hasMethod($interfaceMethod->name)) { |
|
68
|
|
|
throw new \DomainException("Implementation requires method {$interfaceMethod->name}"); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|