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 create a empty skeleton |
||
9 | * Configuration File on specific namespace location |
||
10 | * |
||
11 | * Class MakeConfig |
||
12 | * @package Hafiz\Commands |
||
13 | */ |
||
14 | class MakeConfig extends BaseCommand |
||
15 | { |
||
16 | /** |
||
17 | * The group command is heading under all |
||
18 | * commands will be listed |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $group = 'CI4-Recharge'; |
||
22 | |||
23 | /** |
||
24 | * The Command's name |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $name = 'make:config'; |
||
28 | |||
29 | /** |
||
30 | * The Command's short description |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $description = 'Creates a Configuration file.'; |
||
34 | |||
35 | /** |
||
36 | * The Command's usage |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $usage = 'make:config [config_name] [Options]'; |
||
40 | |||
41 | /** |
||
42 | * The Command's Arguments |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $arguments = [ |
||
46 | 'config_name' => 'The configuration file name', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * The Command's Options |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $options = [ |
||
54 | '-n' => 'Set Configuration namespace', |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Creates a new configuration file with The current timestamp. |
||
59 | * @param array $params |
||
60 | * @return void |
||
61 | */ |
||
62 | public function run(array $params = []) |
||
63 | { |
||
64 | /** |
||
65 | * Calling all Library and helpers |
||
66 | */ |
||
67 | helper(['inflector', 'filesystem']); |
||
68 | $file = new FileHandler(); |
||
69 | |||
70 | /** |
||
71 | * Input Configuration name from input |
||
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.configName'), 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, 'Config'); |
||
88 | |||
89 | //class & file name |
||
90 | $name = pascalize($name); |
||
91 | |||
92 | //target Dir |
||
93 | if ($nsinfo['default']) { |
||
94 | $targetDir = $nsinfo['path'] . '/'; |
||
95 | $ns = $nsinfo['ns']; |
||
96 | } else { |
||
97 | $targetDir = $nsinfo['path'] . '/Config/'; |
||
98 | $ns = $nsinfo['ns'] . '\Config'; |
||
99 | } |
||
100 | |||
101 | $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A")]; |
||
102 | |||
103 | $filepath = $targetDir . $name . '.php'; |
||
104 | |||
105 | //check a directory exist |
||
106 | if ($file->checkFileExist($filepath) == true) { |
||
0 ignored issues
–
show
|
|||
107 | $template = $file->renderTemplate('config', $data); |
||
108 | |||
109 | if (!write_file($filepath, $template)) { |
||
110 | CLI::error(lang('Recharge.writeError', [$filepath])); |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.