1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use phpDocumentor\Reflection\Fqsen; |
18
|
|
|
use phpDocumentor\Reflection\Middleware\ChainFactory; |
19
|
|
|
use phpDocumentor\Reflection\Middleware\Middleware; |
20
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\Adapter; |
21
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand; |
22
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\LocalAdapter; |
23
|
|
|
use phpDocumentor\Reflection\Php\File as FileElement; |
24
|
|
|
use phpDocumentor\Reflection\Php\NodesFactory; |
25
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
26
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
27
|
|
|
use phpDocumentor\Reflection\Types\Context; |
28
|
|
|
use phpDocumentor\Reflection\Types\ContextFactory; |
29
|
|
|
use PhpParser\Comment\Doc; |
30
|
|
|
use PhpParser\Lexer; |
31
|
|
|
use PhpParser\Node; |
32
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
33
|
|
|
use PhpParser\Node\Stmt\Function_ as FunctionNode; |
34
|
|
|
use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
35
|
|
|
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode; |
36
|
|
|
use PhpParser\Node\Stmt\Trait_ as TraitNode; |
37
|
|
|
use PhpParser\NodeAbstract; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Strategy to create File element from the provided filename. |
41
|
|
|
* This class supports extra middle wares to add extra steps to the creation process. |
42
|
|
|
*/ |
43
|
|
|
final class File implements ProjectFactoryStrategy |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var NodesFactory |
47
|
|
|
*/ |
48
|
|
|
private $nodesFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Adapter |
52
|
|
|
*/ |
53
|
|
|
private $adapter; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initializes the object. |
57
|
|
|
* |
58
|
|
|
* @param NodesFactory $nodesFactory |
59
|
|
|
* @param Adapter $adapter |
60
|
|
|
* @param Middleware[] $middleware |
61
|
|
|
*/ |
62
|
9 |
|
public function __construct(NodesFactory $nodesFactory, Adapter $adapter = null, $middleware = array()) |
63
|
|
|
{ |
64
|
9 |
|
if ($adapter === null) { |
65
|
9 |
|
$adapter = new LocalAdapter(); |
66
|
|
|
} |
67
|
|
|
|
68
|
9 |
|
$this->nodesFactory = $nodesFactory; |
69
|
9 |
|
$this->adapter = $adapter; |
70
|
|
|
|
71
|
9 |
|
$lastCallable = function ($command) { |
72
|
5 |
|
return $this->createFile($command); |
73
|
9 |
|
}; |
74
|
|
|
|
75
|
9 |
|
$this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable); |
|
|
|
|
76
|
9 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns true when the strategy is able to handle the object. |
80
|
|
|
* |
81
|
|
|
* @param string $filePath path to check. |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
1 |
|
public function matches($filePath) |
85
|
|
|
{ |
86
|
1 |
|
return is_string($filePath) && $this->adapter->fileExists($filePath); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Creates an File out of the given object. |
91
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
92
|
|
|
* used to create nested Elements. |
93
|
|
|
* |
94
|
|
|
* @param string $object path to the file to convert to an File object. |
95
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
96
|
|
|
* @param Context $context |
97
|
|
|
* @return File |
98
|
|
|
*/ |
99
|
7 |
|
public function create($object, StrategyContainer $strategies, Context $context = null) |
100
|
|
|
{ |
101
|
7 |
|
if (!$this->matches($object)) { |
102
|
1 |
|
throw new InvalidArgumentException( |
103
|
1 |
|
sprintf('%s cannot handle objects with the type %s', |
104
|
1 |
|
__CLASS__, |
105
|
1 |
|
is_object($object) ? get_class($object) : gettype($object) |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
$command = new CreateCommand($this->adapter, $object, $strategies); |
111
|
6 |
|
$middlewareChain = $this->middlewareChain; |
112
|
|
|
|
113
|
6 |
|
return $middlewareChain($command); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $object |
118
|
|
|
* @param StrategyContainer $strategies |
|
|
|
|
119
|
|
|
* @return FileElement |
120
|
|
|
*/ |
121
|
5 |
|
private function createFile(CreateCommand $command) |
122
|
|
|
{ |
123
|
5 |
|
$code = $this->adapter->getContents($command->getFilePath()); |
124
|
5 |
|
$nodes = $this->nodesFactory->create($code); |
125
|
5 |
|
$docBlock = $this->createDocBlock($command->getStrategies(), $code, $nodes); |
126
|
|
|
|
127
|
5 |
|
$file = new FileElement( |
128
|
5 |
|
$this->adapter->md5($command->getFilePath()), |
129
|
5 |
|
$this->adapter->path($command->getFilePath()), |
130
|
|
|
$code, |
131
|
|
|
$docBlock |
|
|
|
|
132
|
|
|
); |
133
|
|
|
|
134
|
5 |
|
$this->createElements(new Fqsen('\\'), $nodes, $file, $command->getStrategies()); |
135
|
|
|
|
136
|
5 |
|
return $file; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param Fqsen $namespace |
141
|
|
|
* @param Node[] $nodes |
142
|
|
|
* @param FileElement $file |
143
|
|
|
* @param StrategyContainer $strategies |
144
|
|
|
*/ |
145
|
5 |
|
private function createElements(Fqsen $namespace, $nodes, FileElement $file, StrategyContainer $strategies) |
146
|
|
|
{ |
147
|
5 |
|
$contextFactory = new ContextFactory(); |
148
|
5 |
|
$context = $contextFactory->createForNamespace((string)$namespace, $file->getSource()); |
149
|
5 |
|
foreach ($nodes as $node) { |
150
|
5 |
|
switch (get_class($node)) { |
151
|
|
|
case ClassNode::class: |
152
|
1 |
|
$strategy = $strategies->findMatching($node); |
153
|
1 |
|
$class = $strategy->create($node, $strategies, $context); |
154
|
1 |
|
$file->addClass($class); |
|
|
|
|
155
|
1 |
|
break; |
156
|
|
|
case FunctionNode::class: |
157
|
1 |
|
$strategy = $strategies->findMatching($node); |
158
|
1 |
|
$function = $strategy->create($node, $strategies, $context); |
159
|
1 |
|
$file->addFunction($function); |
|
|
|
|
160
|
1 |
|
break; |
161
|
|
|
case InterfaceNode::class: |
162
|
1 |
|
$strategy = $strategies->findMatching($node); |
163
|
1 |
|
$interface = $strategy->create($node, $strategies, $context); |
164
|
1 |
|
$file->addInterface($interface); |
|
|
|
|
165
|
1 |
|
break; |
166
|
|
|
case NamespaceNode::class: |
167
|
1 |
|
$file->addNamespace($node->fqsen); |
|
|
|
|
168
|
1 |
|
$this->createElements($node->fqsen, $node->stmts, $file, $strategies); |
|
|
|
|
169
|
1 |
|
break; |
170
|
|
|
case TraitNode::class: |
171
|
1 |
|
$strategy = $strategies->findMatching($node); |
172
|
1 |
|
$trait = $strategy->create($node, $strategies, $context); |
173
|
1 |
|
$file->addTrait($trait); |
|
|
|
|
174
|
5 |
|
break; |
175
|
|
|
} |
176
|
|
|
} |
177
|
5 |
|
} |
178
|
|
|
/** |
179
|
|
|
* @param StrategyContainer $strategies |
180
|
|
|
* @param $code |
181
|
|
|
* @param $nodes |
182
|
|
|
* @return null|\phpDocumentor\Reflection\Element |
183
|
|
|
* @internal param Context $context |
184
|
|
|
*/ |
185
|
5 |
|
private function createDocBlock(StrategyContainer $strategies, $code, $nodes) |
186
|
|
|
{ |
187
|
5 |
|
$contextFactory = new ContextFactory(); |
188
|
5 |
|
$context = $contextFactory->createForNamespace('\\', $code); |
189
|
5 |
|
$docBlock = null; |
190
|
|
|
|
191
|
|
|
/** @var NodeAbstract $node */ |
192
|
5 |
|
$node = current($nodes); |
193
|
5 |
|
if ($node instanceof Node) { |
194
|
5 |
|
$comments = $node->getAttribute('comments'); |
195
|
5 |
|
if (is_array($comments)) { |
196
|
|
|
if ($node instanceof NamespaceNode) { |
197
|
|
|
$strategy = $strategies->findMatching(current($comments)); |
198
|
|
|
$docBlock = $strategy->create(current($comments), $strategies, $context); |
199
|
|
|
} elseif (count($comments) == 2) { |
200
|
|
|
$strategy = $strategies->findMatching(current($comments)); |
201
|
|
|
$docBlock = $strategy->create(current($comments), $strategies, $context); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
5 |
|
return $docBlock; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|