1
|
|
|
<?php namespace Wn\Generators\Commands; |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
|
6
|
|
|
class ControllerCommand extends BaseCommand { |
7
|
|
|
|
8
|
|
|
const DEFAULT_PATH = "app/Http/Controllers"; |
9
|
|
|
|
10
|
|
|
protected $signature = 'wn:controller |
11
|
|
|
{model : Name of the model (with namespace if not App)} |
12
|
|
|
{--path='.ControllerCommand::DEFAULT_PATH.' : where to store the controllers file.} |
13
|
|
|
{--no-routes= : without routes} |
14
|
|
|
{--routes= : where to store the routes.} |
15
|
|
|
{--force= : override the existing files} |
16
|
|
|
{--laravel : Use Laravel style route definitions} |
17
|
|
|
'; |
18
|
|
|
|
19
|
|
|
protected $description = 'Generates RESTful controller using the RESTActions trait'; |
20
|
|
|
|
21
|
|
|
public function handle() |
22
|
|
|
{ |
23
|
|
|
$model = $this->argument('model'); |
24
|
|
|
$name = ''; |
|
|
|
|
25
|
|
|
if(strrpos($model, "\\") === false){ |
26
|
|
|
$name = $model; |
27
|
|
|
$model = "App\\" . $model; |
28
|
|
|
} else { |
29
|
|
|
$name = explode("\\", $model); |
30
|
|
|
$name = $name[count($name) - 1]; |
31
|
|
|
} |
32
|
|
|
$controller = ucwords(str_plural($name)) . 'Controller'; |
33
|
|
|
$content = $this->getTemplate('controller') |
34
|
|
|
->with([ |
35
|
|
|
'name' => $controller, |
36
|
|
|
'model' => $model, |
37
|
|
|
'namespace' => $this->getNamespace(), |
38
|
|
|
'use' => ($this->getNamespace() != $this->getDefaultNamespace()?'use '.$this->getDefaultNamespace().'\Controller;'.PHP_EOL.'use '.$this->getDefaultNamespace().'\RESTActions;'.PHP_EOL:'') |
39
|
|
|
]) |
40
|
|
|
->get(); |
41
|
|
|
|
42
|
|
|
$this->save($content, "./{$this->option('path')}/{$controller}.php", "{$controller}"); |
43
|
|
|
|
44
|
|
|
if(! $this->option('no-routes')){ |
45
|
|
|
$options = [ |
46
|
|
|
'resource' => snake_case($name, '-'), |
47
|
|
|
'--controller' => $controller, |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
if ($this->option('laravel')) { |
51
|
|
|
$options['--laravel'] = true; |
52
|
|
|
} |
53
|
|
|
if ($this->option('routes')) { |
54
|
|
|
$options['--path'] = $this->option('routes'); |
55
|
|
|
} |
56
|
|
|
if ($this->getNamespace() != $this->getDefaultNamespace()) { |
57
|
|
|
$options['--controller-namespace'] = $this->getNamespace(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->call('wn:route', $options); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getDefaultNamespace() { |
65
|
|
|
return $this->getNamespace(ControllerCommand::DEFAULT_PATH); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.