1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrightComponents\Services\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Illuminate\Console\GeneratorCommand; |
7
|
|
|
use BrightComponents\Services\Exceptions\InvalidNamespaceException; |
8
|
|
|
|
9
|
|
|
class ServiceMakeCommand extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'adr:service {name}'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Create new service definition and handler classes'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The type of class being generated. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type = 'Service'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Execute the console command. |
34
|
|
|
*/ |
35
|
|
|
public function handle() |
36
|
|
|
{ |
37
|
|
|
if (false === parent::handle() && ! $this->option('force')) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the stub file for the generator. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function getStub() |
48
|
|
|
{ |
49
|
|
|
return __DIR__.'/stubs/service.stub'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the default namespace for the class. |
54
|
|
|
* |
55
|
|
|
* @param string $rootNamespace |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected function getDefaultNamespace($rootNamespace) |
60
|
|
|
{ |
61
|
|
|
$serviceRootNamespace = Config::get('service-classes.namespace'); |
62
|
|
|
|
63
|
|
|
if (! $serviceRootNamespace) { |
64
|
|
|
throw InvalidNamespaceException::missingServiceNamespace(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $rootNamespace.'\\'.$serviceRootNamespace; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the method name for the class. |
72
|
|
|
* |
73
|
|
|
* @param string $rootNamespace |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getMethodName() |
78
|
|
|
{ |
79
|
|
|
return Config::get('service-classes.method', 'run'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the desired class name from the input. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getNameInput() |
88
|
|
|
{ |
89
|
|
|
$input = $input = studly_case(trim($this->argument('name'))); |
90
|
|
|
$suffix = Config::get('service-classes.suffix'); |
91
|
|
|
|
92
|
|
|
if (Config::get('service-classes.override_duplicate_suffix')) { |
93
|
|
|
return str_finish($input, $suffix); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $input.$suffix; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Build the class with the given name. |
101
|
|
|
* |
102
|
|
|
* @param string $name |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function buildClass($name) |
107
|
|
|
{ |
108
|
|
|
$stub = $this->files->get($this->getStub()); |
109
|
|
|
|
110
|
|
|
return $this->replaceNamespace($stub, $name)->replaceMethod($stub)->replaceClass($stub, $name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Replace the method name in the given stub. |
115
|
|
|
* |
116
|
|
|
* @param string $stub |
117
|
|
|
* @param string $name |
|
|
|
|
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
protected function replaceMethod(&$stub) |
122
|
|
|
{ |
123
|
|
|
$stub = str_replace( |
124
|
|
|
['DummyMethod'], |
125
|
|
|
[$this->getMethodName()], |
126
|
|
|
$stub |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.