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