Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit 6d90d9c

Browse files
committed
Add ObserverCommand and RuleCommand
1 parent 3960b8e commit 6d90d9c

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

config/domain-commands.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
'model' => null,
2828

29+
'observer' => null,
30+
31+
'rule' => null,
32+
2933
],
3034

3135
];

src/Commands/ObserverCommand.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Signifly\Console\Commands;
4+
5+
use Symfony\Component\Console\Input\InputOption;
6+
use Illuminate\Foundation\Console\ObserverMakeCommand;
7+
8+
class ObserverCommand extends ObserverMakeCommand
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'domain:observer';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new observer class for a given domain';
23+
24+
/**
25+
* Get the default namespace for the class.
26+
*
27+
* @param string $rootNamespace
28+
* @return string
29+
*/
30+
protected function getDefaultNamespace($rootNamespace)
31+
{
32+
if ($domain = $this->option('domain')) {
33+
return "{$rootNamespace}\\{$domain}\\Observers";
34+
}
35+
36+
$defaultNamespace = config('domain-commands.default_namespace');
37+
38+
return "{$rootNamespace}\\{$defaultNamespace}\\Observers";
39+
}
40+
41+
/**
42+
* Get the destination class path.
43+
*
44+
* @param string $name
45+
* @return string
46+
*/
47+
protected function getPath($name)
48+
{
49+
return $this->laravel->basePath().'/app/'.str_replace('\\', '/', $name).'.php';
50+
}
51+
52+
/**
53+
* Get the stub file for the generator.
54+
*
55+
* @return string
56+
*/
57+
protected function getStub()
58+
{
59+
$stubPath = config('domain-commands.stubs.rule');
60+
61+
if (! is_null($stubPath) && is_string($stubPath)) {
62+
return $stubPath;
63+
}
64+
65+
return parent::getStub();
66+
}
67+
68+
/**
69+
* Get the root namespace for the class.
70+
*
71+
* @return string
72+
*/
73+
protected function rootNamespace()
74+
{
75+
return 'Domain\\';
76+
}
77+
78+
/**
79+
* Get the console command options.
80+
*
81+
* @return array
82+
*/
83+
protected function getOptions()
84+
{
85+
return array_merge(parent::getOptions(), [
86+
['domain', 'd', InputOption::VALUE_OPTIONAL, 'Set the domain of the model'],
87+
]);
88+
}
89+
}

src/Commands/RuleCommand.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Signifly\Console\Commands;
4+
5+
use Symfony\Component\Console\Input\InputOption;
6+
use Illuminate\Foundation\Console\RuleMakeCommand;
7+
8+
class RuleCommand extends RuleMakeCommand
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'domain:rule';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new validation rule for a given domain';
23+
24+
/**
25+
* Get the default namespace for the class.
26+
*
27+
* @param string $rootNamespace
28+
* @return string
29+
*/
30+
protected function getDefaultNamespace($rootNamespace)
31+
{
32+
if ($domain = $this->option('domain')) {
33+
return "{$rootNamespace}\\{$domain}\\Rules";
34+
}
35+
36+
$defaultNamespace = config('domain-commands.default_namespace');
37+
38+
return "{$rootNamespace}\\{$defaultNamespace}\\Rules";
39+
}
40+
41+
/**
42+
* Get the destination class path.
43+
*
44+
* @param string $name
45+
* @return string
46+
*/
47+
protected function getPath($name)
48+
{
49+
return $this->laravel->basePath().'/app/'.str_replace('\\', '/', $name).'.php';
50+
}
51+
52+
/**
53+
* Get the stub file for the generator.
54+
*
55+
* @return string
56+
*/
57+
protected function getStub()
58+
{
59+
$stubPath = config('domain-commands.stubs.rule');
60+
61+
if (! is_null($stubPath) && is_string($stubPath)) {
62+
return $stubPath;
63+
}
64+
65+
return parent::getStub();
66+
}
67+
68+
/**
69+
* Get the root namespace for the class.
70+
*
71+
* @return string
72+
*/
73+
protected function rootNamespace()
74+
{
75+
return 'Domain\\';
76+
}
77+
78+
/**
79+
* Get the console command options.
80+
*
81+
* @return array
82+
*/
83+
protected function getOptions()
84+
{
85+
return array_merge(parent::getOptions(), [
86+
['domain', 'd', InputOption::VALUE_OPTIONAL, 'Set the domain of the model'],
87+
]);
88+
}
89+
}

src/DomainServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Illuminate\Support\ServiceProvider;
66
use Signifly\Console\Commands\DTOCommand;
77
use Signifly\Console\Commands\EnumCommand;
8+
use Signifly\Console\Commands\RuleCommand;
89
use Signifly\Console\Commands\EventCommand;
910
use Signifly\Console\Commands\ModelCommand;
1011
use Signifly\Console\Commands\ActionCommand;
12+
use Signifly\Console\Commands\ObserverCommand;
1113

1214
class DomainServiceProvider extends ServiceProvider
1315
{
@@ -24,6 +26,8 @@ public function boot()
2426
EnumCommand::class,
2527
EventCommand::class,
2628
ModelCommand::class,
29+
ObserverCommand::class,
30+
RuleCommand::class,
2731
]);
2832
}
2933

0 commit comments

Comments
 (0)