Skip to content

Commit

Permalink
[1.x] Fixes memory leak on Translator implementation (#416)
Browse files Browse the repository at this point in the history
* Fixes memory leak on translator keys

* Apply fixes from StyleCI

* Bumps framework dependency

Co-authored-by: Taylor Otwell <taylorotwell@users.noreply.github.com>
  • Loading branch information
nunomaduro and taylorotwell authored Nov 9, 2021
1 parent 750a072 commit fbde6dd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
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();
}
}

0 comments on commit fbde6dd

Please sign in to comment.