|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
7
|
|
|
use Nwidart\Modules\Support\Stub; |
|
8
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
|
|
12
|
|
|
class ObserverMakeCommand extends GeneratorCommand |
|
13
|
|
|
{ |
|
14
|
|
|
use ModuleCommandTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The console command name. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $name = 'module:make-observer'; |
|
22
|
|
|
|
|
23
|
|
|
protected $argumentName = 'name'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The console command description. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $description = 'Create a new observer class for the specified module.'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the default namespace for the class. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $rootNamespace |
|
|
|
|
|
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getDefaultNamespace() : string |
|
39
|
|
|
{ |
|
40
|
|
|
$module = $this->laravel['modules']; |
|
41
|
|
|
|
|
42
|
|
|
return $module->config('paths.generator.observers.namespace') ?: $module->config('paths.generator.observers.path', 'Observers'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the default namespace for the class. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $rootNamespace |
|
|
|
|
|
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getModelNamespace() : string |
|
52
|
|
|
{ |
|
53
|
|
|
$module = $this->laravel['modules']; |
|
54
|
|
|
|
|
55
|
|
|
return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Entities'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get template contents. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getTemplateContents() |
|
64
|
|
|
{ |
|
65
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
|
66
|
|
|
|
|
67
|
|
|
if($this->option('model') ) { |
|
68
|
|
|
return (new Stub('/observer.stub', [ |
|
69
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
|
70
|
|
|
'CLASS' => $this->getClass(), |
|
71
|
|
|
|
|
72
|
|
|
'NAMESPACEDMODEL' => $this->getNamespacedModel(), |
|
73
|
|
|
'DUMMYMODEL' => $this->getModel(), |
|
74
|
|
|
'MODEL' => $this->getModelVariable(), |
|
75
|
|
|
]))->render(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return (new Stub('/observer.plain.stub', [ |
|
79
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
|
80
|
|
|
'CLASS' => $this->getClass(), |
|
81
|
|
|
]))->render(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the destination file path. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getDestinationFilePath() |
|
90
|
|
|
{ |
|
91
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
92
|
|
|
|
|
93
|
|
|
$notificationPath = GenerateConfigReader::read('observers'); |
|
94
|
|
|
|
|
95
|
|
|
return $path . $notificationPath->getPath() . '/' . $this->getFileName() . '.php'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
private function getFileName() |
|
102
|
|
|
{ |
|
103
|
|
|
return Str::studly($this->argument('name')); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
private function getNamespacedModel() |
|
110
|
|
|
{ |
|
111
|
|
|
$model = $this->option('model'); |
|
112
|
|
|
|
|
113
|
|
|
$model = str_replace('/', '\\', $model); |
|
114
|
|
|
|
|
115
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
|
116
|
|
|
$namespaceModel = $this->getClassNamespace($module, $this->getModelNamespace() ) . '\\' . $model; |
|
117
|
|
|
|
|
118
|
|
|
if (Str::startsWith($model, '\\')) { |
|
119
|
|
|
return trim($model, '\\'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $namespaceModel; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
private function getModel() |
|
129
|
|
|
{ |
|
130
|
|
|
$model = $this->option('model'); |
|
131
|
|
|
|
|
132
|
|
|
$model = class_basename(trim($model, '\\')); |
|
133
|
|
|
|
|
134
|
|
|
return $model; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getModelVariable() |
|
141
|
|
|
{ |
|
142
|
|
|
$model = $this->getModel(); |
|
143
|
|
|
|
|
144
|
|
|
return '$' . Str::camel($model); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the console command arguments. |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
128 |
|
protected function getArguments() |
|
153
|
|
|
{ |
|
154
|
|
|
return [ |
|
155
|
128 |
|
['name', InputArgument::REQUIRED, 'The name of the observer class.'], |
|
156
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get the console command arguments. |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
128 |
|
protected function getOptions() |
|
166
|
|
|
{ |
|
167
|
|
|
return [ |
|
168
|
128 |
|
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the observer applies to.'], |
|
169
|
|
|
]; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.