|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace controllers; |
|
4
|
|
|
|
|
5
|
|
|
use terminal\Terminal; |
|
6
|
|
|
use filesystem\Filesystem; |
|
7
|
|
|
use \Config; |
|
8
|
|
|
|
|
9
|
|
|
trait HelpTrait |
|
10
|
|
|
{ |
|
11
|
|
|
function __construct( |
|
|
|
|
|
|
12
|
|
|
Config $config, |
|
13
|
|
|
Terminal $terminal, |
|
14
|
|
|
Filesystem $filesystem |
|
15
|
|
|
) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->config = $config; |
|
|
|
|
|
|
18
|
|
|
$this->terminal = $terminal; |
|
|
|
|
|
|
19
|
|
|
$this->filesystem = $filesystem; |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function help($search = '') |
|
23
|
|
|
{ |
|
24
|
|
|
$this->terminal->echo('Usage: php ccg.php [generator] [command] [options]'); |
|
25
|
|
|
|
|
26
|
|
|
$docs = $this->getHelp(); |
|
27
|
|
|
|
|
28
|
|
|
array_walk($docs, function(array $doc) use ($search) { |
|
29
|
|
|
|
|
30
|
|
|
if ($search && strpos($doc['name'], $search) === false) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->terminal->echo(str_repeat(PHP_EOL, 2)); |
|
35
|
|
|
|
|
36
|
|
|
$docLines = preg_split('/\r\n|\r|\n/', $doc['help']); |
|
37
|
|
|
$descriptionLines = $docLines; |
|
38
|
|
|
|
|
39
|
|
|
unset($descriptionLines[0]); |
|
40
|
|
|
|
|
41
|
|
|
$this->terminal->success($docLines[0]); |
|
42
|
|
|
$this->terminal->echo(implode(PHP_EOL, $descriptionLines)); |
|
|
|
|
|
|
43
|
|
|
}); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getHelp() |
|
47
|
|
|
{ |
|
48
|
|
|
$ref = new \ReflectionClass($this); |
|
49
|
|
|
$methods = $ref->getMethods(); |
|
50
|
|
|
|
|
51
|
|
|
$docs = array_map(function(\ReflectionMethod $method) { |
|
52
|
|
|
$helpCommentPurified = ''; |
|
53
|
|
|
$helpComment = self::_extractCommentForHelp($method->getDocComment() ?: ''); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if (!empty($helpComment)) { |
|
56
|
|
|
$helpCommentPurified = self::_purifyCommentForHelp($helpComment[1]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'name' => $method->getName(), |
|
61
|
|
|
'help' => $helpCommentPurified, |
|
62
|
|
|
]; |
|
63
|
|
|
}, $methods); |
|
64
|
|
|
|
|
65
|
|
|
$docsFiltered = array_values( |
|
66
|
|
|
array_filter($docs, function($doc) { |
|
67
|
|
|
return (bool) $doc['help']; |
|
68
|
|
|
}) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return $docsFiltered; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @todo test |
|
76
|
|
|
*/ |
|
77
|
|
|
private static function _extractCommentForHelp(string $comment): array |
|
78
|
|
|
{ |
|
79
|
|
|
preg_match('/\/\*{2}\s+\*\shelp:\s+(.*)/usmi', $comment, $matches); |
|
80
|
|
|
|
|
81
|
|
|
return $matches; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @todo test |
|
86
|
|
|
*/ |
|
87
|
|
|
private static function _purifyCommentForHelp(string $comment): string |
|
88
|
|
|
{ |
|
89
|
|
|
$clearedComment = trim(preg_replace('/(\*\s)|(\*\/)/sm', '', $comment)); |
|
90
|
|
|
$indentedComment = implode( |
|
91
|
|
|
PHP_EOL . str_repeat(' ', 8), |
|
92
|
|
|
preg_split('/\r\n|\r|\n/', $clearedComment) |
|
|
|
|
|
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
return $indentedComment; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.