|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Nwidart\Modules\Module; |
|
6
|
|
|
use Nwidart\Modules\Support\Stub; |
|
7
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
|
|
11
|
|
|
class GenerateListenerCommand extends GeneratorCommand |
|
12
|
|
|
{ |
|
13
|
|
|
use ModuleCommandTrait; |
|
14
|
|
|
|
|
15
|
|
|
protected $argumentName = 'name'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name = 'module:make-listener'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Generate a new Listener Class for the specified module'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the console command arguments. |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
56 |
|
protected function getArguments() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
56 |
|
['name', InputArgument::REQUIRED, 'The name of the command.'], |
|
40
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the console command options. |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
56 |
|
protected function getOptions() |
|
50
|
|
|
{ |
|
51
|
|
|
return [ |
|
52
|
56 |
|
['event', null, InputOption::VALUE_REQUIRED, 'Event name this is listening to', null], |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
protected function getTemplateContents() |
|
57
|
|
|
{ |
|
58
|
2 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
|
59
|
|
|
|
|
60
|
2 |
|
return (new Stub('/listener.stub', [ |
|
61
|
2 |
|
'NAMESPACE' => $this->getNamespace($module), |
|
62
|
2 |
|
"EVENTNAME" => $this->getEventName($module), |
|
63
|
2 |
|
"EVENTSHORTENEDNAME" => $this->option('event'), |
|
64
|
2 |
|
"CLASS" => $this->getClass(), |
|
65
|
2 |
|
'DUMMYNAMESPACE' => $this->laravel->getNamespace() . "Events", |
|
66
|
2 |
|
]))->render(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
protected function getDestinationFilePath() |
|
70
|
|
|
{ |
|
71
|
2 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
72
|
|
|
|
|
73
|
2 |
|
$seederPath = $this->laravel['modules']->config('paths.generator.listener'); |
|
74
|
|
|
|
|
75
|
2 |
|
return $path . $seederPath . '/' . $this->getFileName() . '.php'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
2 |
|
protected function getFileName() |
|
82
|
|
|
{ |
|
83
|
2 |
|
return studly_case($this->argument('name')); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
public function handle() |
|
87
|
|
|
{ |
|
88
|
2 |
|
if (!$this->option('event')) { |
|
89
|
|
|
$this->error('The --event option is necessary'); |
|
90
|
|
|
|
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
parent::handle(); |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
protected function getEventName(Module $module) |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.event') . "\\" . $this->option('event'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
private function getNamespace($module) |
|
103
|
|
|
{ |
|
104
|
2 |
|
$namespace = str_replace('/', '\\', config('modules.paths.generator.listener')); |
|
105
|
|
|
|
|
106
|
2 |
|
return $this->getClassNamespace($module) . "\\" . $namespace; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.