178 lines
4.8 KiB
PHP
178 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Licensing\Providers;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Database\Eloquent\Factory;
|
|
use Illuminate\Support\Facades\File;
|
|
use Modules\Licensing\Support\ModuleInfo;
|
|
use Config;
|
|
|
|
class LicensingServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* @var string $moduleName
|
|
*/
|
|
protected string $moduleName = 'Licensing';
|
|
|
|
/**
|
|
* @var string $moduleNameLower
|
|
*/
|
|
protected string $moduleNameLower = 'licensing';
|
|
|
|
|
|
/**
|
|
* Get the services provided by the provider.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function provides():array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Boot the application events.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot() : void
|
|
{
|
|
|
|
\Illuminate\Support\Facades\Blade::anonymousComponentPath(
|
|
base_path('modules/Licensing/Resources/views/components/layouts'),
|
|
'layouts'
|
|
);
|
|
|
|
RateLimiter::for('login', fn (Request $request) => Limit::perMinute(5)
|
|
->by(strtolower((string) $request->input('email')).'|'.$request->ip()));
|
|
|
|
RateLimiter::for('registration', fn (Request $request) =>
|
|
Limit::perHour(5)->by($request->ip())
|
|
);
|
|
|
|
RateLimiter::for('license-activation', function (Request $request): array {
|
|
return [
|
|
Limit::perMinute(12)->by($request->ip()),
|
|
Limit::perMinute(6)->by(hash('sha256', (string) $request->input('installationID'))),
|
|
];
|
|
});
|
|
$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) {
|
|
$class = 'Modules\\' . $this->moduleName . '\\Console\\' . $file->getFilenameWithoutExtension();
|
|
return $class;
|
|
})
|
|
->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()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|