From 293b55a437c34b4494d9419250b17e254aeb286b Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Tue, 23 Apr 2024 14:58:54 -0400 Subject: [PATCH] feat: migrate Translation initialization process to support Laravel 11 new application structure --- README.md | 17 +++--------- src/Application.php | 18 +++++++++++++ src/Traits/CustomRouterDispatcher.php | 37 --------------------------- src/TranslationServiceProvider.php | 7 +++++ tests/Integration/TestCase.php | 5 ++++ 5 files changed, 33 insertions(+), 51 deletions(-) create mode 100644 src/Application.php delete mode 100644 src/Traits/CustomRouterDispatcher.php diff --git a/README.md b/README.md index f023372..5294318 100644 --- a/README.md +++ b/README.md @@ -15,21 +15,10 @@ Require this package with composer: composer require exolnet/laravel-translation ``` -In Laravel 5.5, the service provider and facade will automatically get registered. For older versions of the framework, follow the steps below: +To make sure the routing system is using the one supporting the translation you must edit your `bootstrap/app.php` to change the Application class import -Register the service provider in `config/app.php` - -```php -'providers' => [ - // [...] - Exolnet\Translation\TranslationServiceProvider::class, -], -``` - -To make sure the routing system is using the one supporting the translation you must edit your `app/Http/Kernel.php` to add the usage of the custom user dispatcher - -```php -use \Exolnet\Translation\Traits\CustomRouterDispatcher; +```bash +sed -i '' 's/Illuminate\\Foundation\\Application/Exolnet\\Translation\\Application/g' bootstrap/app.php ``` Now you're ready to start using the translation in your application. diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 0000000..b2c7b36 --- /dev/null +++ b/src/Application.php @@ -0,0 +1,18 @@ +register(new TranslationServiceProvider($this)); + } +} diff --git a/src/Traits/CustomRouterDispatcher.php b/src/Traits/CustomRouterDispatcher.php deleted file mode 100644 index 7a2fbc1..0000000 --- a/src/Traits/CustomRouterDispatcher.php +++ /dev/null @@ -1,37 +0,0 @@ -router; - /** @var \Exolnet\Translation\Routing\Router $newRouter */ - $newRouter = $this->app['router']; - - foreach ($this->middlewareGroups as $key => $middlewares) { - $newRouter->middlewareGroup($key, $middlewares); - } - - foreach ($oldRouter->getMiddleware() as $key => $middleware) { - $newRouter->aliasMiddleware($key, $middleware); - } - - $newRouter->middlewarePriority = $oldRouter->middlewarePriority; - - array_unshift($newRouter->middlewarePriority, SetLocaleFromRouteLocalized::class); - - $this->router = $newRouter; - - return parent::dispatchToRouter(); - } -} diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php index 4f9ca40..580258f 100644 --- a/src/TranslationServiceProvider.php +++ b/src/TranslationServiceProvider.php @@ -12,6 +12,7 @@ use Illuminate\Foundation\Events\LocaleUpdated; use Illuminate\Routing\Route; use Illuminate\Support\ServiceProvider; +use LogicException; class TranslationServiceProvider extends ServiceProvider { @@ -57,6 +58,12 @@ protected function offerPublishing(): void */ public function register() { + if (! $this->app instanceof Application) { + throw new LogicException( + 'The application must be an instance of ' . Application::class . ' to use the Translation package.' + ); + } + $this->registerRouter(); $this->registerUrlGenerator(); $this->registerLocaleService(); diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index c223358..7acec1c 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -2,11 +2,16 @@ namespace Exolnet\Translation\Tests\Integration; +use Exolnet\Translation\Application; use Illuminate\Routing\Route; use Orchestra\Testbench\TestCase as Orchestra; abstract class TestCase extends Orchestra { + protected function resolveApplication() + { + return new Application($this->getBasePath()); + } /** * @return void */