Files
WMS/Modules/M3/Providers/M3ServiceProvider.php
2026-09-21 10:14:57 +01:00

178 lines
4.8 KiB
PHP
Executable File

<?php
namespace Modules\M3\Providers;
use Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Modules\M3\Support\ModuleInfo;
class M3ServiceProvider extends ServiceProvider
{
/**
* @var string
*/
protected string $moduleName = 'M3';
/**
* @var string
*/
protected string $moduleNameLower = 'm3';
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides(): array
{
return [];
}
/**
* Boot the application events.
*
* @return void
*/
public function boot(): void
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerHelpers();
$this->registerCommands();
$this->loadMigrationsFrom(module_path(ModuleInfo::name(), 'Database/Migrations'));
}
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
$this->app->register(RouteServiceProvider::class);
}
/**
* Register the helpers.
*
* @return void
*/
protected function registerHelpers(): void
{
foreach (glob(__DIR__ . '/../Helpers/*.php') as $filename) {
require_once $filename;
}
}
/**
* Dynamically Register Commands.
*
* @return void
*/
public function registerCommands(): void
{
if (! $this->app->runningInConsole()) {
return;
}
$commandPath = __DIR__ . '/../Console';
$commands = collect(File::allFiles($commandPath))
->map(function ($file) {
return 'Modules\\' . $this->moduleName . '\\Console\\' . $file->getFilenameWithoutExtension();
})
->filter(function ($class) {
return class_exists($class);
})
->values()
->all();
$this->commands($commands);
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig(): void
{
$this->publishes([
module_path(ModuleInfo::name(), 'Config/config.php') => config_path(ModuleInfo::nameLower() . '.php'),
], 'config');
$this->mergeConfigFrom(
module_path(ModuleInfo::name(), 'Config/config.php'),
ModuleInfo::nameLower(),
);
// The legacy M3 application reads these keys directly as arrays.* and
// arrays_custom.*. Keep those public config keys while the application
// is hosted inside Core.
$this->mergeConfigFrom(module_path(ModuleInfo::name(), 'Config/arrays.php'), 'arrays');
$this->mergeConfigFrom(module_path(ModuleInfo::name(), 'Config/arrays_custom.php'), 'arrays_custom');
// Legacy M3 storage disks now live under Core storage by default.
if (! config('filesystems.disks.publicLabels')) {
config(['filesystems.disks.publicLabels' => [
'driver' => 'local',
'root' => config('m3.labels_path'),
'visibility' => 'public',
]]);
}
if (! config('filesystems.disks.publicCL')) {
config(['filesystems.disks.publicCL' => [
'driver' => 'local',
'root' => config('m3.closing_orders_path'),
'visibility' => 'public',
]]);
}
}
/**
* Register views.
*
* @return void
*/
public function registerViews(): void
{
$viewPath = resource_path('views/modules/' . ModuleInfo::nameLower());
$sourcePath = module_path(ModuleInfo::name(), 'Resources/views');
$this->publishes([
$sourcePath => $viewPath,
], ['views', ModuleInfo::nameLower() . '-module-views']);
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), ModuleInfo::nameLower());
}
/**
* Register translations.
*
* @return void
*/
public function registerTranslations(): void
{
$langPath = resource_path('lang/modules/' . ModuleInfo::nameLower());
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, ModuleInfo::nameLower());
} else {
$this->loadTranslationsFrom(module_path(ModuleInfo::name(), 'Resources/lang'), ModuleInfo::nameLower());
}
}
/**
* Regsiter View Paths
*
* @return array
*/
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;
}
}