Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Fixes memory leak on Translator implementation #416

Merged
merged 3 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"require": {
"php": "^8.0",
"laravel/framework": "^8.62",
"laravel/framework": "^8.70",
"laminas/laminas-diactoros": "^2.5",
"laravel/serializable-closure": "^1.0",
"symfony/psr-http-message-bridge": "^2.0"
Expand Down
1 change: 1 addition & 0 deletions src/Concerns/ProvidesDefaultConfigurationOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static function prepareApplicationForNextOperation(): array
\Laravel\Octane\Listeners\FlushDatabaseQueryLog::class,
\Laravel\Octane\Listeners\FlushLogContext::class,
\Laravel\Octane\Listeners\FlushArrayCache::class,
\Laravel\Octane\Listeners\FlushTranslatorCache::class,

// First-Party Packages...
\Laravel\Octane\Listeners\PrepareInertiaForNextOperation::class,
Expand Down
27 changes: 27 additions & 0 deletions src/Listeners/FlushTranslatorCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Laravel\Octane\Listeners;

use Illuminate\Support\NamespacedItemResolver;

class FlushTranslatorCache
{
/**
* Handle the event.
*
* @param mixed $event
* @return void
*/
public function handle($event)
{
if (! $event->sandbox->resolved('translator')) {
return;
}

$translator = $event->sandbox->make('translator');

if ($translator instanceof NamespacedItemResolver) {
$translator->flushParsedKeys();
}
}
}
33 changes: 33 additions & 0 deletions tests/Listeners/FlushTranslatorCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Laravel\Octane\Listeners;

use Illuminate\Http\Request;
use Laravel\Octane\Tests\TestCase;
use Mockery;

class FlushTranslatorCacheTest extends TestCase
{
/** @doesNotPerformAssertions */
public function test_parsed_keys_cache_is_flushed()
{
[$app, $worker, $client] = $this->createOctaneContext([
Request::create('/test-translator', 'GET'),
Request::create('/test-translator', 'GET'),
]);

$app['router']->middleware('web')->get('/test-cache', function () {
Validator::make($data, [
'name' => 'string|max:50',
])->validate();
});

$translator = $app['translator'];

$app['translator'] = tap(Mockery::mock($translator), function ($translator) {
$translator->shouldReceive('flushParsedKeys')->twice();
});

$worker->run();
}
}