From c317e2e19ff49929b9272e8dc5e6bf5d1ec0eef7 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Thu, 5 Mar 2020 09:57:17 +0100 Subject: [PATCH] Updating the last changes --- _docs/2-Configuration.md | 10 ++++-- composer.json | 1 - config/localization.php | 6 +--- src/Middleware/Middleware.php | 31 ++++++++++--------- ...06_01_000000_create_translatable_table.php | 2 +- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/_docs/2-Configuration.md b/_docs/2-Configuration.md index 4b7bf57..5ad145f 100644 --- a/_docs/2-Configuration.md +++ b/_docs/2-Configuration.md @@ -6,7 +6,7 @@ 2. [Configuration](2-Configuration.md) 3. [Usage](3-Usage.md) 4. [FAQ](4-FAQ.md) - + ## Settings ```php @@ -66,15 +66,19 @@ return [ | localized-routes | Allows to register all translatable routes. | | translation-redirect | Allows to translate the route attributes by using the translation event. | -## Ignored URI +## Ignored URI / Route ```php 'ignored-uri' => [ // ], + + 'ignored-routes' => [ + // + ], ``` -You can set a list of uris to ignore from localization checks. +You can set a list of uris or route names to ignore from localization checks / redirection. ## Locales diff --git a/composer.json b/composer.json index 0f77ba0..3a1f69d 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,6 @@ "ext-intl": "*", "orchestra/testbench": "^4.0", "mockery/mockery": "^1.3.1", - "phpunit/phpcov": "^6.0", "phpunit/phpunit": "^8.0" }, "autoload": { diff --git a/config/localization.php b/config/localization.php index 9bc0845..f68daa0 100644 --- a/config/localization.php +++ b/config/localization.php @@ -33,7 +33,7 @@ ], /* ----------------------------------------------------------------- - | Ignored URI from localization + | Ignored URI/Routes from localization | ----------------------------------------------------------------- */ @@ -41,10 +41,6 @@ // ], - /* ----------------------------------------------------------------- - | Ignored routes from localization - | ----------------------------------------------------------------- - */ 'ignored-routes' => [ // ], diff --git a/src/Middleware/Middleware.php b/src/Middleware/Middleware.php index 30b2894..9bc3a7a 100644 --- a/src/Middleware/Middleware.php +++ b/src/Middleware/Middleware.php @@ -29,19 +29,12 @@ abstract class Middleware protected $localization; /** - * The URIs that should not be localized. + * The URIs or route names that should not be localized. * * @var array */ protected $except = []; - /** - * The routes that should not be localized. - * - * @var array - */ - protected $except_routes = []; - /* ----------------------------------------------------------------- | Constructor | ----------------------------------------------------------------- @@ -54,9 +47,8 @@ abstract class Middleware */ public function __construct(Localization $localization) { - $this->localization = $localization; - $this->except = config('localization.ignored-uri', []); - $this->except_routes = config('localization.ignored-routes', []); + $this->localization = $localization; + $this->except = $this->getIgnoredRedirection(); } /* ----------------------------------------------------------------- @@ -127,7 +119,7 @@ protected function isDefaultLocaleHidden($locale) * * @return bool */ - protected function shouldIgnore(Request $request) + protected function shouldIgnore(Request $request): bool { foreach ($this->except as $except) { if ($except !== '/') @@ -135,9 +127,7 @@ protected function shouldIgnore(Request $request) if ($request->is($except)) return true; - } - foreach ($this->except_routes as $except) { if ($request->routeIs($except)) return true; } @@ -176,4 +166,17 @@ protected function makeRedirectResponse($url, $code = null) { return new RedirectResponse($url, $code ?? config('localization.redirection-code', 302), ['Vary' => 'Accept-Language']); } + + /** + * The URIs or route names that should not be redirected. + * + * @return array + */ + protected function getIgnoredRedirection(): array + { + return array_merge( + config('localization.ignored-uri', []), + config('localization.ignored-routes', []) + ); + } } diff --git a/tests/fixtures/migrations/2017_06_01_000000_create_translatable_table.php b/tests/fixtures/migrations/2017_06_01_000000_create_translatable_table.php index 46398c7..6f4ddea 100644 --- a/tests/fixtures/migrations/2017_06_01_000000_create_translatable_table.php +++ b/tests/fixtures/migrations/2017_06_01_000000_create_translatable_table.php @@ -29,7 +29,7 @@ class CreateTranslatableTable extends Migration /** * Migrate to database. */ - public function up() + public function up(): void { $this->createSchema(function (Blueprint $table) { $table->increments('id');