1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* phpDocumentor |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com) |
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
9
|
|
|
* @link http://phpdoc.org |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace phpDocumentor\Compiler\Pass; |
13
|
|
|
|
14
|
|
|
use phpDocumentor\Compiler\CompilerPassInterface; |
15
|
|
|
use phpDocumentor\Descriptor\Collection; |
16
|
|
|
use phpDocumentor\Descriptor\DescriptorAbstract; |
17
|
|
|
use phpDocumentor\Descriptor\PackageDescriptor; |
18
|
|
|
use phpDocumentor\Descriptor\ProjectDescriptor; |
19
|
|
|
use phpDocumentor\Descriptor\TagDescriptor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Rebuilds the package tree from the elements found in files. |
23
|
|
|
* |
24
|
|
|
* On every compiler pass is the package tree rebuild to aid in the process |
25
|
|
|
* of incremental updates. |
26
|
|
|
* |
27
|
|
|
* If the package tree were to be persisted then both locations needed to be |
28
|
|
|
* invalidated if a file were to change. |
29
|
|
|
*/ |
30
|
|
|
class PackageTreeBuilder implements CompilerPassInterface |
31
|
|
|
{ |
32
|
|
|
const COMPILER_PRIORITY = 9001; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function getDescription() |
38
|
|
|
{ |
39
|
|
|
return 'Build "packages" index'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Compiles a 'packages' index on the project and create all Package Descriptors necessary. |
44
|
|
|
* |
45
|
|
|
* @param ProjectDescriptor $project |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function execute(ProjectDescriptor $project) |
50
|
|
|
{ |
51
|
|
|
$rootPackageDescriptor = new PackageDescriptor(); |
|
|
|
|
52
|
|
|
$rootPackageDescriptor->setName('\\'); |
53
|
|
|
$project->getIndexes()->set('packages', new Collection()); |
54
|
|
|
$project->getIndexes()->packages['\\'] = $rootPackageDescriptor; |
|
|
|
|
55
|
|
|
|
56
|
|
|
foreach ($project->getFiles() as $file) { |
57
|
|
|
$this->addElementsOfTypeToPackage($project, array($file), 'files'); |
58
|
|
|
$this->addElementsOfTypeToPackage($project, $file->getConstants()->getAll(), 'constants'); |
59
|
|
|
$this->addElementsOfTypeToPackage($project, $file->getFunctions()->getAll(), 'functions'); |
60
|
|
|
$this->addElementsOfTypeToPackage($project, $file->getClasses()->getAll(), 'classes'); |
61
|
|
|
$this->addElementsOfTypeToPackage($project, $file->getInterfaces()->getAll(), 'interfaces'); |
62
|
|
|
$this->addElementsOfTypeToPackage($project, $file->getTraits()->getAll(), 'traits'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds the given elements of a specific type to their respective Package Descriptors. |
68
|
|
|
* |
69
|
|
|
* This method will assign the given elements to the package as registered in the package field of that |
70
|
|
|
* element. If a package does not exist yet it will automatically be created. |
71
|
|
|
* |
72
|
|
|
* @param ProjectDescriptor $project |
73
|
|
|
* @param DescriptorAbstract[] $elements Series of elements to add to their respective package. |
74
|
|
|
* @param string $type Declares which field of the package will be populated with the given |
75
|
|
|
* series of elements. This name will be transformed to a getter which must exist. Out of performance |
76
|
|
|
* considerations will no effort be done to verify whether the provided type is valid. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
protected function addElementsOfTypeToPackage(ProjectDescriptor $project, array $elements, $type) |
81
|
|
|
{ |
82
|
|
|
/** @var DescriptorAbstract $element */ |
83
|
|
|
foreach ($elements as $element) { |
84
|
|
|
$packageName = ''; |
85
|
|
|
$packageTags = $element->getTags()->get('package'); |
86
|
|
|
if ($packageTags instanceof Collection) { |
87
|
|
|
$packageTag = $packageTags->getIterator()->current(); |
|
|
|
|
88
|
|
|
if ($packageTag instanceof TagDescriptor) { |
89
|
|
|
$packageName = $packageTag->getDescription(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$subpackageCollection = $element->getTags()->get('subpackage'); |
94
|
|
|
if ($subpackageCollection instanceof Collection && $subpackageCollection->count() > 0) { |
95
|
|
|
$subpackageTag = $subpackageCollection->getIterator()->current(); |
|
|
|
|
96
|
|
|
if ($subpackageTag instanceof TagDescriptor) { |
97
|
|
|
$packageName .= '\\' . $subpackageTag->getDescription(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// ensure consistency by trimming the slash prefix and then re-appending it. |
102
|
|
|
$packageIndexName = '\\' . ltrim($packageName, '\\'); |
103
|
|
|
if (!isset($project->getIndexes()->packages[$packageIndexName])) { |
|
|
|
|
104
|
|
|
$this->createPackageDescriptorTree($project, $packageName); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @var PackageDescriptor $package */ |
108
|
|
|
$package = $project->getIndexes()->packages[$packageIndexName]; |
|
|
|
|
109
|
|
|
|
110
|
|
|
// replace textual representation with an object representation |
111
|
|
|
$element->setPackage($package); |
112
|
|
|
|
113
|
|
|
// add element to package |
114
|
|
|
$getter = 'get'.ucfirst($type); |
115
|
|
|
|
116
|
|
|
/** @var Collection $collection */ |
117
|
|
|
$collection = $package->$getter(); |
118
|
|
|
$collection->add($element); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Creates a tree of PackageDescriptors based on the provided FQNN (package name). |
124
|
|
|
* |
125
|
|
|
* This method will examine the package name and create a package descriptor for each part of |
126
|
|
|
* the FQNN if it doesn't exist in the packages field of the current package (starting with the root |
127
|
|
|
* Package in the Project Descriptor), |
128
|
|
|
* |
129
|
|
|
* As an intended side effect this method also populates the *elements* index of the ProjectDescriptor with all |
130
|
|
|
* created PackageDescriptors. Each index key is prefixed with a tilde (~) so that it will not conflict with |
131
|
|
|
* other FQSEN's, such as classes or interfaces. |
132
|
|
|
* |
133
|
|
|
* @param ProjectDescriptor $project |
134
|
|
|
* @param string $packageName A FQNN of the package (and parents) to create. |
135
|
|
|
* |
136
|
|
|
* @see ProjectDescriptor::getPackage() for the root package. |
137
|
|
|
* @see PackageDescriptor::getChildren() for the child packages of a given package. |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
protected function createPackageDescriptorTree(ProjectDescriptor $project, $packageName) |
142
|
|
|
{ |
143
|
|
|
$parts = explode('\\', ltrim($packageName, '\\')); |
144
|
|
|
$fqnn = ''; |
145
|
|
|
|
146
|
|
|
// this method does not use recursion to traverse the tree but uses a pointer that will be overridden with the |
147
|
|
|
// next item that is to be traversed (child package) at the end of the loop. |
148
|
|
|
|
149
|
|
|
/** @var PackageDescriptor $pointer */ |
150
|
|
|
$pointer = $project->getIndexes()->packages['\\']; |
|
|
|
|
151
|
|
|
foreach ($parts as $part) { |
152
|
|
|
$fqnn .= '\\' . $part; |
153
|
|
|
if ($pointer->getChildren()->get($part)) { |
154
|
|
|
$pointer = $pointer->getChildren()->get($part); |
155
|
|
|
continue; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// package does not exist, create it |
159
|
|
|
$interimPackageDescriptor = new PackageDescriptor(); |
|
|
|
|
160
|
|
|
$interimPackageDescriptor->setParent($pointer); |
161
|
|
|
$interimPackageDescriptor->setName($part); |
162
|
|
|
$interimPackageDescriptor->setFullyQualifiedStructuralElementName($fqnn); |
163
|
|
|
|
164
|
|
|
// add to the pointer's list of children |
165
|
|
|
$pointer->getChildren()->set($part ?: 'UNKNOWN', $interimPackageDescriptor); |
166
|
|
|
|
167
|
|
|
// add to index |
168
|
|
|
$project->getIndexes()->packages[$fqnn] = $interimPackageDescriptor; |
|
|
|
|
169
|
|
|
|
170
|
|
|
// move pointer forward |
171
|
|
|
$pointer = $interimPackageDescriptor; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.