Skip to content

Commit e5b0e48

Browse files
committed
Add event generator
1 parent 8fb6412 commit e5b0e48

19 files changed

+542
-68
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
app
22
composer.lock
33
vendor
4+
*.php_cs.cache

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
}
1212
],
1313
"require": {
14-
"illuminate/console": "^5.4",
15-
"illuminate/support": "^5.4",
16-
"illuminate/filesystem": "^5.4"
14+
"illuminate/console": "^5.7",
15+
"illuminate/support": "^5.7",
16+
"illuminate/filesystem": "^5.7"
1717
},
1818
"autoload": {
1919
"psr-4": {

src/Commands/AppAttributeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function getClassName()
7575
*/
7676
protected function getDefaultNamespace($rootNamespace)
7777
{
78-
$namespace = $this->option('namespace') ?? $this->argument('name');
78+
$namespace = $this->option('namespace') ?? $this->argument('name');
7979
return $rootNamespace . '\Models' . '\\' . $namespace . '\Traits' . '\\' . $this->type;
8080
}
8181
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Hariadi\Boilerplate\Commands;
4+
5+
class AppEventCreatedCommand extends AppGeneratorCommand
6+
{
7+
/**
8+
* The type of class being generated.
9+
*
10+
* @var string
11+
*/
12+
protected $type = 'Created';
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'event:created
20+
{name : The name of the class}
21+
{--E|event=* : Default created event}';
22+
23+
/**
24+
* Default events
25+
*
26+
* @var array
27+
*/
28+
protected $events = ['created', 'updated', 'deleted'];
29+
30+
/**
31+
* The console command description.
32+
*
33+
* @var string
34+
*/
35+
protected $description = 'Create a new created event for model';
36+
37+
/**
38+
* The methods available.
39+
*
40+
* @var array
41+
*/
42+
protected function getMethods()
43+
{
44+
return [];
45+
}
46+
47+
/**
48+
* Get the stub file for the generator.
49+
*
50+
* @return string
51+
*/
52+
protected function getStub()
53+
{
54+
return __DIR__.'/stubs/event.stub';
55+
}
56+
57+
/**
58+
* Execute the console command.
59+
*
60+
* @return mixed
61+
*/
62+
public function handle()
63+
{
64+
$events = $this->option('event');
65+
66+
if (empty($events)) {
67+
$events = $this->events;
68+
}
69+
70+
if (parent::handle() !== false) {
71+
if (in_array('updated', $events)) {
72+
$this->call('event:updated', [
73+
'name' => $this->argument('name'),
74+
]);
75+
}
76+
77+
if (in_array('deleted', $events)) {
78+
$this->call('event:deleted', [
79+
'name' => $this->argument('name'),
80+
]);
81+
}
82+
83+
84+
$this->call('app:listener', [
85+
'name' => $this->argument('name')
86+
]);
87+
88+
$this->info('You need to subscribe the listener to your EventServiceProvider manually');
89+
}
90+
}
91+
92+
/**
93+
* Get the intended name for class.
94+
*
95+
* @return string
96+
*/
97+
protected function getClassName()
98+
{
99+
return basename($this->getNameInput()) . $this->type;
100+
}
101+
102+
/**
103+
* Get the default namespace for the class.
104+
*
105+
* @param string $rootNamespace
106+
* @return string
107+
*/
108+
protected function getDefaultNamespace($rootNamespace)
109+
{
110+
$namespace = $this->argument('name');
111+
112+
return $rootNamespace . '\Events\Backend' . '\\' . $namespace;
113+
}
114+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Hariadi\Boilerplate\Commands;
4+
5+
class AppEventDeletedCommand extends AppGeneratorCommand
6+
{
7+
/**
8+
* The type of class being generated.
9+
*
10+
* @var string
11+
*/
12+
protected $type = 'Deleted';
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'event:deleted {name : The name of the class}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Create a new deleted event for model';
27+
28+
/**
29+
* The methods available.
30+
*
31+
* @var array
32+
*/
33+
protected function getMethods()
34+
{
35+
return [];
36+
}
37+
38+
/**
39+
* Get the stub file for the generator.
40+
*
41+
* @return string
42+
*/
43+
protected function getStub()
44+
{
45+
return __DIR__.'/stubs/event.stub';
46+
}
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return mixed
52+
*/
53+
public function handle()
54+
{
55+
parent::handle();
56+
}
57+
58+
/**
59+
* Get the intended name for class.
60+
*
61+
* @return string
62+
*/
63+
protected function getClassName()
64+
{
65+
return basename($this->getNameInput()) . $this->type;
66+
}
67+
68+
/**
69+
* Get the default namespace for the class.
70+
*
71+
* @param string $rootNamespace
72+
* @return string
73+
*/
74+
protected function getDefaultNamespace($rootNamespace)
75+
{
76+
$namespace = $this->argument('name');
77+
78+
return $rootNamespace . '\Events\Backend' . '\\' . $namespace;
79+
}
80+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Hariadi\Boilerplate\Commands;
4+
5+
class AppEventListenerCommand extends AppGeneratorCommand
6+
{
7+
/**
8+
* The type of class being generated.
9+
*
10+
* @var string
11+
*/
12+
protected $type = 'EventListener';
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'app:listener {name : The name of the class}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Create a new event listener for model';
27+
28+
/**
29+
* The methods available.
30+
*
31+
* @var array
32+
*/
33+
protected function getMethods()
34+
{
35+
return ['created', 'updated', 'deleted'];
36+
}
37+
38+
/**
39+
* Get the stub file for the generator.
40+
*
41+
* @return string
42+
*/
43+
protected function getStub()
44+
{
45+
return __DIR__.'/stubs/listener.stub';
46+
}
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return mixed
52+
*/
53+
public function handle()
54+
{
55+
// $event = $this->option('event');
56+
57+
// if ($event != 'created') {
58+
// $this->methods = [$event];
59+
// }
60+
61+
// dd($this->methods);
62+
63+
parent::handle();
64+
}
65+
66+
/**
67+
* Get the intended name for class.
68+
*
69+
* @return string
70+
*/
71+
protected function getClassName()
72+
{
73+
return basename($this->getNameInput()) . $this->type;
74+
}
75+
76+
/**
77+
* Get the default namespace for the class.
78+
*
79+
* @param string $rootNamespace
80+
* @return string
81+
*/
82+
protected function getDefaultNamespace($rootNamespace)
83+
{
84+
$namespace = $this->argument('name');
85+
86+
return $rootNamespace . '\Listeners\Backend' . '\\' . $namespace;
87+
}
88+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Hariadi\Boilerplate\Commands;
4+
5+
class AppEventUpdatedCommand extends AppGeneratorCommand
6+
{
7+
/**
8+
* The type of class being generated.
9+
*
10+
* @var string
11+
*/
12+
protected $type = 'Updated';
13+
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'event:updated {name : The name of the class}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Create a new updated event for model';
27+
28+
/**
29+
* The methods available.
30+
*
31+
* @var array
32+
*/
33+
protected function getMethods()
34+
{
35+
return [];
36+
}
37+
38+
/**
39+
* Get the stub file for the generator.
40+
*
41+
* @return string
42+
*/
43+
protected function getStub()
44+
{
45+
return __DIR__.'/stubs/event.stub';
46+
}
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return mixed
52+
*/
53+
public function handle()
54+
{
55+
parent::handle();
56+
}
57+
58+
/**
59+
* Get the intended name for class.
60+
*
61+
* @return string
62+
*/
63+
protected function getClassName()
64+
{
65+
return basename($this->getNameInput()) . $this->type;
66+
}
67+
68+
/**
69+
* Get the default namespace for the class.
70+
*
71+
* @param string $rootNamespace
72+
* @return string
73+
*/
74+
protected function getDefaultNamespace($rootNamespace)
75+
{
76+
$namespace = $this->argument('name');
77+
78+
return $rootNamespace . '\Events\Backend' . '\\' . $namespace;
79+
}
80+
}

0 commit comments

Comments
 (0)