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 as FileCreateCommand; |
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
|
|
|
private $fileMiddleware; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initializes the object. |
53
|
|
|
* |
54
|
|
|
* @param NodesFactory $nodesFactory |
55
|
|
|
* @param Middleware[] $middleware |
56
|
|
|
*/ |
57
|
9 |
|
public function __construct(NodesFactory $nodesFactory, $middleware = array()) |
58
|
|
|
{ |
59
|
9 |
|
parent::__construct(); |
60
|
9 |
|
$this->nodesFactory = $nodesFactory; |
61
|
|
|
|
62
|
9 |
|
$lastCallable = function ($command) { |
63
|
5 |
|
return $this->createFile($command); |
64
|
9 |
|
}; |
65
|
|
|
|
66
|
9 |
|
$this->fileMiddleware = ChainFactory::createExecutionChain($middleware, $lastCallable); |
67
|
9 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns true when the strategy is able to handle the object. |
71
|
|
|
* |
72
|
|
|
* @param string $file path to check. |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
1 |
|
public function matches($file) |
76
|
|
|
{ |
77
|
1 |
|
return $file instanceof FileSystemFile; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Creates an File out of the given object. |
82
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
83
|
|
|
* used to create nested Elements. |
84
|
|
|
* |
85
|
|
|
* @param FileSystemFile $object path to the file to convert to an File object. |
86
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
87
|
|
|
* @param Context $context |
88
|
|
|
* @return File |
89
|
|
|
*/ |
90
|
6 |
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
91
|
|
|
{ |
92
|
6 |
|
$command = new FileCreateCommand($object, $strategies); |
93
|
6 |
|
$middlewareChain = $this->fileMiddleware; |
94
|
|
|
|
95
|
6 |
|
return $middlewareChain($command); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param CreateCommand $command |
100
|
|
|
* @return FileElement |
101
|
|
|
*/ |
102
|
5 |
|
private function createFile(FileCreateCommand $command) |
103
|
|
|
{ |
104
|
5 |
|
$file = $command->getFile(); |
105
|
5 |
|
$code = $file->getContents(); |
106
|
5 |
|
$nodes = $this->nodesFactory->create($code); |
107
|
|
|
|
108
|
5 |
|
$contextFactory = new ContextFactory(); |
109
|
5 |
|
$context = $contextFactory->createForNamespace('\\', $code); |
110
|
|
|
|
111
|
5 |
|
$docBlock = $this->createFileDocBlock(null, $command->getStrategies(), $context, $nodes); |
112
|
|
|
|
113
|
5 |
|
$result = new FileElement( |
114
|
5 |
|
$file->md5(), |
115
|
5 |
|
$file->path(), |
116
|
5 |
|
$code, |
117
|
|
|
$docBlock |
|
|
|
|
118
|
5 |
|
); |
119
|
|
|
|
120
|
5 |
|
$this->createElements(new Fqsen('\\'), $nodes, $result, $command->getStrategies()); |
121
|
|
|
|
122
|
5 |
|
return $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param Fqsen $namespace |
127
|
|
|
* @param Node[] $nodes |
128
|
|
|
* @param FileElement $file |
129
|
|
|
* @param StrategyContainer $strategies |
130
|
|
|
*/ |
131
|
5 |
|
private function createElements(Fqsen $namespace, $nodes, FileElement $file, StrategyContainer $strategies) |
132
|
|
|
{ |
133
|
5 |
|
$contextFactory = new ContextFactory(); |
134
|
5 |
|
$context = $contextFactory->createForNamespace((string)$namespace, $file->getSource()); |
135
|
5 |
|
foreach ($nodes as $node) { |
136
|
5 |
|
switch (get_class($node)) { |
137
|
5 |
|
case ClassNode::class: |
138
|
1 |
|
$strategy = $strategies->findMatching($node); |
139
|
1 |
|
$class = $strategy->create($node, $strategies, $context); |
140
|
1 |
|
$file->addClass($class); |
|
|
|
|
141
|
1 |
|
break; |
142
|
4 |
|
case FunctionNode::class: |
143
|
1 |
|
$strategy = $strategies->findMatching($node); |
144
|
1 |
|
$function = $strategy->create($node, $strategies, $context); |
145
|
1 |
|
$file->addFunction($function); |
|
|
|
|
146
|
1 |
|
break; |
147
|
3 |
|
case InterfaceNode::class: |
148
|
1 |
|
$strategy = $strategies->findMatching($node); |
149
|
1 |
|
$interface = $strategy->create($node, $strategies, $context); |
150
|
1 |
|
$file->addInterface($interface); |
|
|
|
|
151
|
1 |
|
break; |
152
|
2 |
|
case NamespaceNode::class: |
153
|
1 |
|
$file->addNamespace($node->fqsen); |
|
|
|
|
154
|
1 |
|
$this->createElements($node->fqsen, $node->stmts, $file, $strategies); |
|
|
|
|
155
|
1 |
|
break; |
156
|
1 |
|
case TraitNode::class: |
157
|
1 |
|
$strategy = $strategies->findMatching($node); |
158
|
1 |
|
$trait = $strategy->create($node, $strategies, $context); |
159
|
1 |
|
$file->addTrait($trait); |
|
|
|
|
160
|
1 |
|
break; |
161
|
5 |
|
} |
162
|
5 |
|
} |
163
|
5 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param Doc $docBlock |
167
|
|
|
* @param StrategyContainer $strategies |
168
|
|
|
* @param Context $context |
169
|
|
|
* @param Node[] $nodes |
170
|
|
|
* @return null|\phpDocumentor\Reflection\Element |
171
|
|
|
*/ |
172
|
5 |
|
protected function createFileDocBlock( |
173
|
|
|
Doc $docBlock = null, |
174
|
|
|
StrategyContainer $strategies = null, |
175
|
|
|
Context $context = null, |
176
|
1 |
|
$nodes = array() |
177
|
|
|
) { |
178
|
|
|
/** @var NodeAbstract $node */ |
179
|
5 |
|
$node = current($nodes); |
180
|
5 |
|
if ($node instanceof Node) { |
181
|
5 |
|
$comments = $node->getAttribute('comments'); |
182
|
5 |
|
if (is_array($comments)) { |
183
|
|
|
if ($node instanceof NamespaceNode) { |
184
|
|
|
$docBlock = $this->createDocBlock($strategies, current($comments), $context); |
|
|
|
|
185
|
|
|
} elseif (count($comments) == 2) { |
186
|
|
|
$docBlock = $this->createDocBlock($strategies, current($comments), $context); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
5 |
|
} |
190
|
|
|
|
191
|
5 |
|
return $docBlock; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|