1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apiato\Core\Generator\Commands; |
4
|
|
|
|
5
|
|
|
use Apiato\Core\Generator\GeneratorCommand; |
6
|
|
|
use Apiato\Core\Generator\Interfaces\ComponentsGenerator; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EventGenerator |
12
|
|
|
* |
13
|
|
|
* @author Johannes Schobel <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class EventGenerator extends GeneratorCommand implements ComponentsGenerator |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command name. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'apiato:generate:event'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Create a new Event class and its corresponding Handler'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The type of class being generated. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $fileType = 'Event'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The structure of the file path. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $pathStructure = '{container-name}/Events/Events/*'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The structure of the file name. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $nameStructure = '{file-name}'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The name of the stub file. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $stubName = 'events/event.stub'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* User required/optional inputs expected to be passed while calling the command. |
62
|
|
|
* This is a replacement of the `getArguments` function "which reads whenever it's called". |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
public $inputs = [ |
67
|
|
|
['model', null, InputOption::VALUE_OPTIONAL, 'The model to generate this Event for'], |
68
|
|
|
['handler', null, InputOption::VALUE_OPTIONAL, 'Generate a Handler for this Event?'], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getUserInputs() |
75
|
|
|
{ |
76
|
|
|
$model = $this->checkParameterOrAsk('model', 'Enter the name of the Model to generate this Event for'); |
77
|
|
|
|
78
|
|
|
$handler = $this->checkParameterOrConfirm('handler', 'Do you want to generate a Handler for this Event?', true); |
79
|
|
View Code Duplication |
if($handler) { |
|
|
|
|
80
|
|
|
// we need to generate a corresponding handler |
81
|
|
|
// so call the other command |
82
|
|
|
$status = $this->call('apiato:generate:eventhandler', [ |
83
|
|
|
'--container' => $this->containerName, |
84
|
|
|
'--file' => $this->fileName . 'Handler', |
85
|
|
|
'--event' => $this->fileName |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
if ($status == 0) { |
89
|
|
|
$this->printInfoMessage('The Handler for Event was successfully generated'); |
90
|
|
|
} |
91
|
|
|
else { |
92
|
|
|
$this->printErrorMessage('Could not generate the corresponding Handler!'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->printInfoMessage('!!! Do not forget to register the Event and/or EventHandler !!!'); |
97
|
|
|
|
98
|
|
|
return [ |
99
|
|
|
'path-parameters' => [ |
100
|
|
|
'container-name' => $this->containerName, |
101
|
|
|
], |
102
|
|
|
'stub-parameters' => [ |
103
|
|
|
'_container-name' => Str::lower($this->containerName), |
104
|
|
|
'container-name' => $this->containerName, |
105
|
|
|
'class-name' => $this->fileName, |
106
|
|
|
'model' => $model, |
107
|
|
|
], |
108
|
|
|
'file-parameters' => [ |
109
|
|
|
'file-name' => $this->fileName, |
110
|
|
|
], |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
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.