|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHINT package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
|
7
|
|
|
* <https://github.com/adhocore> |
|
8
|
|
|
* |
|
9
|
|
|
* Licensed under MIT license. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ahc\Phint\Console; |
|
13
|
|
|
|
|
14
|
|
|
use Ahc\Phint\Generator\TwigGenerator; |
|
15
|
|
|
use Ahc\Phint\Util\Composer; |
|
16
|
|
|
use CrazyFactory\DocBlocks\DocBlock; |
|
17
|
|
|
|
|
18
|
|
|
class DocsCommand extends BaseCommand |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string Command name */ |
|
21
|
|
|
protected $_name = 'docs'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string Command description */ |
|
24
|
|
|
protected $_desc = 'Generate basic readme docs from docblocks'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string Current working dir */ |
|
27
|
|
|
protected $_workDir; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Configure the command options/arguments. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function onConstruct() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->_workDir = \realpath(\getcwd()); |
|
37
|
|
|
|
|
38
|
|
|
$this |
|
39
|
|
|
->option('-o --output', 'Output file (default README.md). For old project you should use something else' |
|
40
|
|
|
."\n(OR mark region with <!-- DOCS START --> and <!-- DOCS END --> to inject docs)", |
|
41
|
|
|
null, 'README.md' |
|
42
|
|
|
) |
|
43
|
|
|
->option('-a --with-abstract', 'Create stub for abstract/interface class') |
|
44
|
|
|
->usage( |
|
45
|
|
|
'<bold> phint docs</end> Appends to readme.md<eol/>' . |
|
46
|
|
|
'<bold> phint d</end> <comment>-o docs/api.md</end> Writes to docs/api.md<eol/>' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Generate test stubs. |
|
52
|
|
|
* |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function execute() |
|
56
|
|
|
{ |
|
57
|
|
|
$io = $this->app()->io(); |
|
58
|
|
|
|
|
59
|
|
|
$io->comment('Preparing metadata ...', true); |
|
60
|
|
|
$docsMetadata = $this->prepare(); |
|
61
|
|
|
|
|
62
|
|
|
if (empty($docsMetadata)) { |
|
63
|
|
|
$io->bgGreen('Looks like nothing to do here', true); |
|
64
|
|
|
|
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$io->comment('Generating docs ...', true); |
|
69
|
|
|
$generated = $this->generate($docsMetadata, $this->values()); |
|
70
|
|
|
|
|
71
|
|
|
if ($generated) { |
|
72
|
|
|
$io->cyan("$generated doc(s) generated", true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$io->ok('Done', true); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function prepare(): array |
|
79
|
|
|
{ |
|
80
|
|
|
// Sorry psr-0! |
|
81
|
|
|
$namespaces = $this->_composer->config('autoload.psr-4'); |
|
82
|
|
|
|
|
83
|
|
|
$srcPaths = []; |
|
84
|
|
|
foreach ($namespaces as $ns => $path) { |
|
85
|
|
|
if (\preg_match('!^(source|src|lib|class)/?!', $path)) { |
|
86
|
|
|
$srcPaths[] = $this->_pathUtil->join($this->_workDir, $path); |
|
87
|
|
|
} else { |
|
88
|
|
|
unset($namespaces[$ns]); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$classes = $this->_pathUtil->loadClasses($srcPaths, \array_keys($namespaces)); |
|
93
|
|
|
|
|
94
|
|
|
return $this->getClassesMetadata($classes); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function getClassesMetadata(array $classes): array |
|
98
|
|
|
{ |
|
99
|
|
|
$metadata = []; |
|
100
|
|
|
|
|
101
|
|
|
foreach ($classes as $classFqcn) { |
|
102
|
|
|
if ([] === $meta = $this->getClassMetadata($classFqcn)) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$metadata[] = $meta; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $metadata; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function getClassMetadata(string $classFqcn): array |
|
113
|
|
|
{ |
|
114
|
|
|
$reflex = new \ReflectionClass($classFqcn); |
|
115
|
|
|
|
|
116
|
|
|
if (!$this->shouldGenerateDocs($reflex)) { |
|
117
|
|
|
return []; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$methods = []; |
|
121
|
|
|
$isTrait = $reflex->isTrait(); |
|
122
|
|
|
$name = $reflex->getShortName(); |
|
123
|
|
|
$exclude = ['__construct', '__destruct']; |
|
124
|
|
|
|
|
125
|
|
|
foreach ($reflex->getMethods(\ReflectionMethod::IS_PUBLIC) as $m) { |
|
126
|
|
|
if ($m->class !== $classFqcn && \in_array($m->name, $exclude)) { |
|
127
|
|
|
continue; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$methods[$m->name] = $this->getMethodMetadata($m); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (empty($methods)) { |
|
134
|
|
|
return []; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$texts = (new DocBlock($reflex))->texts(); |
|
138
|
|
|
$title = \array_shift($texts); |
|
139
|
|
|
|
|
140
|
|
|
return \compact('classFqcn', 'name', 'isTrait', 'title', 'texts', 'methods'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function shouldGenerateDocs(\ReflectionClass $reflex): bool |
|
144
|
|
|
{ |
|
145
|
|
|
if ($this->abstract) { |
|
|
|
|
|
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return !$reflex->isInterface() && !$reflex->isAbstract(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
protected function getMethodMetadata(\ReflectionMethod $method): array |
|
153
|
|
|
{ |
|
154
|
|
|
$params = []; |
|
155
|
|
|
$parser = new DocBlock($method); |
|
156
|
|
|
|
|
157
|
|
|
foreach ($parser->find('param') as $param) { |
|
158
|
|
|
$params[] = \preg_replace(['/(.*\$\w+)(.*)/', '/ +/'], ['$1', ' '], $param->getValue()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
if (null !== $return = $parser->first('return')) { |
|
162
|
|
|
$return = \preg_replace('/ .*?$/', '', $return->getValue()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$texts = $parser->texts(); |
|
166
|
|
|
$title = \array_shift($texts); |
|
167
|
|
|
|
|
168
|
|
|
return ['static' => $method->isStatic()] + \compact('title', 'texts', 'params', 'return'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
protected function generate(array $docsMetadata, array $parameters): int |
|
172
|
|
|
{ |
|
173
|
|
|
$templatePath = __DIR__ . '/../../resources'; |
|
174
|
|
|
$generator = new TwigGenerator($templatePath, $this->getCachePath()); |
|
175
|
|
|
|
|
176
|
|
|
if (!$this->_pathUtil->isAbsolute($parameters['output'])) { |
|
177
|
|
|
$parameters['output'] = $this->_pathUtil->join($this->_workDir, $parameters['output']); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $generator->generateDocs($docsMetadata, $parameters); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|