Completed
Push — master ( 7f9538...d6cc27 )
by Philip
04:07
created

genAPIDoc()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 8
nop 6
dl 0
loc 26
rs 9.7666
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
$doxphpPath = getenv('DOXPHPPATH');
13
14
$classHierarchyToctree = [
15
    'Valdi\\\\Validator\\\\AbstractArray' => '.. toctree::
16
17
   Collection
18
   Nested',
19
    'Valdi\\\\Validator\\\\AbstractComparator' => '.. toctree::
20
21
   Between
22
   LengthBetween
23
   Max
24
   MaxLength
25
   Min
26
   MinLength
27
   Regexp
28
   Value',
29
    'Valdi\\\\Validator\\\\AbstractDateTimeComparator' => '.. toctree::
30
31
   AfterDateTime
32
   BeforeDateTime
33
   DateTimeBetween
34
   InTheFuture
35
   InThePast
36
   OlderThan
37
   YoungerThan',
38
    'Valdi\\\\Validator\\\\AbstractFilter' => '.. toctree::
39
40
   Email
41
   Floating
42
   Integer
43
   IP
44
   IPv4
45
   IPv6
46
   Url',
47
    'Valdi\\\\Validator\\\\AbstractParametrizedValidator' => '.. toctree::
48
49
   AbstractComparator
50
   AbstractDateTimeComparator',
51
    'Valdi\\\\Validator\\\\Regexp' => '.. toctree::
52
53
   Alphabetical
54
   AlphaNumerical
55
   Slug'
56
];
57
58
function scanFiles($dir) : array
59
{
60
    echo "Scanning $dir\n";
61
    $result = [];
62
    $files = scandir($dir);
63
    $omit = ['.', '..', '.DS_Store'];
64
    foreach ($files as $file) {
65
        $completeFile = $dir.DIRECTORY_SEPARATOR.$file;
66
        if (in_array($file, $omit)) {
67
            continue;
68
        }
69
        if (is_file($completeFile)) {
70
            $result[] = $completeFile;
71
        }
72
        if (is_dir($completeFile)) {
73
            $result = array_merge($result, scanFiles($completeFile));
74
        }
75
    }
76
    return $result;
77
}
78
79
function genAPIDoc($doxphpPath, $baseDir, $targetDir, $baseNamespace, $classHierarchyToctree, $file)
80
{
81
    $cmd = $doxphpPath.'/doxphp < "'.$file.'" | '.$doxphpPath.'/doxphp2sphinx';
82
    $doc = shell_exec($cmd);
83
84
    $fileWithoutExtension = substr($file, strlen($baseDir) + 1, strlen($file) - strlen($baseDir) - 5);
85
    $fullClassname = $baseNamespace.'\\\\'.str_replace('/', '\\\\', $fileWithoutExtension);
86
87
    $headlineSeparator = '';
88
    for ($i = 0; $i < strlen($fullClassname); ++$i) {
89
        $headlineSeparator .= '-';
90
    }
91
92
    $headline = "$headlineSeparator\n$fullClassname\n$headlineSeparator\n";
93
94
    if (array_key_exists($fullClassname, $classHierarchyToctree)) {
95
        $headline .= "\n".$classHierarchyToctree[$fullClassname]."\n";
96
    }
97
98
    $targetFile = $targetDir.'/'.$fileWithoutExtension.'.rst';
99
    $targetClassDir = dirname($targetFile);
100
    if (!is_dir($targetClassDir)) {
101
        mkdir($targetClassDir, 0755, true);
102
    }
103
104
    file_put_contents($targetFile, $headline."\n".$doc);
105
106
}
107
108
$baseDir = 'src/Valdi';
109
$files = scanFiles($baseDir);
110
111
$targetDir = 'docs/api';
112
113
shell_exec('rm -r '.$targetDir);
114
foreach ($files as $file) {
115
    genAPIDoc($doxphpPath, $baseDir, $targetDir, 'Valdi', $classHierarchyToctree, $file);
116
}