These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Encore\Admin\Console; |
||
4 | |||
5 | use Illuminate\Console\GeneratorCommand; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Illuminate\Support\Str; |
||
8 | |||
9 | class MakeCommand extends GeneratorCommand |
||
10 | { |
||
11 | /** |
||
12 | * The console command name. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $signature = 'admin:make {name} |
||
17 | {--model=} |
||
18 | {--title=} |
||
19 | {--stub= : Path to the custom stub file. } |
||
20 | {--namespace=} |
||
21 | {--O|output}'; |
||
22 | |||
23 | /** |
||
24 | * The console command description. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $description = 'Make admin controller'; |
||
29 | |||
30 | /** |
||
31 | * @var ResourceGenerator |
||
32 | */ |
||
33 | protected $generator; |
||
34 | |||
35 | /** |
||
36 | * Execute the console command. |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | public function handle() |
||
41 | { |
||
42 | if (!$this->modelExists()) { |
||
43 | $this->error('Model does not exists !'); |
||
44 | |||
45 | return false; |
||
46 | } |
||
47 | |||
48 | $stub = $this->option('stub'); |
||
49 | |||
50 | if ($stub and !is_file($stub)) { |
||
51 | $this->error('The stub file dose not exist.'); |
||
52 | |||
53 | return false; |
||
54 | } |
||
55 | |||
56 | $modelName = $this->option('model'); |
||
57 | |||
58 | $this->generator = new ResourceGenerator($modelName); |
||
59 | |||
60 | if ($this->option('output')) { |
||
61 | return $this->output($modelName); |
||
62 | } |
||
63 | |||
64 | if (parent::handle() !== false) { |
||
65 | $name = $this->argument('name'); |
||
66 | $path = Str::plural(Str::kebab(class_basename($this->option('model')))); |
||
67 | |||
68 | $this->line(''); |
||
69 | $this->comment('Add the following route to app/Admin/routes.php:'); |
||
70 | $this->line(''); |
||
71 | $this->info(" \$router->resource('{$path}', {$name}::class);"); |
||
72 | $this->line(''); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $modelName |
||
78 | */ |
||
79 | protected function output($modelName) |
||
80 | { |
||
81 | $this->alert("laravel-admin controller code for model [{$modelName}]"); |
||
82 | |||
83 | $this->info($this->generator->generateGrid()); |
||
84 | $this->info($this->generator->generateShow()); |
||
85 | $this->info($this->generator->generateForm()); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Determine if the model is exists. |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | protected function modelExists() |
||
94 | { |
||
95 | $model = $this->option('model'); |
||
96 | |||
97 | if (empty($model)) { |
||
98 | return true; |
||
99 | } |
||
100 | |||
101 | return class_exists($model) && is_subclass_of($model, Model::class); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Replace the class name for the given stub. |
||
106 | * |
||
107 | * @param string $stub |
||
108 | * @param string $name |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | protected function replaceClass($stub, $name) |
||
113 | { |
||
114 | $stub = parent::replaceClass($stub, $name); |
||
115 | |||
116 | return str_replace( |
||
117 | [ |
||
118 | 'DummyModelNamespace', |
||
119 | 'DummyTitle', |
||
120 | 'DummyModel', |
||
121 | 'DummyGrid', |
||
122 | 'DummyShow', |
||
123 | 'DummyForm', |
||
124 | ], |
||
125 | [ |
||
126 | $this->option('model'), |
||
127 | $this->option('title') ?: $this->option('model'), |
||
128 | class_basename($this->option('model')), |
||
129 | $this->indentCodes($this->generator->generateGrid()), |
||
130 | $this->indentCodes($this->generator->generateShow()), |
||
131 | $this->indentCodes($this->generator->generateForm()), |
||
132 | ], |
||
133 | $stub |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param string $code |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function indentCodes($code) |
||
143 | { |
||
144 | $indent = str_repeat(' ', 8); |
||
145 | |||
146 | return rtrim($indent.preg_replace("/\r\n/", "\r\n{$indent}", $code)); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get the stub file for the generator. |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | protected function getStub() |
||
155 | { |
||
156 | if ($stub = $this->option('stub')) { |
||
157 | return $stub; |
||
0 ignored issues
–
show
|
|||
158 | } |
||
159 | |||
160 | if ($this->option('model')) { |
||
161 | return __DIR__.'/stubs/controller.stub'; |
||
162 | } |
||
163 | |||
164 | return __DIR__.'/stubs/blank.stub'; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get the default namespace for the class. |
||
169 | * |
||
170 | * @param string $rootNamespace |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function getDefaultNamespace($rootNamespace) |
||
175 | { |
||
176 | if ($namespace = $this->option('namespace')) { |
||
177 | return $namespace; |
||
0 ignored issues
–
show
The return type of
return $namespace; (array|string|boolean ) is incompatible with the return type documented by Encore\Admin\Console\Mak...nd::getDefaultNamespace of type string .
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function
Loading history...
|
|||
178 | } |
||
179 | |||
180 | return config('admin.route.namespace'); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get the desired class name from the input. |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | protected function getNameInput() |
||
189 | { |
||
190 | $name = trim($this->argument('name')); |
||
191 | |||
192 | $this->type = $this->qualifyClass($name); |
||
193 | |||
194 | return $name; |
||
195 | } |
||
196 | } |
||
197 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.