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