|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use exceptions\FileAlreadyExistsException; |
|
4
|
|
|
use generators\AddonXml\AddonXmlGenerator; |
|
5
|
|
|
use generators\Language\LanguageGenerator; |
|
6
|
|
|
use generators\Readme\ReadmeGenerator; |
|
7
|
|
|
use generators\FileGenerator; |
|
8
|
|
|
use enums\Config as EnumConfig; |
|
9
|
|
|
use terminal\Terminal; |
|
10
|
|
|
use filesystem\Filesystem; |
|
11
|
|
|
use mediators\GeneratorMediator; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Enter point for all commands |
|
15
|
|
|
*/ |
|
16
|
|
|
class Ccg |
|
17
|
|
|
{ |
|
18
|
|
|
private $config; |
|
19
|
|
|
private $terminal; |
|
20
|
|
|
private $filesystem; |
|
21
|
|
|
|
|
22
|
|
|
function __construct( |
|
23
|
|
|
Config $config, |
|
24
|
|
|
Terminal $terminal, |
|
25
|
|
|
Filesystem $filesystem |
|
26
|
|
|
) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->config = $config; |
|
29
|
|
|
$this->terminal = $terminal; |
|
30
|
|
|
$this->filesystem = $filesystem; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function generate() |
|
34
|
|
|
{ |
|
35
|
|
|
$generator = $this->config->get(EnumConfig::$GENERATOR); |
|
36
|
|
|
$command = $this->config->get(EnumConfig::$COMMAND) ?: 'index'; |
|
37
|
|
|
$controllerClass = 'controllers\\' . $generator; |
|
38
|
|
|
|
|
39
|
|
|
if (!$generator) { |
|
40
|
|
|
throw new \InvalidArgumentException('Provide a command. See php ccg.php help'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!class_exists($controllerClass)) { |
|
44
|
|
|
throw new \InvalidArgumentException('Command not recognized. See php ccg.php help'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$refl = new ReflectionClass($controllerClass); |
|
48
|
|
|
$controller = $refl->newInstanceArgs([ |
|
49
|
|
|
$this->config, |
|
50
|
|
|
$this->terminal, |
|
51
|
|
|
$this->filesystem |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
$allowedMethods = $controller::getAllowedMethods(); |
|
55
|
|
|
if (in_array($command, $allowedMethods)) { |
|
56
|
|
|
$controller->{$command}(); |
|
57
|
|
|
} else { |
|
58
|
|
|
throw new \InvalidArgumentException('Command not recognized. See php ccg.php help'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function autocomplete($arguments) |
|
65
|
|
|
{ |
|
66
|
|
|
$generator = $arguments['generator']; |
|
67
|
|
|
$command = $arguments['command']; |
|
68
|
|
|
$prev = $arguments['prev']; |
|
69
|
|
|
$cur = $arguments['cur']; |
|
70
|
|
|
$controllerClass = 'controllers\\' . $generator; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @todo add autogeneration this list |
|
74
|
|
|
*/ |
|
75
|
|
|
$generators = [ |
|
76
|
|
|
'addon', |
|
77
|
|
|
'addon-xml', |
|
78
|
|
|
'help' |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$autocompletes = []; |
|
82
|
|
|
// $this->filesystem->write(ROOT_DIR . '/logs/terminal.txt', "gen: $generator; com: $command; prev: $prev; cur: $cur; " . implode(',', $autocompletes)); |
|
83
|
|
|
|
|
84
|
|
|
// if (empty($generator)) { |
|
85
|
|
|
// $autocompletes = $generators; |
|
86
|
|
|
// } |
|
87
|
|
|
|
|
88
|
|
|
if (class_exists($controllerClass)) { |
|
89
|
|
|
$refl = new ReflectionClass($controllerClass); |
|
90
|
|
|
$controller = $refl->newInstanceArgs([ |
|
91
|
|
|
$this->config, |
|
92
|
|
|
$this->terminal, |
|
93
|
|
|
$this->filesystem |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
$method = $command; |
|
97
|
|
|
$is_method_exists = method_exists($controllerClass, $method); |
|
98
|
|
|
|
|
99
|
|
|
if ($method) { |
|
100
|
|
|
$method_autocomplete = $command . 'Autocomplete'; |
|
101
|
|
|
$autocompletes = method_exists($controllerClass, $method_autocomplete) ? $controller->{$method_autocomplete}($prev, $cur, $arguments) : []; |
|
102
|
|
|
// $autocompletes = method_exists($controllerClass, $method) ? $controller->{$method}($prev, $cur) : $this->config->getSystemKeys(); |
|
103
|
|
|
// $autocompletes = array_merge($autocompletes, $this->config->getSystemKeys()); |
|
104
|
|
|
$this->filesystem->write(ROOT_DIR . '/logs/terminal.txt', "gen: $generator; method: $method_autocomplete; " . implode(',', $autocompletes) . ':::' . method_exists($controllerClass, $method_autocomplete)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (empty($autocompletes) && !$is_method_exists) { |
|
108
|
|
|
$allowedMethods = $controller::getAllowedMethods(); |
|
109
|
|
|
$autocompletes = array_map(function($method) use ($generator) { |
|
110
|
|
|
return $generator . '/' . $method; |
|
111
|
|
|
}, $allowedMethods); |
|
112
|
|
|
// $autocompletes[] = 'aaaaaaaaaaaaaddddd'; |
|
113
|
|
|
} |
|
114
|
|
|
} else { |
|
115
|
|
|
$autocompletes = $generators; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @todo return array |
|
120
|
|
|
*/ |
|
121
|
|
|
$this->terminal->echo(implode(' ', $autocompletes)); |
|
122
|
|
|
exit(0); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.