summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/ext/apigen/ApiGenTask.php
blob: d8438b3e8a6ea5cb871838bd0d84e97e3df8b195 (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?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>.
 */

require_once 'phing/Task.php';

/**
 * ApiGen task (http://apigen.org).
 *
 * @package   phing.tasks.ext.apigen
 * @author    Martin Srank <martin@smasty.net>
 * @author    Jaroslav Hanslík <kukulich@kukulich.cz>
 * @since     2.4.10
 */
class ApiGenTask extends Task
{
    /**
     * Default ApiGen executable name.
     *
     * @var string
     */
    private $executable = 'apigen';

    /**
     * Default options for ApiGen.
     *
     * @var array
     */
    private $options = array(
        'progressbar' => false,
        'colors' => false,
        'update-check' => false
    );

    /**
     * Sets the ApiGen executable name.
     *
     * @param string $executable
     */
    public function setExecutable($executable)
    {
        $this->executable = (string) $executable;
    }

    /**
     * Sets the config file name.
     *
     * @param string $config
     */
    public function setConfig($config)
    {
        $this->options['config'] = (string) $config;
    }

    /**
     * Sets source files or directories.
     *
     * @param string $source
     */
    public function setSource($source)
    {
        $this->options['source'] = explode(',', $source);
    }

    /**
     * Sets the destination directory.
     *
     * @param string $destination
     */
    public function setDestination($destination)
    {
        $this->options['destination'] = (string) $destination;
    }

    /**
     * Sets list of allowed file extensions.
     *
     * @param string $extensions
     */
    public function setExtensions($extensions)
    {
        $this->options['extensions'] = explode(',', $extensions);
    }

    /**
     * Sets masks (case sensitive) to exclude files or directories from processing.
     *
     * @param string $exclude
     */
    public function setExclude($exclude)
    {
        $this->options['exclude'] = explode(',', $exclude);
    }

    /**
     * Sets masks to exclude elements from documentation generating.
     *
     * @param string $skipDocPath
     */
    public function setSkipDocPath($skipDocPath)
    {
        $this->options['skip-doc-path'] = explode(',', $skipDocPath);
    }

    /**
     * Sets a name prefix to exclude elements from documentation generating.
     *
     * @param string $skipDocPrefix
     */
    public function setSkipDocPrefix($skipDocPrefix)
    {
        $this->options['skip-doc-prefix'] = explode(',', $skipDocPrefix);
    }

    /**
     * Sets the character set of source files.
     *
     * @param string $charset
     */
    public function setCharset($charset)
    {
        $this->options['charset'] = explode(',', $charset);
    }

    /**
     * Sets the main project name prefix.
     *
     * @param string $main
     */
    public function setMain($main)
    {
        $this->options['main'] = (string) $main;
    }

    /**
     * Sets the title of generated documentation.
     *
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->options['title'] = (string) $title;
    }

    /**
     * Sets the documentation base URL.
     *
     * @param string $baseUrl
     */
    public function setBaseUrl($baseUrl)
    {
        $this->options['base-url'] = (string) $baseUrl;
    }

    /**
     * Sets the Google Custom Search ID.
     *
     * @param string $googleCseId
     */
    public function setGoogleCseId($googleCseId)
    {
        $this->options['google-cse-id'] = (string) $googleCseId;
    }

    /**
     * Sets the Google Custom Search label.
     *
     * @param string $googleCseLabel
     */
    public function setGoogleCseLabel($googleCseLabel)
    {
        $this->options['google-cse-label'] = (string) $googleCseLabel;
    }

    /**
     * Sets the Google Analytics tracking code.
     *
     * @param string $googleAnalytics
     */
    public function setGoogleAnalytics($googleAnalytics)
    {
        $this->options['google-analytics'] = (string) $googleAnalytics;
    }

    /**
     * Sets the template config file name.
     *
     * @param string $templateConfig
     */
    public function setTemplateConfig($templateConfig)
    {
        $this->options['template-config'] = (string) $templateConfig;
    }

    /**
     * Sets a list of HTML tags allowed in the documentation.
     *
     * @param string $allowedHtml
     */
    public function setAllowedHtml($allowedHtml)
    {
        $this->options['allowed-html'] = (string) $allowedHtml;
    }

    /**
     * Sets how elements should be grouped in the menu.
     *
     * @param string $groups
     */
    public function setGroups($groups)
    {
        $this->options['groups'] = (string) $groups;
    }

    /**
     * Sets element types for search input autocomplete.
     *
     * @param string $autocomplete
     */
    public function setAutocomplete($autocomplete)
    {
        $this->options['autocomplete'] = (string) $autocomplete;
    }

    /**
     * Sets the element access levels.
     *
     * Documentation only for methods and properties with the given access level will be generated.
     *
     * @param string $accessLevels
     */
    public function setAccessLevels($accessLevels)
    {
        $this->options['access-levels'] = (string) $accessLevels;
    }

    /**
     * Sets if documentation for elements marked as internal and internal documentation parts should be generated.
     *
     * @param boolean $internal
     */
    public function setInternal($internal)
    {
        $this->options['internal'] = (bool) $internal;
    }

    /**
     * Sets if documentation for PHP internal classes should be generated.
     *
     * @param boolean $php
     */
    public function setPhp($php)
    {
        $this->options['php'] = (bool) $php;
    }

    /**
     * Sets if tree view of classes, interfaces, traits and exceptions should be generated.
     *
     * @param boolean $tree
     */
    public function setTree($tree)
    {
        $this->options['tree'] = (bool) $tree;
    }

    /**
     * Sets if documentation for deprecated elements should be generated.
     *
     * @param boolean $deprecated
     */
    public function setDeprecated($deprecated)
    {
        $this->options['deprecated'] = (bool) $deprecated;
    }

    /**
     * Sets if documentation of tasks should be generated.
     *
     * @param boolean $todo
     */
    public function setTodo($todo)
    {
        $this->options['todo'] = (bool) $todo;
    }

    /**
     * Sets if highlighted source code files should be generated.
     *
     * @param boolean $sourceCode
     */
    public function setSourceCode($sourceCode)
    {
        $this->options['source-code'] = (bool) $sourceCode;
    }

    /**
     * Sets if a link to download documentation as a ZIP archive should be generated.
     *
     * @param boolean $download
     */
    public function setDownload($download)
    {
        $this->options['download'] = (bool) $download;
    }

    /**
     * Sets a file name for checkstyle report of poorly documented elements.
     *
     * @param string $report
     */
    public function setReport($report)
    {
        $this->options['report'] = (string) $report;
    }

    /**
     * Sets if the destination directory should be wiped out first.
     *
     * @param boolean $wipeout
     */
    public function setWipeout($wipeout)
    {
        $this->options['wipeout'] = (bool) $wipeout;
    }

    /**
     * Enables/disables scaning and generating messages.
     *
     * @param boolean $quiet
     */
    public function setQuiet($quiet)
    {
        $this->options['quiet'] = (bool) $quiet;
    }

    /**
     * Enables/disables the check for ApiGen updates.
     *
     * @param boolean $updateCheck
     */
    public function setUpdateCheck($updateCheck)
    {
        $this->options['update-check'] = (bool) $updateCheck;
    }

    /**
     * Enables/disables the debug mode.
     *
     * @param boolean $debug
     */
    public function setDebug($debug)
    {
        $this->options['debug'] = (bool) $debug;
    }

    /**
     * Runs ApiGen.
     *
     * @throws BuildException If something is wrong.
     * @see Task::main()
     */
    public function main()
    {
        if ('apigen' !== $this->executable && !is_file($this->executable)) {
            throw new BuildException(sprintf('Executable %s not found', $this->executable), $this->getLocation());
        }

        if (!empty($this->options['config'])) {
            // Config check
            if (!is_file($this->options['config'])) {
                throw new BuildException(sprintf('Config file %s doesn\'t exist', $this->options['config']), $this->getLocation());
            }
        } else {
            // Source check
            if (empty($this->options['source'])) {
                throw new BuildException('Source is not set', $this->getLocation());
            }
            // Destination check
            if (empty($this->options['destination'])) {
                throw new BuildException('Destination is not set', $this->getLocation());
            }
        }

        // Source check
        if (!empty($this->options['source'])) {
            foreach ($this->options['source'] as $source) {
                if (!file_exists($source)) {
                    throw new BuildException(sprintf('Source %s doesn\'t exist', $source), $this->getLocation());
                }
            }
        }

        // Execute ApiGen
        exec(escapeshellcmd($this->executable) . ' ' . $this->constructArguments(), $output, $return);

        $logType = 0 === $return ? Project::MSG_INFO : Project::MSG_ERR;
        foreach ($output as $line) {
            $this->log($line, $logType);
        }
    }

    /**
     * Generates command line arguments for the ApiGen executable.
     *
     * @return string
     */
    protected function constructArguments()
    {
        $args = array();
        foreach ($this->options as $option => $value) {
            if (is_bool($value)) {
                $args[] = '--' . $option . '=' . ($value ? 'yes' : 'no');
            } elseif (is_array($value)) {
                foreach ($value as $v) {
                    $args[] = '--' . $option . '=' . escapeshellarg($v);
                }
            } else {
                $args[] = '--' . $option . '=' . escapeshellarg($value);
            }
        }
        return implode(' ', $args);
    }
}