From d95f37fc272d26bbbd2a432898c4c3c5974645e2 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 25 Jul 2022 15:11:08 +0100 Subject: [PATCH 1/3] Fix carbon locale when setting it via app locale setter --- composer.json | 7 +++++++ src/Listeners/FlushLocaleState.php | 6 +++++- tests/LocaleStateTest.php | 25 +++++++++++++++++++++++++ tests/TestCase.php | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6913acbad..1882acf44 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,15 @@ "laravel/framework": "^8.81|^9.0", "laminas/laminas-diactoros": "^2.5", "laravel/serializable-closure": "^1.0", + "nesbot/carbon": "dev-master as 2.53.1", "symfony/psr-http-message-bridge": "^2.0" }, + "repositories": [ + { + "type": "github", + "url": "https://github.com/briannesbitt/Carbon" + } + ], "require-dev": { "guzzlehttp/guzzle": "^7.2", "mockery/mockery": "^1.4", diff --git a/src/Listeners/FlushLocaleState.php b/src/Listeners/FlushLocaleState.php index e9e20bfcc..c197cc7d8 100644 --- a/src/Listeners/FlushLocaleState.php +++ b/src/Listeners/FlushLocaleState.php @@ -21,6 +21,10 @@ public function handle($event): void $translator->setFallback($config->get('app.fallback_locale')); }); - (new CarbonServiceProvider($event->app))->updateLocale(); + $provider = tap(new CarbonServiceProvider($event->app))->updateLocale(); + + collect($event->sandbox->getProviders($provider)) + ->values() + ->whenNotEmpty(fn ($providers) => $providers->first()->setAppGetter(fn () => $event->sandbox)); } } diff --git a/tests/LocaleStateTest.php b/tests/LocaleStateTest.php index c2971f2ad..59e555739 100644 --- a/tests/LocaleStateTest.php +++ b/tests/LocaleStateTest.php @@ -53,4 +53,29 @@ public function test_carbon_state_is_reset_across_subsequent_requests() $this->assertEquals('nl', $client->responses[1]->getContent()); $this->assertEquals('en', $client->responses[2]->getContent()); } + + public function test_carbon_and_app_locale_gets_updated_when_changing_app_locale() + { + [$app, $worker, $client] = $this->createOctaneContext([ + Request::create('/test-locale', 'GET'), // should be "en" + Request::create('/test-locale?locale=nl', 'GET'), // should be "nl" + Request::create('/test-locale', 'GET'), // should be "en", and not "nl" + Request::create('/test-locale?locale=pt', 'GET'), // should be "pt" + ]); + + $app['router']->get('/test-locale', function (Application $app, Request $request) { + if ($request->has('locale')) { + app()->setLocale($request->query('locale')); + } + + return [app()->getLocale(), now()->getLocale()]; + }); + + $worker->run(); + + $this->assertEquals(['en', 'en'], json_decode($client->responses[0]->getContent(), true)); + $this->assertEquals(['nl', 'nl'], json_decode($client->responses[1]->getContent(), true)); + $this->assertEquals(['en', 'en'], json_decode($client->responses[2]->getContent(), true)); + $this->assertEquals(['pt', 'pt'], json_decode($client->responses[3]->getContent(), true)); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8745b5968..6300e923f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Laravel\Octane\Tests; +use Carbon\Laravel\ServiceProvider as CarbonServiceProvider; use Laravel\Octane\ApplicationFactory; use Laravel\Octane\Contracts\Client; use Laravel\Octane\Octane; @@ -21,6 +22,7 @@ protected function createOctaneContext(array $requests) $appFactory->shouldReceive('createApplication')->andReturn($app = $this->createApplication()); + $app->register(new CarbonServiceProvider($app)); $app->register(new OctaneServiceProvider($app)); $worker = new FakeWorker($appFactory, $roadRunnerClient = new FakeClient($requests)); From d94013a028a3b6122a28c4b08e8877d803d7d312 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 28 Jul 2022 07:59:05 +0100 Subject: [PATCH 2/3] Uses latest version of Carbon --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1882acf44..e10f6daf9 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "laravel/framework": "^8.81|^9.0", "laminas/laminas-diactoros": "^2.5", "laravel/serializable-closure": "^1.0", - "nesbot/carbon": "dev-master as 2.53.1", + "nesbot/carbon": "^2.60", "symfony/psr-http-message-bridge": "^2.0" }, "repositories": [ From 76877ff3d29fa1990e0db37eef835c9286dc9f56 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 28 Jul 2022 08:04:25 +0100 Subject: [PATCH 3/3] Removes repositories on composer --- composer.json | 6 ------ 1 file changed, 6 deletions(-) diff --git a/composer.json b/composer.json index e10f6daf9..6b81a2834 100644 --- a/composer.json +++ b/composer.json @@ -21,12 +21,6 @@ "nesbot/carbon": "^2.60", "symfony/psr-http-message-bridge": "^2.0" }, - "repositories": [ - { - "type": "github", - "url": "https://github.com/briannesbitt/Carbon" - } - ], "require-dev": { "guzzlehttp/guzzle": "^7.2", "mockery/mockery": "^1.4",