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
|
|
|
|
11
|
|
View Code Duplication |
class EventMakeCommand 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-event'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Create a new event class for the specified module'; |
30
|
|
|
|
31
|
4 |
|
public function getTemplateContents() |
32
|
|
|
{ |
33
|
4 |
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
34
|
|
|
|
35
|
4 |
|
return (new Stub('/event.stub', [ |
36
|
4 |
|
'NAMESPACE' => $this->getClassNamespace($module), |
37
|
4 |
|
'CLASS' => $this->getClass(), |
38
|
4 |
|
]))->render(); |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
public function getDestinationFilePath() |
42
|
|
|
{ |
43
|
4 |
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
44
|
|
|
|
45
|
4 |
|
$eventPath = GenerateConfigReader::read('event'); |
46
|
|
|
|
47
|
4 |
|
return $path . $eventPath->getPath() . '/' . $this->getFileName() . '.php'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
4 |
|
protected function getFileName() |
54
|
|
|
{ |
55
|
4 |
|
return Str::studly($this->argument('name')); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
public function getDefaultNamespace() : string |
59
|
|
|
{ |
60
|
4 |
|
$module = $this->laravel['modules']; |
61
|
|
|
|
62
|
4 |
|
return $module->config('paths.generator.event.namespace') ?: $module->config('paths.generator.event.path', 'Events'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the console command arguments. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
120 |
|
protected function getArguments() |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
120 |
|
['name', InputArgument::REQUIRED, 'The name of the event.'], |
74
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.