|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Factory\Reflection; |
|
16
|
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Factory\BaseFactory; |
|
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionFile; |
|
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\NamespaceBlock; |
|
20
|
|
|
use Spaark\CompositeUtils\Model\Reflection\UseStatement; |
|
21
|
|
|
use Spaark\CompositeUtils\Service\RawPropertyAccessor; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Builds a ReflectionFile for a given filename |
|
25
|
|
|
*/ |
|
26
|
|
|
class ReflectionFileFactory extends ReflectorFactory |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The filename to parse |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $filename; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ReflectionFile |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $object; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates the ReflectionFileFactory with the given filename |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $filename The filename to parse |
|
44
|
|
|
*/ |
|
45
|
22 |
|
public function __construct(string $filename) |
|
46
|
|
|
{ |
|
47
|
22 |
|
$this->filename = $filename; |
|
48
|
22 |
|
$this->object = new ReflectionFile(); |
|
49
|
22 |
|
$this->accessor = new RawPropertyAccessor($this->object); |
|
50
|
22 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Builds the ReflectionFile from the provided parameters |
|
54
|
|
|
* |
|
55
|
|
|
* @return ReflectionFile |
|
56
|
|
|
*/ |
|
57
|
22 |
|
public function build() |
|
58
|
|
|
{ |
|
59
|
22 |
|
$this->parseFile(); |
|
60
|
|
|
|
|
61
|
22 |
|
return $this->object; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Parses a file to obtain its namespace and use declarations |
|
66
|
|
|
*/ |
|
67
|
22 |
|
private function parseFile() |
|
68
|
|
|
{ |
|
69
|
22 |
|
$tokens = token_get_all(file_get_contents($this->filename)); |
|
70
|
|
|
|
|
71
|
22 |
|
$matching = null; |
|
72
|
22 |
|
$classname = ''; |
|
73
|
22 |
|
$as = ''; |
|
74
|
22 |
|
$currentNS = null; |
|
75
|
|
|
|
|
76
|
22 |
|
foreach ($tokens as $token) |
|
77
|
|
|
{ |
|
78
|
22 |
|
if ($token === ';') |
|
79
|
|
|
{ |
|
80
|
|
|
switch ($matching) |
|
81
|
|
|
{ |
|
82
|
22 |
|
case T_NAMESPACE: |
|
83
|
22 |
|
$ns = new NamespaceBlock($classname); |
|
84
|
22 |
|
$currentNS = new RawPropertyAccessor($ns); |
|
85
|
22 |
|
$this->accessor->getRawValue |
|
86
|
|
|
( |
|
87
|
22 |
|
'namespaces' |
|
88
|
|
|
) |
|
89
|
22 |
|
->add($classname, $ns); |
|
90
|
22 |
|
$currentNS->setRawValue('file', $this->object); |
|
91
|
22 |
|
$matching = null; |
|
92
|
22 |
|
break; |
|
93
|
22 |
|
case T_AS: |
|
94
|
22 |
|
case T_USE: |
|
95
|
22 |
|
if (!$as) |
|
96
|
|
|
{ |
|
97
|
22 |
|
$as = explode('\\', $classname); |
|
98
|
22 |
|
$as = end($as); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
22 |
|
$currentNS->getRawValue |
|
102
|
|
|
( |
|
103
|
22 |
|
'useStatements' |
|
104
|
|
|
) |
|
105
|
22 |
|
->add($as, new UseStatement($classname, $as)); |
|
106
|
22 |
|
$matching = null; |
|
107
|
22 |
|
break; |
|
108
|
|
|
} |
|
109
|
22 |
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
22 |
|
if ($matching === T_AS) |
|
113
|
|
|
{ |
|
114
|
20 |
|
if ($token[0] === T_STRING) |
|
115
|
|
|
{ |
|
116
|
20 |
|
$as .= $token[1]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
22 |
|
elseif ($matching) |
|
120
|
|
|
{ |
|
121
|
22 |
|
switch ($token[0]) |
|
122
|
|
|
{ |
|
123
|
22 |
|
case T_STRING: |
|
124
|
22 |
|
case T_NS_SEPARATOR: |
|
125
|
22 |
|
$classname .= $token[1]; |
|
126
|
22 |
|
break; |
|
127
|
22 |
|
case T_AS: |
|
128
|
22 |
|
$matching = T_AS; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
else |
|
132
|
|
|
{ |
|
133
|
22 |
|
switch ($token[0]) |
|
134
|
|
|
{ |
|
135
|
22 |
|
case T_NAMESPACE: |
|
136
|
22 |
|
case T_USE: |
|
137
|
22 |
|
$as = ''; |
|
138
|
22 |
|
$classname = ''; |
|
139
|
22 |
|
$matching = $token[0]; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
22 |
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|