Issues (39)

src/Commands/Controller/Create.php (2 issues)

Severity
1
<?php namespace Hafiz\Commands\Controller;
2
3
use CodeIgniter\CLI\BaseCommand;
4
use CodeIgniter\CLI\CLI;
5
use Config\Services;
6
use ReflectionClass;
7
use ReflectionException;
8
9
/**
10
 * Creates a new Controller file.
11
 * @package CodeIgniter\Commands
12
 * @extend BaseCommand
13
 */
14
class Create extends BaseCommand
15
{
16
17
    /**
18
     * The group command is heading under all
19
     * commands will be listed
20
     * @var string
21
     */
22
    protected $group = 'CI4-Recharge';
23
24
    /**
25
     * The Command's name
26
     * @var string
27
     */
28
    protected $name = 'make:controller';
29
30
    /**
31
     * The Command's short description
32
     * @var string
33
     */
34
    protected $description = 'Creates a controller file.';
35
36
    /**
37
     * The Command's usage
38
     * @var string
39
     */
40
    protected $usage = 'make:controller [controller_name] [Options]';
41
42
    /**
43
     * The Command's Arguments
44
     * @var array
45
     */
46
    protected $arguments = [
47
        'controller_name' => 'The Controller file name'
48
    ];
49
50
    /**
51
     * The Command's Options
52
     * @var array
53
     */
54
    protected $options = [
55
        '-n' => 'Set controller namespace',
56
        '-b' => 'Set controller base/extends class',
57
        '-s' => 'Set controller sub-directory after namespace',
58
        '-rest' => 'Set controller sub-directory after namespace',
59
    ];
60
61
    /**
62
     * Creates a new Controller file with the current timestamp.
63
     * @param array $params
64
     * @return void
65
     * @throws ReflectionException
66
     */
67
    public function run(array $params = [])
68
    {
69
        helper(['inflector', 'filesystem']);
70
71
        $name = array_shift($params);
72
73
        if (empty($name)) {
74
            $name = CLI::prompt(lang('Recharge.nameController'));
75
        }
76
77
        if (empty($name)) {
78
            CLI::error(lang('Recharge.badControllerName'));
79
            return;
80
        }
81
82
        //Class Namespace
83
        $ns = $params['-n'] ?? CLI::getOption('n');
84
85
        //Extends Base Class
86
        $base = $params['-b'] ?? CLI::getOption('b');
87
88
        //Sub directory after Namespace
89
        $sub = $params['-s'] ?? CLI::getOption('s');
0 ignored issues
show
The assignment to $sub is dead and can be removed.
Loading history...
90
91
        //Rest Controller Style
92
        $is_rest = CLI::getOption('rest');
93
94
        /** @var string real path $homepath */
95
        $homepath = APPPATH;
96
97
        $package = "App";
98
        $baseNameSpace = 'use App\Controllers\BaseController;';
99
        $parentController = 'BaseController';
100
101
        //Finding Namespace Location
102
        if (!empty($ns)) {
103
            // Get all namespaces
104
            $namespaces = Services::autoloader()->getNamespace();
105
106
            foreach ($namespaces as $namespace => $path) {
107
                if ($namespace === $ns) {
108
                    $homepath = realpath(reset($path));
109
                    $package = $ns;
110
                    break;
111
                }
112
            }
113
        } else {
114
            $ns = "App";
115
        }
116
117
        //Finding Base Class
118
        if (!empty($base)) {
119
            // Get all namespaces
120
            $namespaces = Services::autoloader()->getNamespace();
121
122
            foreach ($namespaces as $namespace => $path) {
123
                if ($namespace == "App" || $namespace == $ns) {
124
                    $full_path = realpath(reset($path)) . "/Controllers/" . $base . ".php";
125
                    if (file_exists($full_path)) {
126
                        $tempObj = new ReflectionClass($namespace . "\Controllers\\" . $base);
127
                        $baseNameSpace = 'use ' . $tempObj->getName() . ";";
128
                        $package = $ns;
129
                        break;
130
                    }
131
                }
132
            }
133
        } else {
134
            $base = $parentController;
0 ignored issues
show
The assignment to $base is dead and can be removed.
Loading history...
135
        }
136
137
        // Always use UTC/GMT so global teams can work together
138
        $fileName = pascalize($name);
139
140
        // full path
141
        $path = $homepath . '/Controllers/' . $fileName . '.php';
142
143
        // Class name should be Pascal case
144
        $name = pascalize($name);
145
        $date = date("d F, Y h:i:s A");
146
147
        //Basic Controller Template
148
        $basicTemplate = '';
149
150
        //REST Controller Template
151
        $restTemplate = <<<EOD
152
<?php namespace $ns\Controllers;
153
154
$baseNameSpace
155
use CodeIgniter\RESTful\ResourceController;
156
157
/**
158
 * @class $name
159
 * @author CI-Recharge
160
 * @package $package
161
 * @extends ResourceController
162
 * @created $date
163
 */
164
165
166
class $name extends ResourceController
167
{
168
    /**
169
     * $name constructor
170
     */
171
    public function __construct() 
172
    {
173
    
174
    }
175
    /**
176
     * @return array|string
177
     */
178
    public function index()
179
    {
180
        
181
        return view('');
182
    }
183
    
184
    /**
185
     * @return array|string
186
     */
187
    public function create()
188
    {
189
        
190
        return view('');
191
    }
192
    
193
    /**
194
     * @return array|string
195
     */
196
    public function store()
197
    {
198
        
199
        return ;
200
    }
201
    
202
    /**
203
     * @param int|null \$id
204
     * @return array|string
205
     */
206
    public function show(int \$id = null){
207
        
208
        return view('');
209
    }
210
    
211
    /**
212
     * @param int|null \$id
213
     * @return array|string
214
     */
215
    public function edit(int \$id = null)
216
    {
217
        
218
        return view('');
219
    }
220
    
221
    /**
222
     * @param int|null \$id
223
     * @return array|string
224
     */
225
    public function update(int \$id = null)
226
    {
227
        
228
        return ;
229
    }
230
    
231
    /**
232
     * @param int|null \$id
233
     * @return array|string
234
     */
235
    public function delete(int \$id = null)
236
    {
237
        
238
        return ;
239
    }   
240
}
241
242
EOD;
243
244
        $template = ($is_rest == true) ? $restTemplate : $basicTemplate;
245
246
        if (!write_file($path, $template)) {
247
            CLI::error(lang('Recharge.writeError', [$path]));
248
            return;
249
        }
250
251
        CLI::write('Created file: ' . CLI::color(str_replace($homepath, $ns, $path), 'green'));
252
    }
253
254
}
255