1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BiiiiiigMonster\LaravelEnum; |
4
|
|
|
|
5
|
|
|
use BiiiiiigMonster\LaravelEnum\Commands\EnumMakeCommand; |
6
|
|
|
use BiiiiiigMonster\LaravelEnum\Commands\EnumMetaMakeCommand; |
7
|
|
|
use BiiiiiigMonster\LaravelEnum\Commands\EnumPhpdocCommand; |
8
|
|
|
use BiiiiiigMonster\LaravelEnum\Rules\Enum; |
9
|
|
|
use BiiiiiigMonster\LaravelEnum\Rules\EnumMeta; |
10
|
|
|
use Illuminate\Support\Facades\Validator; |
11
|
|
|
use Illuminate\Support\ServiceProvider; |
12
|
|
|
|
13
|
|
|
class EnumServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
const LANG_NAMESPACE = 'enum'; |
16
|
|
|
|
17
|
|
|
public function boot(): void |
18
|
|
|
{ |
19
|
|
|
$this->bootCommands(); |
20
|
|
|
$this->bootTranslations(); |
21
|
|
|
$this->bootValidators(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function bootCommands(): void |
25
|
|
|
{ |
26
|
|
|
$this->publishes([ |
27
|
|
|
__DIR__.'/Commands/stubs' => $this->app->basePath('stubs'), |
28
|
|
|
], 'stubs'); |
29
|
|
|
|
30
|
|
|
if ($this->app->runningInConsole()) { |
31
|
|
|
$this->commands([ |
32
|
|
|
EnumPhpdocCommand::class, |
33
|
|
|
EnumMakeCommand::class, |
34
|
|
|
EnumMetaMakeCommand::class, |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function bootValidators(): void |
40
|
|
|
{ |
41
|
|
|
Validator::extend('enumerate', function ($attribute, $value, $parameters, $validator) { |
|
|
|
|
42
|
|
|
return (new Enum(...$parameters))->passes($attribute, $value); |
|
|
|
|
43
|
|
|
}, trans(static::LANG_NAMESPACE.'::validations.enumerate')); |
44
|
|
|
|
45
|
|
|
Validator::extend('enum_meta', function ($attribute, $value, $parameters, $validator) { |
|
|
|
|
46
|
|
|
return (new EnumMeta(...$parameters))->passes($attribute, $value); |
|
|
|
|
47
|
|
|
}, trans(static::LANG_NAMESPACE.'::validations.enum_meta')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function bootTranslations(): void |
51
|
|
|
{ |
52
|
|
|
$this->publishes([ |
53
|
|
|
__DIR__.'/../lang' => $this->app->langPath('vendor/'.static::LANG_NAMESPACE), |
54
|
|
|
], 'translations'); |
55
|
|
|
|
56
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../lang', static::LANG_NAMESPACE); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.