Skip to content

Commit 6f482eb

Browse files
[1.x] Fixes sharing "Carbon" state shared between requests (#552)
* Fixes shared Carbon Locale between requests * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]>
1 parent 950afc8 commit 6f482eb

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Listeners/FlushLocaleState.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Laravel\Octane\Listeners;
44

5+
use Carbon\Laravel\ServiceProvider as CarbonServiceProvider;
6+
57
class FlushLocaleState
68
{
79
/**
@@ -18,5 +20,7 @@ public function handle($event): void
1820
$translator->setLocale($config->get('app.locale'));
1921
$translator->setFallback($config->get('app.fallback_locale'));
2022
});
23+
24+
(new CarbonServiceProvider($event->app))->updateLocale();
2125
}
2226
}

tests/LocaleStateTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Octane\Tests;
44

5+
use Carbon\Carbon;
56
use Illuminate\Foundation\Application;
67
use Illuminate\Http\Request;
78

@@ -29,4 +30,27 @@ public function test_translator_state_is_reset_across_subsequent_requests()
2930
$this->assertEquals('en', $client->responses[1]->getContent());
3031
$this->assertEquals('ms', $client->responses[2]->getContent());
3132
}
33+
34+
public function test_carbon_state_is_reset_across_subsequent_requests()
35+
{
36+
[$app, $worker, $client] = $this->createOctaneContext([
37+
Request::create('/test-locale', 'GET'), // should be "en"
38+
Request::create('/test-locale?locale=nl', 'GET'),
39+
Request::create('/test-locale', 'GET'), // should be "en", and not "nl"...
40+
]);
41+
42+
$app['router']->get('/test-locale', function (Application $app, Request $request) {
43+
if ($request->has('locale')) {
44+
Carbon::setLocale($request->query('locale'));
45+
}
46+
47+
return now()->getLocale();
48+
});
49+
50+
$worker->run();
51+
52+
$this->assertEquals('en', $client->responses[0]->getContent());
53+
$this->assertEquals('nl', $client->responses[1]->getContent());
54+
$this->assertEquals('en', $client->responses[2]->getContent());
55+
}
3256
}

0 commit comments

Comments
 (0)