From a2565061bfdda3535cc451ebd2b0578c8715b989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdi=20U=CC=88NAL?= Date: Sun, 8 Sep 2024 12:46:53 +0300 Subject: [PATCH] chore: Refactor NovaTranslationsServiceProvider to improve readability and organization --- config/nova-translations.php | 2 +- src/Nova/TranslationResource.php | 10 +- src/NovaTranslationsServiceProvider.php | 141 ++++++++++++++++++------ src/Services/SaveScan.php | 10 +- 4 files changed, 119 insertions(+), 44 deletions(-) diff --git a/config/nova-translations.php b/config/nova-translations.php index 51ea091..afb7486 100644 --- a/config/nova-translations.php +++ b/config/nova-translations.php @@ -21,7 +21,7 @@ 'patternA' => implode('', [ '[^\w]', // Must not start with any alphanum or _ '(?)', // Must not start with -> - '(' . implode('|', [ + '('.implode('|', [ 'trans', 'trans_choice', 'Lang::get', diff --git a/src/Nova/TranslationResource.php b/src/Nova/TranslationResource.php index 9d325cd..7abf148 100644 --- a/src/Nova/TranslationResource.php +++ b/src/Nova/TranslationResource.php @@ -2,12 +2,12 @@ namespace Ferdiunal\NovaTranslations\Nova; +use App\Nova\Resource; use Ferdiunal\NovaTranslations\Models\Translation; use Ferdiunal\NovaTranslations\TranslationField; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest; -use Laravel\Nova\Resource; class TranslationResource extends Resource { @@ -21,7 +21,11 @@ class TranslationResource extends Resource public static $displayInNavigation = false; public static $search = [ - 'namespace', 'group', 'key', 'text', 'meta', + 'namespace', + 'group', + 'key', + 'text', + 'meta', ]; public function fields(NovaRequest $request) @@ -46,6 +50,6 @@ public static function defaultOrderings($query) { $table = app(static::$model)->getTable(); - return $query->reorder()->orderBy($table . '.namespace', 'asc')->orderBy($table . '.group', 'asc')->orderBy($table . '.key', 'asc'); + return $query->reorder()->orderBy($table.'.namespace', 'asc')->orderBy($table.'.group', 'asc')->orderBy($table.'.key', 'asc'); } } diff --git a/src/NovaTranslationsServiceProvider.php b/src/NovaTranslationsServiceProvider.php index c442085..0fa186f 100644 --- a/src/NovaTranslationsServiceProvider.php +++ b/src/NovaTranslationsServiceProvider.php @@ -21,52 +21,122 @@ class NovaTranslationsServiceProvider extends ServiceProvider */ public function boot() { - $resource = $this->app['config']->get('nova-translations.resource', TranslationResource::class); - - if (!$this->app->configurationIsCached()) { - $this->mergeConfigFrom(__DIR__ . '/../config/nova-translations.php', 'nova-translations'); - } + $this->registerConfig(); + $this->registerResources(); + $this->registerMigrations(); + $this->registerCommands(); + + Nova::serving(function (ServingNova $event) { + $this->registerNovaResources(); + $this->loadNovaTranslations(); + }); + } - if ($this->app->runningInConsole()) { - //Register Migrations - $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + /** + * Register package configuration. + * + * @return void + */ + protected function registerConfig() + { + if (! $this->app->configurationIsCached()) { + $this->mergeConfigFrom(__DIR__.'/../config/nova-translations.php', 'nova-translations'); } + } - $this->publishes([ - __DIR__ . '/../config/nova-translations.php' => config_path('nova-translations.php'), - ], 'nova-translations-config'); + /** + * Register Nova resources. + * + * @return void + */ + protected function registerResources() + { + $resource = $this->getConfiguredResource(); + $this->app->booted(function () use ($resource) { + $this->setTranslationModel($resource); + $this->routes(); + }); + } - //Publish Migrations - $this->publishes([ - __DIR__ . '/../database/migrations' => database_path('migrations'), - ], 'nova-translations-migrations'); + /** + * Register migrations for the package. + * + * @return void + */ + protected function registerMigrations() + { + if ($this->app->runningInConsole()) { + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); - $this->app->booted(function () use (&$resource) { - $config = $this->app['config']; - $config->set( - 'translation-loader.model', - $config->get('nova-translations.model') - ); + $this->publishes([ + __DIR__.'/../config/nova-translations.php' => config_path('nova-translations.php'), + ], 'nova-translations-config'); - $resource::$model = $config->get('nova-translations.model'); + $this->publishes([ + __DIR__.'/../database/migrations' => database_path('migrations'), + ], 'nova-translations-migrations'); + } + } + /** + * Register console commands. + * + * @return void + */ + protected function registerCommands() + { + if ($this->app->runningInConsole()) { $this->commands([ ImportScanCommand::class, ]); + } + } - $this->routes(); - }); + /** + * Register Nova-specific resources. + * + * @return void + */ + protected function registerNovaResources() + { + $resource = $this->getConfiguredResource(); + Nova::resources([$resource]); + } - Nova::serving(function (ServingNova $event) use (&$resource) { - Nova::resources([ - $resource, - ]); + /** + * Load translations for Nova interface. + * + * @return void + */ + protected function loadNovaTranslations() + { + Nova::$translations = Translation::getTranslationsForGroup( + $this->app->getLocale(), + '*' + ); + } - Nova::$translations = Translation::getTranslationsForGroup( - $this->app->getLocale(), - '*' - ); - }); + /** + * Set the translation model for the resource. + * + * @param string $resource + * @return void + */ + protected function setTranslationModel($resource) + { + $config = $this->app['config']; + $config->set('translation-loader.model', $config->get('nova-translations.model')); + $resource::$model = $config->get('nova-translations.model'); + } + + /** + * Get the configured Nova resource class. + * + * @return string + */ + protected function getConfiguredResource() + { + return $this->app['config']->get('nova-translations.resource', TranslationResource::class); } /** @@ -81,11 +151,11 @@ protected function routes() } Nova::router(['nova', Authenticate::class, Authorize::class], 'nova-translations') - ->group(__DIR__ . '/../routes/inertia.php'); + ->group(__DIR__.'/../routes/inertia.php'); Route::middleware(['nova', Authorize::class]) ->prefix('nova-vendor/ferdiunal/nova-translations') - ->group(__DIR__ . '/../routes/api.php'); + ->group(__DIR__.'/../routes/api.php'); } /** @@ -95,5 +165,6 @@ protected function routes() */ public function register() { + // Register any application services or bindings here } } diff --git a/src/Services/SaveScan.php b/src/Services/SaveScan.php index e0cbac9..d2739ce 100644 --- a/src/Services/SaveScan.php +++ b/src/Services/SaveScan.php @@ -66,8 +66,8 @@ protected function save(string $namespace, string $group, string $key, ?string $ $model = app(config('nova-translations.model')); - if (!is_a($model, Translation::class, true)) { - throw new \Exception('Model must be an instance of ' . Translation::class); + if (! is_a($model, Translation::class, true)) { + throw new \Exception('Model must be an instance of '.Translation::class); } foreach ($locals as $locale) { @@ -102,12 +102,12 @@ protected function lang($key, $replace = null, $locale = null): string } $isTranslatable = (str($trans)->contains('::') || str($trans)->contains('_') || - (!str($trans)->contains('::') && - !str($trans)->endsWith('.') && + (! str($trans)->contains('::') && + ! str($trans)->endsWith('.') && str($trans)->contains('.'))); if ($this->defaultLocale !== $locale) { - if ($this->translater instanceof Translator && !$isTranslatable) { + if ($this->translater instanceof Translator && ! $isTranslatable) { $trans = $this->translater->run( source: $this->defaultLocale, target: $locale,