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
|
|
|
namespace phpDocumentor\Reflection\Php; |
14
|
|
|
|
15
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
16
|
|
|
use phpDocumentor\Reflection\Exception; |
17
|
|
|
use phpDocumentor\Reflection\Fqsen; |
18
|
|
|
use phpDocumentor\Reflection\PrettyPrinter; |
19
|
|
|
use phpDocumentor\Reflection\ProjectFactory as ProjectFactoryInterface; |
20
|
|
|
use phpDocumentor\Reflection\Php\Factory as Factory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Factory class to transform files into a project description. |
24
|
|
|
*/ |
25
|
|
|
final class ProjectFactory implements ProjectFactoryInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ProjectFactoryStrategies[] |
29
|
|
|
*/ |
30
|
|
|
private $strategies; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes the factory with a number of strategies. |
34
|
|
|
* |
35
|
|
|
* @param ProjectFactoryStrategy[] $strategies |
36
|
|
|
*/ |
37
|
|
|
public function __construct($strategies) |
38
|
|
|
{ |
39
|
|
|
$this->strategies = new ProjectFactoryStrategies($strategies); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a new instance of this factory. With all default strategies. |
44
|
|
|
* |
45
|
|
|
* @return static; |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public static function createInstance() |
48
|
|
|
{ |
49
|
|
|
return new static( |
50
|
|
|
[ |
51
|
|
|
new Factory\Argument(new PrettyPrinter()), |
52
|
|
|
new Factory\Class_(), |
53
|
|
|
new Factory\Constant(new PrettyPrinter()), |
54
|
|
|
new Factory\DocBlock(DocBlockFactory::createInstance()), |
55
|
|
|
new Factory\File(NodesFactory::createInstance()), |
56
|
|
|
new Factory\Function_(), |
57
|
|
|
new Factory\Interface_(), |
58
|
|
|
new Factory\Method(), |
59
|
|
|
new Factory\Property(new PrettyPrinter()), |
60
|
|
|
new Factory\Trait_(), |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates a project from the set of files. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @param \phpDocumentor\Reflection\File[] $files |
70
|
|
|
* @return Project |
71
|
|
|
* @throws Exception when no matching strategy was found. |
72
|
|
|
*/ |
73
|
9 |
|
public function create($name, array $files) |
74
|
|
|
{ |
75
|
9 |
|
$project = new Project($name); |
76
|
|
|
|
77
|
9 |
|
foreach ($files as $filePath) { |
78
|
9 |
|
$strategy = $this->strategies->findMatching($filePath); |
|
|
|
|
79
|
8 |
|
$file = $strategy->create($filePath, $this->strategies); |
80
|
8 |
|
if ($file !== null) { |
81
|
8 |
|
$project->addFile($file); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
$this->buildNamespaces($project); |
86
|
|
|
|
87
|
8 |
|
return $project; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Builds the namespace tree with all elements in the project. |
92
|
|
|
* |
93
|
|
|
* @param Project $project |
94
|
|
|
*/ |
95
|
8 |
|
private function buildNamespaces(Project $project) |
96
|
|
|
{ |
97
|
8 |
|
foreach ($project->getFiles() as $file) { |
98
|
8 |
|
foreach ($file->getNamespaces() as $namespaceFqsen) { |
99
|
7 |
|
$namespace = $this->getNamespaceByName($project, (string)$namespaceFqsen); |
100
|
8 |
|
$this->buildNamespace($file, $namespace); |
101
|
|
|
} |
102
|
|
|
} |
103
|
8 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Gets Namespace from the project if it exists, otherwise returns a new namepace |
107
|
|
|
* |
108
|
|
|
* @param Project $project |
109
|
|
|
* @param $name |
110
|
|
|
* @return Namespace_ |
111
|
|
|
*/ |
112
|
7 |
|
private function getNamespaceByName(Project $project, $name) |
113
|
|
|
{ |
114
|
7 |
|
$existingNamespaces = $project->getNamespaces(); |
115
|
|
|
|
116
|
7 |
|
if (isset($existingNamespaces[$name])) { |
117
|
1 |
|
return $existingNamespaces[$name]; |
118
|
|
|
} |
119
|
|
|
|
120
|
7 |
|
$namespace = new Namespace_(new Fqsen($name)); |
121
|
7 |
|
$project->addNamespace($namespace); |
122
|
7 |
|
return $namespace; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Adds all elements belonging to the namespace to the namespace. |
127
|
|
|
* |
128
|
|
|
* @param File $file |
129
|
|
|
* @param Namespace_ $namespace |
130
|
|
|
*/ |
131
|
7 |
|
private function buildNamespace(File $file, Namespace_ $namespace) |
|
|
|
|
132
|
|
|
{ |
133
|
7 |
|
foreach ($file->getClasses() as $class) { |
134
|
3 |
|
if ($namespace->getFqsen() . '\\' . $class->getName() == $class->getFqsen()) { |
135
|
3 |
|
$namespace->addClass($class->getFqsen()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
7 |
|
foreach ($file->getInterfaces() as $interface) { |
140
|
1 |
|
if ($namespace->getFqsen() . '\\' . $interface->getName() == $interface->getFqsen()) { |
141
|
1 |
|
$namespace->addInterface($interface->getFqsen()); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
foreach ($file->getFunctions() as $function) { |
146
|
1 |
|
if ($namespace->getFqsen() . '\\' . $function->getName() . '()' == $function->getFqsen()) { |
147
|
1 |
|
$namespace->addFunction($function->getFqsen()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
foreach ($file->getConstants() as $constant) { |
152
|
1 |
|
if ($namespace->getFqsen() . '::' . $constant->getName() == $constant->getFqsen()) { |
153
|
1 |
|
$namespace->addConstant($constant->getFqsen()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
foreach ($file->getTraits() as $trait) { |
158
|
1 |
|
if ($namespace->getFqsen() . '\\' . $trait->getName() == $trait->getFqsen()) { |
159
|
1 |
|
$namespace->addTrait($trait->getFqsen()); |
160
|
|
|
} |
161
|
|
|
} |
162
|
7 |
|
} |
163
|
|
|
} |
164
|
|
|
|