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