1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Parser Reflection API |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2015, 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 Go\ParserReflection; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use PhpParser\Node; |
15
|
|
|
use PhpParser\Node\Name\FullyQualified; |
16
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* AST-based reflector for the source file |
20
|
|
|
*/ |
21
|
|
|
class ReflectionFile |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Name of the file for reflectino |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $fileName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* List of namespaces in the file |
33
|
|
|
* |
34
|
|
|
* @var ReflectionFileNamespace[]|array |
35
|
|
|
*/ |
36
|
|
|
protected $fileNamespaces; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Top-level nodes for the file |
40
|
|
|
* |
41
|
|
|
* @var Node[] |
42
|
|
|
*/ |
43
|
|
|
private $topLevelNodes; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ReflectionFile constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $fileName Name of the file to reflect |
49
|
|
|
* @param null|array|Node[] $topLevelNodes Optional corresponding list of AST nodes for that file |
50
|
|
|
*/ |
51
|
137 |
|
public function __construct($fileName, $topLevelNodes = null) |
52
|
|
|
{ |
53
|
137 |
|
$this->fileName = $fileName; |
54
|
137 |
|
$this->topLevelNodes = $topLevelNodes ?: ReflectionEngine::parseFile($fileName); |
55
|
137 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a namespace from the file or false if no such a namespace |
59
|
|
|
* |
60
|
|
|
* @param string $namespaceName |
61
|
|
|
* |
62
|
|
|
* @return bool|ReflectionFileNamespace |
63
|
|
|
*/ |
64
|
130 |
|
public function getFileNamespace($namespaceName) |
65
|
|
|
{ |
66
|
130 |
|
if ($this->hasFileNamespace($namespaceName)) { |
67
|
130 |
|
return $this->fileNamespaces[$namespaceName]; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the list of namespaces in the file |
75
|
|
|
* |
76
|
|
|
* @return array|ReflectionFileNamespace[] |
77
|
|
|
*/ |
78
|
134 |
|
public function getFileNamespaces() |
79
|
|
|
{ |
80
|
134 |
|
if (!isset($this->fileNamespaces)) { |
81
|
134 |
|
$this->fileNamespaces = $this->findFileNamespaces(); |
82
|
134 |
|
} |
83
|
|
|
|
84
|
134 |
|
return $this->fileNamespaces; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns the name of current reflected file |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
public function getName() |
93
|
|
|
{ |
94
|
1 |
|
return $this->fileName; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the presence of namespace in the file |
99
|
|
|
* |
100
|
|
|
* @param string $namespaceName Namespace to check |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
131 |
|
public function hasFileNamespace($namespaceName) |
105
|
|
|
{ |
106
|
131 |
|
$namespaces = $this->getFileNamespaces(); |
107
|
|
|
|
108
|
131 |
|
return isset($namespaces[$namespaceName]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Searches for file namespaces in the given AST |
113
|
|
|
* |
114
|
|
|
* @return array|ReflectionFileNamespace[] |
115
|
|
|
*/ |
116
|
134 |
|
private function findFileNamespaces() |
117
|
|
|
{ |
118
|
134 |
|
$namespaces = array(); |
119
|
|
|
|
120
|
|
|
// namespaces can be only top-level nodes, so we can scan them directly |
121
|
134 |
|
foreach ($this->topLevelNodes as $topLevelNode) { |
122
|
134 |
|
if ($topLevelNode instanceof Namespace_) { |
123
|
133 |
|
$namespaceName = $topLevelNode->name ? $topLevelNode->name->toString() : '\\'; |
124
|
|
|
|
125
|
133 |
|
$namespaces[$namespaceName] = new ReflectionFileNamespace( |
126
|
133 |
|
$this->fileName, |
127
|
133 |
|
$namespaceName, |
128
|
|
|
$topLevelNode |
129
|
133 |
|
); |
130
|
133 |
|
} |
131
|
134 |
|
} |
132
|
|
|
|
133
|
134 |
|
if (!$namespaces) { |
134
|
|
|
// if we don't have a namespaces at all, this is global namespace |
135
|
1 |
|
$globalNamespaceNode = new Namespace_(new FullyQualified(''), $this->topLevelNodes); |
136
|
1 |
|
$namespaces['\\'] = new ReflectionFileNamespace($this->fileName, '\\', $globalNamespaceNode); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
134 |
|
return $namespaces; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|