1 | <?php |
||
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) |
|
94 | |||
95 | /** |
||
96 | * @param CreateCommand $command |
||
97 | * @return FileElement |
||
98 | */ |
||
99 | 8 | private function createFile(CreateCommand $command) |
|
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 | 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 | 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 | 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 | case NamespaceNode::class: |
||
150 | 3 | $file->addNamespace($node->fqsen); |
|
151 | 3 | $this->createElements($node->fqsen, $node->stmts, $file, $strategies); |
|
152 | 3 | break; |
|
153 | case TraitNode::class: |
||
154 | 1 | $strategy = $strategies->findMatching($node); |
|
155 | 1 | $trait = $strategy->create($node, $strategies, $context); |
|
156 | 1 | $file->addTrait($trait); |
|
157 | 8 | break; |
|
158 | } |
||
159 | } |
||
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( |
|
210 | } |
||
211 |