Files
WMS/Modules/Countries/Providers/CountriesServiceProvider.php
T

154 lines
3.8 KiB
PHP

<?php
namespace Modules\Countries\Providers;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Modules\Countries\Support\ModuleInfo;
class CountriesServiceProvider extends ServiceProvider
{
protected string $moduleName = 'Countries';
protected string $moduleNameLower = 'countries';
/**
* Register module services.
*/
public function register(): void
{
$this->app->register(RouteServiceProvider::class);
}
/**
* Bootstrap module services.
*/
public function boot(): void
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerHelpers();
$this->registerCommands();
$this->registerMigrations();
}
/**
* Register module migrations using the real filesystem path.
*/
protected function registerMigrations(): void
{
$path = dirname(__DIR__).'/Database/Migrations';
if (is_dir($path)) {
$this->loadMigrationsFrom($path);
}
}
/**
* Register module helper files.
*/
protected function registerHelpers(): void
{
foreach (glob(dirname(__DIR__).'/Helpers/*.php') ?: [] as $filename) {
require_once $filename;
}
}
/**
* Register console commands when present.
*/
protected function registerCommands(): void
{
$commandPath = dirname(__DIR__).'/Console';
if (! is_dir($commandPath)) {
return;
}
$commands = collect(File::allFiles($commandPath))
->map(fn ($file): string => 'Modules\\'.$this->moduleName.'\\Console\\'.$file->getFilenameWithoutExtension())
->filter(fn (string $class): bool => class_exists($class))
->values()
->all();
if ($commands !== []) {
$this->commands($commands);
}
}
/**
* Register module config.
*/
protected function registerConfig(): void
{
$sourcePath = dirname(__DIR__).'/Config/config.php';
if (! is_file($sourcePath)) {
return;
}
$this->publishes([
$sourcePath => config_path(ModuleInfo::nameLower().'.php'),
], 'config');
$this->mergeConfigFrom($sourcePath, ModuleInfo::nameLower());
}
/**
* Register module views.
*/
protected function registerViews(): void
{
$viewPath = resource_path('views/modules/'.ModuleInfo::nameLower());
$sourcePath = dirname(__DIR__).'/Resources/views';
if (! is_dir($sourcePath)) {
return;
}
$this->publishes([
$sourcePath => $viewPath,
], ['views', ModuleInfo::nameLower().'-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), ModuleInfo::nameLower());
}
/**
* Register module translations.
*/
protected function registerTranslations(): void
{
$publishedPath = resource_path('lang/modules/'.ModuleInfo::nameLower());
$sourcePath = dirname(__DIR__).'/Resources/lang';
if (is_dir($publishedPath)) {
$this->loadTranslationsFrom($publishedPath, ModuleInfo::nameLower());
return;
}
if (is_dir($sourcePath)) {
$this->loadTranslationsFrom($sourcePath, ModuleInfo::nameLower());
}
}
/**
* Get published view paths.
*
* @return array<int, string>
*/
private function getPublishableViewPaths(): array
{
$paths = [];
foreach (Config::get('view.paths', []) as $path) {
if (is_dir($path.'/modules/'.ModuleInfo::nameLower())) {
$paths[] = $path.'/modules/'.ModuleInfo::nameLower();
}
}
return $paths;
}
}