1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class CrudControllerMakeCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'make:crud:controller'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $description = 'Create a new crud friendly controller class'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $type = 'Controller'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
protected function getStub() |
31
|
|
|
{ |
32
|
|
|
return __DIR__.'/stubs/controller.stub'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $rootNamespace |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getDefaultNamespace($rootNamespace) |
40
|
|
|
{ |
41
|
|
|
return $rootNamespace.'\Http\Controllers'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get and parse the model name. |
46
|
|
|
* |
47
|
|
|
* @param void |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function getModelName() |
51
|
|
|
{ |
52
|
|
|
$name = trim($this->argument('model')); |
53
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
54
|
|
|
|
55
|
|
|
if (Str::contains($name, '/')) |
56
|
|
|
{ |
57
|
|
|
$name = str_replace('/', '\\', $name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (Str::startsWith($name, $rootNamespace)) |
61
|
|
|
{ |
62
|
|
|
return '\\' . $name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return '\\' . $rootNamespace . $name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function getArguments() |
72
|
|
|
{ |
73
|
|
|
return array_merge(parent::getArguments(), [ |
74
|
|
|
['model', InputArgument::REQUIRED, 'The name of the model, for example, "Post" or "App/Post"'], |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $name |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function buildClass($name) |
83
|
|
|
{ |
84
|
|
|
$namespace = $this->getNamespace($name); |
85
|
|
|
|
86
|
|
|
$modelName = $this->getModelName(); |
87
|
|
|
$modelClass = $modelName . '::class'; |
88
|
|
|
|
89
|
|
|
$class = parent::buildClass($name); |
90
|
|
|
$class = str_replace("use {$namespace}\Controller;\n", '', $class); |
91
|
|
|
$class = str_replace('DummyModelClass', $modelClass, $class); |
92
|
|
|
$class = str_replace('DummyModel', $modelName, $class); |
93
|
|
|
|
94
|
|
|
return $class; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|