/
BespredeL
/
encryption-form
Обзор
Документация
Войти
/
BespredeL
/
encryption-form
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
Аналитика
Безопасность
master
src/EncryptionFormServiceProvider.php
203 строки
5 KB
Aleksandr Kireev
add: Добавлены baseline-тесты для Render, ServiceProvider и helper
25 апр 2026, 18:44
25 апр 2026, 18:44
f92b3c4
Код
Авторство
О чём код?
<?php declare(strict_types=1); namespace Bespredel\EncryptionForm; use Bespredel\EncryptionForm\Blade\Directives; use Bespredel\EncryptionForm\Console\Commands\GenerateEncryptionKeys; use Bespredel\EncryptionForm\Middleware\DecryptRequestFields; use Bespredel\EncryptionForm\Services\Contracts\DecryptorInterface; use Bespredel\EncryptionForm\Services\Decryptor; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Log; use Illuminate\Support\ServiceProvider; class EncryptionFormServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @param Router $router * * @return void * * @throws \Exception */ public function boot(Router $router): void { $langPath = $this->getLangPath('vendor/encryption-form'); $this->registerPublishGroups($langPath); $this->loadPackageTranslations($langPath); $this->registerMiddleware($router); $this->registerCommands(); $this->registerBladeDirectives(); } /** * Register the application services. * * @return void */ public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/encryption-form.php', 'encryption-form'); $this->registerBindings(); $this->validateConfiguration(); $this->registerHelpers(); } /** * Validate encryption form configuration * * @return void */ protected function validateConfiguration(): void { if (!config('encryption-form.enabled', true)) { return; } $publicKey = config('encryption-form.public_key'); $privateKey = config('encryption-form.private_key'); // Only validate if keys are set if ($publicKey !== null || $privateKey !== null) { if (empty($publicKey)) { Log::warning('Encryption form is enabled but public key is not set'); } if (empty($privateKey)) { Log::warning('Encryption form is enabled but private key is not set'); } // Validate key format if both are set if (!empty($publicKey) && !empty($privateKey)) { $publicKeyResource = openssl_pkey_get_public($publicKey); $privateKeyResource = openssl_pkey_get_private($privateKey); if ($publicKeyResource === false) { Log::warning('Encryption form public key is invalid'); } if ($privateKeyResource === false) { Log::warning('Encryption form private key is invalid'); } } } } /** * Register middleware. * * @param Router $router * * @return void */ public function registerMiddleware(Router $router): void { $router->aliasMiddleware('decrypt-form', DecryptRequestFields::class); } /** * Register the commands. * * @return void */ public function registerCommands(): void { $this->commands([ GenerateEncryptionKeys::class, ]); if (config('encryption-form.enabled', true) && config('encryption-form.key_rotation.enabled', false)) { $this->app->booted(function (): void { app(Schedule::class) ->command('encryption-form:generate-keys') ->cron(config('encryption-form.key_rotation.cron_expression', '0 0 * * *')); }); } } /** * Register the bindings. * * @return void */ protected function registerBindings(): void { $this->app->bind(DecryptorInterface::class, Decryptor::class); } /** * Register the helpers. * * @return void */ protected function registerHelpers(): void { if (file_exists(__DIR__ . '/Support/helpers.php')) { require_once __DIR__ . '/Support/helpers.php'; } } /** * Register the publish groups. * * @param string $langPath * * @return void */ protected function registerPublishGroups(string $langPath): void { $this->publishes([ __DIR__ . '/../config/encryption-form.php' => config_path('encryption-form.php'), __DIR__ . '/../resources/js' => public_path('vendor/encryption-form/js'), __DIR__ . '/../resources/css' => public_path('vendor/encryption-form/css'), __DIR__ . '/../resources/lang' => $langPath, ], 'encryption-form'); } /** * Load the package translations. * * @param string $langPath * * @return void */ protected function loadPackageTranslations(string $langPath): void { if (!is_dir($langPath)) { $langPath = __DIR__ . '/../resources/lang'; } $this->loadTranslationsFrom($langPath, 'encryption-form'); } /** * Register the blade directives. * * @return void * @throws \Exception */ protected function registerBladeDirectives(): void { Directives::register(); } /** * Get the language path. * * @param string $relativePath * * @return string */ protected function getLangPath(string $relativePath): string { return function_exists('lang_path') ? lang_path($relativePath) : resource_path('lang/' . $relativePath); } }