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