1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of cloak. |
5
|
|
|
* |
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace cloak\reflection; |
13
|
|
|
|
14
|
|
|
use cloak\reflection\collection\ReflectionCollection; |
15
|
|
|
use cloak\value\LineRange; |
16
|
|
|
use cloak\result\FileResult; |
17
|
|
|
use cloak\result\LineResultSelectable; |
18
|
|
|
use PhpCollection\Sequence; |
19
|
|
|
use Zend\Code\Reflection\ClassReflection as ZendClassReflection; |
20
|
|
|
use Zend\Code\Reflection\FileReflection as ZendFileReflection; |
21
|
|
|
use Closure; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class FileReflection |
26
|
|
|
* @package cloak\reflection |
27
|
|
|
*/ |
28
|
|
|
class FileReflection implements Reflection, ResultConvertible |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
private $filename; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Zend\Code\Reflection\FileReflection |
35
|
|
|
*/ |
36
|
|
|
private $reflection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LineRange |
40
|
|
|
*/ |
41
|
|
|
private $lineRange; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $filename |
46
|
|
|
*/ |
47
|
|
|
public function __construct($filename) |
48
|
|
|
{ |
49
|
|
|
$this->filename = $filename; |
50
|
|
|
$this->reflection = new ZendFileReflection($this->filename, true); |
51
|
|
|
|
52
|
|
|
$content = $this->reflection->getContents(); //$fileReflection->getEndLine() return null.... |
53
|
|
|
$totalLineCount = substr_count(trim($content), PHP_EOL) + 1; |
54
|
|
|
|
55
|
|
|
$this->lineRange = new LineRange(1, $totalLineCount); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getIdentityName() |
62
|
|
|
{ |
63
|
|
|
return $this->getName(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getName() |
70
|
|
|
{ |
71
|
|
|
return $this->filename; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return ReflectionCollection |
76
|
|
|
*/ |
77
|
|
|
public function getClasses() |
78
|
|
|
{ |
79
|
|
|
return $this->selectClassReflections(function (ZendClassReflection $reflection) { |
80
|
|
|
return $reflection->isTrait(); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return ReflectionCollection |
86
|
|
|
*/ |
87
|
|
|
public function getTraits() |
88
|
|
|
{ |
89
|
|
|
return $this->selectClassReflections(function (ZendClassReflection $reflection) { |
90
|
|
|
return $reflection->isTrait() === false; |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getLineRange() |
98
|
|
|
{ |
99
|
|
|
return $this->lineRange; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param callable $filter |
104
|
|
|
* @return ReflectionCollection |
105
|
|
|
*/ |
106
|
|
|
private function selectClassReflections(Closure $filter) |
107
|
|
|
{ |
108
|
|
|
$classes = $this->reflection->getClasses(); |
109
|
|
|
|
110
|
|
|
$excludeInterface = function (ZendClassReflection $reflection) { |
111
|
|
|
return $reflection->isInterface() === false; |
112
|
|
|
}; |
113
|
|
|
|
114
|
|
|
$createClassReflection = function (ZendClassReflection $reflection) { |
115
|
|
|
return new ClassReflection($reflection->name); |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
$reflections = new Sequence($classes); |
119
|
|
|
$reflections = $reflections->filter($filter) |
120
|
|
|
->filter($excludeInterface) |
121
|
|
|
->map($createClassReflection); |
122
|
|
|
|
123
|
|
|
return new ReflectionCollection($reflections->all()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function convertToResult(LineResultSelectable $selector) |
130
|
|
|
{ |
131
|
|
|
return new FileResult($this->getName(), $selector); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function __toString() |
138
|
|
|
{ |
139
|
|
|
return $this->getIdentityName(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|