Skip to content

Commit

Permalink
refactor LaravelMyanmarToolsServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
PyaeSoneAungRgn committed Feb 18, 2023
1 parent bd42c65 commit 6dbb055
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/LaravelMyanmarToolsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,84 @@

class LaravelMyanmarToolsServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->getPackageName());

$this->publishes([
__DIR__.'/../resources/lang' => base_path('/lang/vendor/'.$this->getPackageName()),
]);
$this->registerStrMacros();
$this->registerRequestMacros();
$this->registerCollectionMacros();
$this->registerEloquentBuilderMacros();
$this->registerQueryBuilderMacros();
$this->registerValidatorMacros();
$this->registerCarbonMacros();

$this->registerLang();
}

private function registerStrMacros(): void
{
Collection::make($this->strMacros())
->reject(function ($class, $macro) {
return Str::hasMacro($macro);
})
->each(function ($class, $macro) {
Str::macro($macro, app($class)());
});
}

private function registerRequestMacros(): void
{
Collection::make($this->requestMacros())
->reject(function ($class, $macro) {
return Request::hasMacro($macro);
})
->each(function ($class, $macro) {
Request::macro($macro, app($class)());
});
}

private function registerCollectionMacros(): void
{
Collection::make($this->collectionMacros())
->reject(function ($class, $macro) {
return Collection::hasMacro($macro);
})
->each(function ($class, $macro) {
Collection::macro($macro, app($class)());
});
}

private function registerEloquentBuilderMacros(): void
{
Collection::make($this->eloquentBuilderMacros())
->reject(function ($class, $macro) {
return EloquentBuilder::hasGlobalMacro($macro);
})
->each(function ($class, $macro) {
EloquentBuilder::macro($macro, app($class)());
});
}

private function registerQueryBuilderMacros(): void
{
Collection::make($this->queryBuilderMacros())
->reject(function ($class, $macro) {
return QueryBuilder::hasMacro($macro);
})
->each(function ($class, $macro) {
QueryBuilder::macro($macro, app($class)());
});
}

private function registerValidatorMacros(): void
{
Collection::make($this->validatorExtends())
->each(function ($class, $extend) {
Validator::extend($extend, app($class)(), $this->getErrorMessage($extend));
});
}

private function registerCarbonMacros(): void
{
Collection::make($this->carbonMacros())
->reject(function ($class, $macro) {
return Carbon::hasMacro($macro);
Expand All @@ -75,6 +100,14 @@ public function boot()
});
}

private function registerLang(): void
{
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', $this->getPackageName());
$this->publishes([
__DIR__.'/../resources/lang' => base_path('/lang/vendor/'.$this->getPackageName()),
]);
}

private function strMacros(): array
{
return [
Expand Down Expand Up @@ -199,7 +232,7 @@ private function getPackageName(): string
return 'laravelMyanmarTools';
}

private function getErrorMessage($extend)
private function getErrorMessage($extend): mixed
{
return trans($this->getPackageName().'::validation.'.Str::snake($extend));
}
Expand Down

0 comments on commit 6dbb055

Please sign in to comment.