|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: root |
|
5
|
|
|
* Date: 29.07.2016 |
|
6
|
|
|
* Time: 21:38. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsonframework\container\resolver; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\Common\Annotations\Annotation; |
|
11
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
12
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
13
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
14
|
|
|
use samsonframework\container\annotation\Alias; |
|
15
|
|
|
use samsonframework\container\annotation\AutoWire; |
|
16
|
|
|
use samsonframework\container\annotation\Controller; |
|
17
|
|
|
use samsonframework\container\annotation\Inject; |
|
18
|
|
|
use samsonframework\container\annotation\MetadataInterface; |
|
19
|
|
|
use samsonframework\container\annotation\MethodAnnotation; |
|
20
|
|
|
use samsonframework\container\annotation\Scope; |
|
21
|
|
|
use samsonframework\container\annotation\Service; |
|
22
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
|
23
|
|
|
use samsonframework\container\metadata\MethodMetadata; |
|
24
|
|
|
use samsonframework\container\scope\ControllerScope; |
|
25
|
|
|
|
|
26
|
|
|
class AnnotationResolver extends Resolver |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var CachedReader |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $reader; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* AnnotationResolver constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $cachePath Path for storing annotation cache |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __construct($cachePath) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->reader = new CachedReader(new AnnotationReader(), new FilesystemCache($cachePath)); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritDoc} |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function resolve($classData, $identifier = null) |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var \ReflectionClass $classData */ |
|
51
|
|
|
|
|
52
|
|
|
/** @var MetadataInterface[] $classAnnotations Read class annotations */ |
|
53
|
1 |
|
$classAnnotations = $this->reader->getClassAnnotations($classData); |
|
54
|
|
|
|
|
55
|
1 |
|
if ($classAnnotations) { |
|
|
|
|
|
|
56
|
1 |
|
$metadata = new ClassMetadata(); |
|
57
|
1 |
|
$metadata->className = $classData->getName(); |
|
58
|
1 |
|
$metadata->internalId = $identifier ?: uniqid('container', true); |
|
59
|
|
|
|
|
60
|
1 |
|
foreach ($classAnnotations as $annotation) { |
|
61
|
1 |
|
if (class_implements($annotation, MetadataInterface::class)) { |
|
62
|
1 |
|
$annotation->toMetadata($metadata, $classData->getName()); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
if ($annotation instanceof Inject) { |
|
66
|
|
|
$argumentList = $annotation->list['value']; |
|
67
|
|
|
foreach ($argumentList as $name => $serviceName) { |
|
68
|
|
|
$arg = ['service' => $serviceName]; |
|
69
|
|
|
if (is_string($name)) { |
|
70
|
|
|
$metadata->args[$name] = $arg; |
|
71
|
|
|
} else { |
|
72
|
1 |
|
$metadata->args[] = $arg; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (!$metadata->name) { |
|
79
|
1 |
|
$metadata->name = $metadata->internalId; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @var \ReflectionMethod $method */ |
|
84
|
1 |
|
foreach ($classData->getMethods() as $method) { |
|
85
|
|
|
$methodAnnotations = $this->reader->getMethodAnnotations($method); |
|
86
|
|
|
$methodMetadata = new MethodMetadata(); |
|
87
|
|
|
$methodMetadata->name = $method->getName(); |
|
88
|
|
|
$methodMetadata->modifiers = $method->getModifiers(); |
|
89
|
|
|
$methodMetadata->parameters = $method->getParameters(); |
|
90
|
|
|
|
|
91
|
|
|
/** @var MethodAnnotation $methodAnnotation */ |
|
92
|
|
|
foreach ($methodAnnotations as $methodAnnotation) { |
|
93
|
|
|
$methodMetadata->options[$methodAnnotation->getMethodAlias()] = $methodAnnotation->convertToMetadata(); |
|
94
|
|
|
} |
|
95
|
|
|
$metadata->methodsMetadata[$method->getName()] = $methodMetadata; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $metadata; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.