1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Nwidart\Modules\Module; |
7
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
8
|
|
|
use Nwidart\Modules\Support\Stub; |
9
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
|
13
|
|
|
class ListenerMakeCommand extends GeneratorCommand |
14
|
|
|
{ |
15
|
|
|
use ModuleCommandTrait; |
16
|
|
|
|
17
|
|
|
protected $argumentName = 'name'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $name = 'module:make-listener'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Create a new event listener class for the specified module'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the console command arguments. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
118 |
|
protected function getArguments() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
118 |
|
['name', InputArgument::REQUIRED, 'The name of the command.'], |
42
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the console command options. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
118 |
|
protected function getOptions() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
118 |
|
['event', 'e', InputOption::VALUE_OPTIONAL, 'The event class being listened for.'], |
55
|
|
|
['queued', null, InputOption::VALUE_NONE, 'Indicates the event listener should be queued.'], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
protected function getTemplateContents() |
60
|
|
|
{ |
61
|
7 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
62
|
|
|
|
63
|
7 |
|
return (new Stub($this->getStubName(), [ |
64
|
7 |
|
'NAMESPACE' => $this->getClassNamespace($module), |
65
|
7 |
|
'EVENTNAME' => $this->getEventName($module), |
66
|
7 |
|
'SHORTEVENTNAME' => $this->option('event'), |
67
|
7 |
|
'CLASS' => $this->getClass(), |
68
|
7 |
|
]))->render(); |
69
|
|
|
} |
70
|
|
|
|
71
|
7 |
|
public function getDefaultNamespace() : string |
72
|
|
|
{ |
73
|
7 |
|
return $this->laravel['modules']->config('paths.generator.listener.path', 'Listeners'); |
74
|
|
|
} |
75
|
|
|
|
76
|
7 |
|
protected function getEventName(Module $module) |
77
|
|
|
{ |
78
|
7 |
|
$eventPath = GenerateConfigReader::read('event'); |
79
|
|
|
|
80
|
7 |
|
return $this->getClassNamespace($module) . "\\" . $eventPath->getPath() . "\\" . $this->option('event'); |
81
|
|
|
} |
82
|
|
|
|
83
|
7 |
|
protected function getDestinationFilePath() |
84
|
|
|
{ |
85
|
7 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
86
|
|
|
|
87
|
7 |
|
$listenerPath = GenerateConfigReader::read('listener'); |
88
|
|
|
|
89
|
7 |
|
return $path . $listenerPath->getPath() . '/' . $this->getFileName() . '.php'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
7 |
|
protected function getFileName() |
96
|
|
|
{ |
97
|
7 |
|
return Str::studly($this->argument('name')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
7 |
|
protected function getStubName(): string |
104
|
|
|
{ |
105
|
7 |
|
if ($this->option('queued')) { |
106
|
2 |
|
if ($this->option('event')) { |
107
|
1 |
|
return '/listener-queued.stub'; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return '/listener-queued-duck.stub'; |
111
|
|
|
} |
112
|
|
|
|
113
|
5 |
|
if ($this->option('event')) { |
114
|
2 |
|
return '/listener.stub'; |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
return '/listener-duck.stub'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|