1
|
|
|
<?php namespace Hafiz\Commands; |
2
|
|
|
|
3
|
|
|
use CodeIgniter\CLI\BaseCommand; |
4
|
|
|
use CodeIgniter\CLI\CLI; |
5
|
|
|
use Hafiz\Libraries\FileHandler; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Make command class for a Filter skeleton |
9
|
|
|
* class generation |
10
|
|
|
* skeleton has session and validation instance |
11
|
|
|
* already implemented |
12
|
|
|
* |
13
|
|
|
* @package CodeIgniter\Commands |
14
|
|
|
* @extend BaseCommand |
15
|
|
|
*/ |
16
|
|
|
class MakeFilter extends BaseCommand |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The group command is heading under all |
21
|
|
|
* commands will be listed |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $group = 'CI4-Recharge'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Command's name |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $name = 'make:filter'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The Command's short description |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $description = 'Creates a skeleton Filter file.'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The Command's usage |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $usage = 'make:filter [filter_name] [Options]'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The Command's Arguments |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $arguments = [ |
49
|
|
|
'filter_name' => 'The Filter file name', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The Command's Option |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $options = [ |
57
|
|
|
'-n' => 'Set Filter namespace', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates a new Filter file with the current timestamp. |
62
|
|
|
* @param array $params |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function run(array $params = []) |
66
|
|
|
{ |
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
helper(['inflector', 'filesystem']); |
71
|
|
|
$file = new FileHandler(); |
72
|
|
|
|
73
|
|
|
$name = array_shift($params); |
74
|
|
|
|
75
|
|
|
//if namespace is given |
76
|
|
|
$ns = $params['-n'] ?? CLI::getOption('n'); |
77
|
|
|
|
78
|
|
|
if (empty($name)) |
79
|
|
|
$name = CLI::prompt(lang('Recharge.filterName'), null, 'required|string'); |
80
|
|
|
|
81
|
|
|
if (empty($name)) { |
82
|
|
|
CLI::error(lang('Recharge.badName')); |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//namespace locator |
87
|
|
|
$nsinfo = $file->getNamespaceInfo($ns, 'App'); |
88
|
|
|
$ns = $nsinfo['ns']; |
89
|
|
|
$targetDir = $nsinfo['path'] . '/Filters/'; |
90
|
|
|
|
91
|
|
|
//class & file name |
92
|
|
|
$name = pascalize($name) . ((stripos($name, 'filter') == false) ? 'Filter' : ''); |
|
|
|
|
93
|
|
|
$filepath = $targetDir . $name . '.php'; |
94
|
|
|
|
95
|
|
|
if ($file->verifyDirectory($filepath)) { |
96
|
|
|
|
97
|
|
|
$data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A")]; |
98
|
|
|
|
99
|
|
|
//check a directory exist |
100
|
|
|
if ($file->checkFileExist($filepath) == true) { |
|
|
|
|
101
|
|
|
$template = $file->renderTemplate('filter', $data); |
102
|
|
|
|
103
|
|
|
if (!write_file($filepath, $template)) { |
104
|
|
|
CLI::error(lang('Recharge.writeError', [$filepath])); |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|