-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathGenerateRouterResourceCommand.php
More file actions
111 lines (93 loc) · 3.07 KB
/
GenerateRouterResourceCommand.php
File metadata and controls
111 lines (93 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Bow\Console\Command\Generator;
use Bow\Console\AbstractCommand;
use Bow\Console\Color;
use Bow\Console\Generator;
use Bow\Support\Str;
class GenerateRouterResourceCommand extends AbstractCommand
{
/**
* Command used to set up the resource system.
*
* @param string $controller
* @return void
* @throws
*/
public function run(string $controller): void
{
// We create command generator instance
$generator = new Generator(
$this->setting->getControllerDirectory(),
$controller
);
// We check if the file already exists
if ($generator->fileExists()) {
echo Color::danger('The controller already exists');
exit(1);
}
// We create the resource url prefix
$prefix = preg_replace("/controller/i", "", strtolower($controller));
$prefix = '/' . trim($prefix, '/');
$prefix = Str::plural(Str::snake($prefix));
$parameters = $this->arg->getParameters();
$model_namespace = $parameters->has('--model') ? $parameters->get('--model') : '';
// We check if --with-view exists. If that exists,
// we launch the question
if (
$parameters->has('--with-view')
&& $this->arg->readline("Do you want me to create the associated views? ")
) {
$view_base_directory = preg_replace("/controller/i", "", strtolower($controller));
$view_base_directory = strtolower($view_base_directory);
$this->createDefaultView($view_base_directory);
}
$this->createResourceController(
$generator,
$prefix,
$controller,
$model_namespace
);
exit(0);
}
/**
* Create the default view for rest Generation
*
* @param string $base_directory
* @return void
*/
private function createDefaultView(string $base_directory): void
{
@mkdir(config('view.path') . "/" . $base_directory, 0766);
// We create the default CRUD view
foreach (["create", "edit", "show", "index"] as $value) {
$filename = "$base_directory/$value" . config('view.extension');
touch(config('view.path') . '/' . $filename);
echo "$filename added\n";
}
}
/**
* Create rest controller
*
* @param Generator $generator
* @param string $prefix
* @param string $controller
* @param string $model_namespace
*
* @return void
*/
private function createResourceController(
Generator $generator,
string $prefix,
string $controller,
string $model_namespace = ''
): void {
$generator->write('controller/rest', [
'modelNamespace' => $model_namespace,
'prefix' => $prefix,
'className' => $controller,
'baseNamespace' => $this->namespaces['controller'] ?? 'App\\Controllers'
]);
echo Color::green('The controller Rest was well created.');
}
}