-
Notifications
You must be signed in to change notification settings - Fork 2
/
PhpDocumentor2Task.php
309 lines (259 loc) · 8.21 KB
/
PhpDocumentor2Task.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
namespace Phing\Task\Ext\PhpDoc;
use Phing\Exception\BuildException;
use Phing\Io\File;
use Phing\Io\FileSystem;
use Phing\Phing;
use Phing\Project;
use Phing\Task;
use Phing\Type\Element\FileSetAware;
use phpDocumentor\Bootstrap;
use phpDocumentor\Descriptor\Cache\ProjectDescriptorMapper;
use phpDocumentor\Fileset\Collection;
/**
* PhpDocumentor2 Task (http://www.phpdoc.org)
* Based on the DocBlox Task
*
* @author Michiel Rook <[email protected]>
* @since 2.4.10
* @package phing.tasks.ext.phpdoc
*/
class PhpDocumentor2Task extends Task
{
use FileSetAware;
/**
* Destination/target directory
*
* @var File
*/
private $destDir = null;
/**
* name of the template to use
*
* @var string
*/
private $template = "responsive-twig";
/**
* Title of the project
*
* @var string
*/
private $title = "API Documentation";
/**
* Name of default package
*
* @var string
*/
private $defaultPackageName = "Default";
/**
* Path to the phpDocumentor .phar
*
* @var string
*/
private $pharLocation = '';
/**
* Path to the phpDocumentor 2 source
*
* @var string
*/
private $phpDocumentorPath = "";
/**
* @var \phpDocumentor\Application
*/
private $app = null;
/**
* Sets destination/target directory
*
* @param File $destDir
*/
public function setDestDir(File $destDir)
{
$this->destDir = $destDir;
}
/**
* Convenience setter (@see setDestDir)
*
* @param File $output
*/
public function setOutput(File $output)
{
$this->destDir = $output;
}
/**
* Sets the template to use
*
* @param string $template
*/
public function setTemplate($template)
{
$this->template = (string) $template;
}
/**
* Sets the title of the project
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = (string) $title;
}
/**
* Sets the default package name
*
* @param string $defaultPackageName
*/
public function setDefaultPackageName($defaultPackageName)
{
$this->defaultPackageName = (string) $defaultPackageName;
}
/**
* @param string $pharLocation
*/
public function setPharLocation($pharLocation)
{
$this->pharLocation = $pharLocation;
}
/**
* Finds and initializes the phpDocumentor installation
*/
private function initializePhpDocumentor()
{
$phpDocumentorPath = '';
if (!empty($this->pharLocation)) {
include_once 'phar://' . $this->pharLocation . '/vendor/autoload.php';
if (!class_exists('phpDocumentor\\Bootstrap')) {
throw new BuildException(
$this->pharLocation . ' does not look like a phpDocumentor 2 .phar'
);
}
} elseif (class_exists('Composer\\Autoload\\ClassLoader', false)) {
if (!class_exists('phpDocumentor\\Bootstrap')) {
throw new BuildException('You need to install phpDocumentor 2 or add your include path to your composer installation.');
}
} else {
$phpDocumentorPath = $this->findPhpDocumentorPath();
if (empty($phpDocumentorPath)) {
throw new BuildException("Please make sure phpDocumentor 2 is installed and on the include_path.");
}
set_include_path($phpDocumentorPath . PATH_SEPARATOR . get_include_path());
include_once $phpDocumentorPath . '/phpDocumentor/Bootstrap.php';
}
/** @phpstan-ignore-next-line */
$this->app = Bootstrap::createInstance()->initialize();
$this->phpDocumentorPath = $phpDocumentorPath;
}
/**
* Build a list of files (from the fileset elements)
* and call the phpDocumentor parser
*
* @return string
*/
private function parseFiles()
{
$parser = $this->app['parser'];
$builder = $this->app['descriptor.builder'];
$builder->createProjectDescriptor();
$projectDescriptor = $builder->getProjectDescriptor();
$projectDescriptor->setName($this->title);
$paths = [];
// filesets
foreach ($this->filesets as $fs) {
$ds = $fs->getDirectoryScanner($this->project);
$dir = $fs->getDir($this->project);
$srcFiles = $ds->getIncludedFiles();
foreach ($srcFiles as $file) {
$paths[] = $dir . FileSystem::getFileSystem()->getSeparator() . $file;
}
}
$this->project->log("Will parse " . count($paths) . " file(s)", Project::MSG_VERBOSE);
/** @phpstan-ignore-next-line */
$files = new Collection();
$files->addFiles($paths);
/** @phpstan-ignore-next-line */
$mapper = new ProjectDescriptorMapper($this->app['descriptor.cache']);
$mapper->garbageCollect($files);
$mapper->populate($projectDescriptor);
$parser->setPath($files->getProjectRoot());
$parser->setDefaultPackageName($this->defaultPackageName);
$parser->parse($builder, $files);
$mapper->save($projectDescriptor);
return $mapper;
}
/**
* Transforms the parsed files
*/
private function transformFiles()
{
$transformer = $this->app['transformer'];
$compiler = $this->app['compiler'];
$builder = $this->app['descriptor.builder'];
$projectDescriptor = $builder->getProjectDescriptor();
$transformer->getTemplates()->load($this->template, $transformer);
$transformer->setTarget($this->destDir->getAbsolutePath());
foreach ($compiler as $pass) {
$pass->execute($projectDescriptor);
}
}
/**
* Runs phpDocumentor 2
*/
public function run()
{
}
/**
* Find the correct php documentor path
*
* @return null|string
*/
private function findPhpDocumentorPath()
{
$phpDocumentorPath = null;
$directories = ['phpDocumentor', 'phpdocumentor'];
foreach ($directories as $directory) {
foreach (Phing::explodeIncludePath() as $path) {
$testPhpDocumentorPath = $path . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . 'src';
if (file_exists($testPhpDocumentorPath)) {
$phpDocumentorPath = $testPhpDocumentorPath;
}
}
}
return $phpDocumentorPath;
}
/**
* Task entry point
*
* @see Task::main()
*/
public function main()
{
if (empty($this->destDir)) {
throw new BuildException("You must supply the 'destdir' attribute", $this->getLocation());
}
if (empty($this->filesets)) {
throw new BuildException("You have not specified any files to include (<fileset>)", $this->getLocation());
}
$this->initializePhpDocumentor();
$cache = $this->app['descriptor.cache'];
$cache->getOptions()->setCacheDir($this->destDir->getAbsolutePath());
$this->parseFiles();
$this->project->log("Transforming...", Project::MSG_VERBOSE);
$this->transformFiles();
}
}