90 lines
2.7 KiB
PHP
90 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Nwidart\Modules\Support;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
|
|
abstract class ModuleServiceProvider extends ServiceProvider
|
|
{
|
|
protected string $name;
|
|
protected string $nameLower;
|
|
protected array $providers = [];
|
|
|
|
public function boot(): void
|
|
{
|
|
$this->registerTranslations();
|
|
$this->registerConfig();
|
|
$this->registerViews();
|
|
$this->loadMigrationsFrom(module_path($this->name, 'Database/migrations'));
|
|
|
|
if (method_exists($this, 'configureSchedules')) {
|
|
$this->app->booted(function () {
|
|
$schedule = $this->app->make(Schedule::class);
|
|
$this->configureSchedules($schedule);
|
|
});
|
|
}
|
|
}
|
|
|
|
public function register(): void
|
|
{
|
|
foreach ($this->providers as $provider) {
|
|
$this->app->register($provider);
|
|
}
|
|
}
|
|
|
|
protected function registerConfig(): void
|
|
{
|
|
$configPath = module_path($this->name, 'config/config.php');
|
|
if (file_exists($configPath)) {
|
|
$this->publishes([
|
|
$configPath => config_path($this->nameLower . '.php'),
|
|
], 'config');
|
|
$this->mergeConfigFrom(
|
|
$configPath, $this->nameLower
|
|
);
|
|
}
|
|
}
|
|
|
|
protected function registerViews(): void
|
|
{
|
|
$viewPath = resource_path('views/modules/' . $this->nameLower);
|
|
$sourcePath = module_path($this->name, 'resources/views');
|
|
|
|
$this->publishes([
|
|
$sourcePath => $viewPath
|
|
], ['views', $this->nameLower . '-module-views']);
|
|
|
|
if (is_dir($sourcePath)) {
|
|
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->nameLower);
|
|
}
|
|
}
|
|
|
|
protected function registerTranslations(): void
|
|
{
|
|
$langPath = resource_path('lang/modules/' . $this->nameLower);
|
|
|
|
if (is_dir($langPath)) {
|
|
$this->loadTranslationsFrom($langPath, $this->nameLower);
|
|
$this->loadJsonTranslationsFrom($langPath);
|
|
} else {
|
|
$sourceLangPath = module_path($this->name, 'resources/lang');
|
|
if (is_dir($sourceLangPath)) {
|
|
$this->loadTranslationsFrom($sourceLangPath, $this->nameLower);
|
|
$this->loadJsonTranslationsFrom($sourceLangPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getPublishableViewPaths(): array
|
|
{
|
|
$paths = [];
|
|
foreach (\Config::get('view.paths') as $path) {
|
|
if (is_dir($path . '/modules/' . $this->nameLower)) {
|
|
$paths[] = $path . '/modules/' . $this->nameLower;
|
|
}
|
|
}
|
|
return $paths;
|
|
}
|
|
}
|