1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Deal framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2014, 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 PhpDeal\Aspect; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Annotations\Reader; |
14
|
|
|
use Go\Aop\Aspect; |
15
|
|
|
use Go\Aop\Intercept\MethodInvocation; |
16
|
|
|
use PhpDeal\Annotation\Verify; |
17
|
|
|
use PhpDeal\Contract\Fetcher\ParentClass\MethodConditionWithInheritDocFetcher; |
18
|
|
|
use PhpDeal\Exception\ContractViolation; |
19
|
|
|
use Go\Lang\Annotation\Before; |
20
|
|
|
|
21
|
|
|
class PreconditionCheckerAspect extends AbstractContractAspect implements Aspect |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var MethodConditionWithInheritDocFetcher |
25
|
|
|
*/ |
26
|
|
|
private $methodConditionFetcher; |
27
|
|
|
|
28
|
|
|
public function __construct(Reader $reader) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($reader); |
31
|
|
|
$this->methodConditionFetcher = new MethodConditionWithInheritDocFetcher(Verify::class, $reader); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Verifies pre-condition contract for the method |
36
|
|
|
* |
37
|
|
|
* @param MethodInvocation $invocation |
38
|
|
|
* @Before("@execution(PhpDeal\Annotation\Verify)") |
39
|
|
|
* |
40
|
|
|
* @throws ContractViolation |
41
|
|
|
*/ |
42
|
19 |
|
public function preConditionContract(MethodInvocation $invocation) |
43
|
|
|
{ |
44
|
19 |
|
$object = $invocation->getThis(); |
45
|
19 |
|
$args = $this->fetchMethodArguments($invocation); |
46
|
19 |
|
$scope = $invocation->getMethod()->getDeclaringClass()->name; |
47
|
|
|
|
48
|
19 |
|
$allContracts = $this->fetchAllContracts($invocation); |
49
|
19 |
|
$this->ensureContracts($invocation, $allContracts, $object, $scope, $args); |
50
|
6 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param MethodInvocation $invocation |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
19 |
|
private function fetchAllContracts(MethodInvocation $invocation) |
57
|
|
|
{ |
58
|
19 |
|
$allContracts = $this->fetchParentsContracts($invocation); |
59
|
|
|
|
60
|
19 |
|
foreach ($invocation->getMethod()->getAnnotations() as $annotation) { |
61
|
19 |
|
if ($annotation instanceof Verify) { |
62
|
19 |
|
$allContracts[] = $annotation; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
19 |
|
return array_unique($allContracts); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param MethodInvocation $invocation |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
19 |
|
private function fetchParentsContracts(MethodInvocation $invocation) |
74
|
|
|
{ |
75
|
19 |
|
return $this->methodConditionFetcher->getConditions( |
76
|
19 |
|
$invocation->getMethod()->getDeclaringClass(), |
77
|
19 |
|
$invocation->getMethod()->name |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|