1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
14
|
|
|
* all copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Annotation; |
26
|
|
|
|
27
|
|
|
use Brickoo\Component\Annotation\Exception\FileDoesNotExistException; |
28
|
|
|
use Brickoo\Component\Annotation\Exception\UnableToLocateQualifiedClassNameException; |
29
|
|
|
use Brickoo\Component\Common\Collection; |
30
|
|
|
use Brickoo\Component\Common\Assert; |
31
|
|
|
use ReflectionClass; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* AnnotationClassFileReader |
35
|
|
|
* |
36
|
|
|
* Implements an annotation reader for class files. |
37
|
|
|
* @author Celestino Diaz <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class AnnotationClassFileReader { |
40
|
|
|
|
41
|
|
|
/** @var \Brickoo\Component\Annotation\AnnotationReflectionClassReader */ |
42
|
|
|
private $annotationReflectionReader; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class constructor. |
46
|
|
|
* @param \Brickoo\Component\Annotation\AnnotationReflectionClassReader $annotationReflectionReader |
47
|
|
|
*/ |
48
|
2 |
|
public function __construct(AnnotationReflectionClassReader $annotationReflectionReader) { |
49
|
2 |
|
$this->annotationReflectionReader = $annotationReflectionReader; |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* @param \Brickoo\Component\Common\Collection $collection |
55
|
|
|
* @param string $filename |
56
|
|
|
* @throws \Brickoo\Component\Annotation\Exception\FileDoesNotExistException |
57
|
|
|
* @throws \Brickoo\Component\Annotation\Exception\UnableToLocateQualifiedClassNameException |
58
|
|
|
* @return \Brickoo\Component\Annotation\AnnotationReaderResult |
59
|
|
|
*/ |
60
|
4 |
|
public function getAnnotations(Collection $collection, $filename) { |
61
|
4 |
|
Assert::isString($filename); |
62
|
4 |
|
$reflectionClass = $this->getReflectionClass($filename); |
63
|
2 |
|
return $this->annotationReflectionReader->getAnnotations($collection, $reflectionClass); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the corresponding reflection class. |
68
|
|
|
* @param string $filename |
69
|
|
|
* @throws \Brickoo\Component\Annotation\Exception\UnableToLocateQualifiedClassNameException |
70
|
|
|
* @return \ReflectionClass |
71
|
|
|
*/ |
72
|
3 |
|
private function getReflectionClass($filename) { |
73
|
3 |
|
$this->checkFileAvailability($filename); |
74
|
|
|
|
75
|
3 |
|
$fileContent = file_get_contents($filename); |
76
|
3 |
|
$qualifiedName = sprintf("%s%s", |
77
|
3 |
|
$this->getNamespace($fileContent), |
78
|
3 |
|
$this->getClassName($fileContent) |
79
|
3 |
|
); |
80
|
|
|
|
81
|
|
|
|
82
|
3 |
|
if (preg_match("~^[\\\\][a-zA-Z_\x7f-\xff][\\\\\\w\x7f-\xff]+$~", $qualifiedName) == 0) { |
83
|
1 |
|
throw new UnableToLocateQualifiedClassNameException($filename); |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
include $filename; |
87
|
2 |
|
return new ReflectionClass($qualifiedName); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Checks if the file exists and is readable. |
92
|
|
|
* @param string $filename |
93
|
|
|
* @throws \Brickoo\Component\Annotation\Exception\FileDoesNotExistException |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
3 |
|
private function checkFileAvailability($filename) { |
97
|
3 |
|
if (!is_readable($filename)) { |
98
|
1 |
|
throw new FileDoesNotExistException($filename); |
99
|
|
|
} |
100
|
2 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the file namespace. |
104
|
|
|
* @param string $fileContent |
105
|
|
|
* @return string the file namespace |
106
|
|
|
*/ |
107
|
2 |
|
private function getNamespace($fileContent) { |
108
|
2 |
|
$matches = null; |
109
|
2 |
|
$namespace = "\\"; |
110
|
2 |
|
preg_match("~namespace\\s+(?<namespace>[a-zA-Z_\x7f-\xff][\\\\\\w\x7f-\xff]+)\\s*[\\;\\{]{1}~i", $fileContent, $matches); |
111
|
2 |
|
if (isset($matches["namespace"]) && (!empty($matches["namespace"]))) { |
112
|
2 |
|
$namespace .= $matches["namespace"]."\\"; |
113
|
2 |
|
} |
114
|
2 |
|
return $namespace; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the class name. |
119
|
|
|
* @param string $fileContent |
120
|
|
|
* @return string the class name |
121
|
|
|
*/ |
122
|
2 |
|
private function getClassName($fileContent) { |
123
|
2 |
|
$matches = null; |
124
|
2 |
|
preg_match("~[\r\n][a-z\\s]*class\\s+(?<class>[a-zA-Z_\x7f-\xff][\\w\x7f-\xff]+)[\\w\\s,]*\\s*\\{~i", $fileContent, $matches); |
125
|
2 |
|
return isset($matches["class"]) ? trim($matches["class"]) : ""; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|