Skip to content

Commit 35a73dc

Browse files
authored
Translator memory leak fixed (#70)
1 parent 4f2eabc commit 35a73dc

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## UNRELEASED
8+
9+
### Added
10+
11+
- Listener `FlushTranslatorCacheListener` for memory leak fixing on `Translator` implementation [#69]
12+
13+
[#69]:https://github.com/spiral/roadrunner-laravel/pull/69
14+
715
## v5.4.0
816

917
### Added

src/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static function afterLoopIteration(): array
9494
Listeners\FlushDumperStackListener::class,
9595
Listeners\FlushLogContextListener::class,
9696
Listeners\FlushArrayCacheListener::class,
97+
Listeners\FlushTranslatorCacheListener::class,
9798
Listeners\ResetDatabaseRecordModificationStateListener::class,
9899
Listeners\FlushDatabaseQueryLogListener::class,
99100
Listeners\ClearInstancesListener::class,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
use Illuminate\Translation\Translator;
8+
use Illuminate\Support\NamespacedItemResolver;
9+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
10+
11+
/**
12+
* @link https://github.com/laravel/octane/pull/416/files
13+
*/
14+
class FlushTranslatorCacheListener implements ListenerInterface
15+
{
16+
use Traits\InvokerTrait;
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function handle($event): void
22+
{
23+
if ($event instanceof WithApplication) {
24+
$app = $event->application();
25+
26+
if (! $app->resolved($translator_abstract = 'translator')) {
27+
return;
28+
}
29+
30+
/** @var Translator $translator */
31+
$translator = $app->make($translator_abstract);
32+
33+
if ($translator instanceof NamespacedItemResolver) {
34+
/**
35+
* Method `flushParsedKeys` for the Translator available since Laravel v8.70.0.
36+
*
37+
* @link https://git.io/JXd0v Source code (v8.70.0)
38+
* @see Translator::flushParsedKeys()
39+
*/
40+
if (! $this->invokeMethod($translator, 'flushParsedKeys')) {
41+
$this->setProperty($translator, 'parsed', []);
42+
}
43+
}
44+
}
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Mockery as m;
8+
use Illuminate\Translation\Translator;
9+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
10+
use Spiral\RoadRunnerLaravel\Listeners\FlushTranslatorCacheListener;
11+
12+
/**
13+
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushTranslatorCacheListener
14+
*/
15+
class FlushTranslatorCacheListenerTest extends AbstractListenerTestCase
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function testHandle(): void
21+
{
22+
/** @var Translator $translator */
23+
$translator = $this->app->make('translator');
24+
25+
$translator->setParsedKey('foo::bar.baz', ['foo', 'bar', 'baz']);
26+
27+
/** @var m\MockInterface|WithApplication $event_mock */
28+
$event_mock = m::mock(WithApplication::class)
29+
->makePartial()
30+
->expects('application')
31+
->andReturn($this->app)
32+
->getMock();
33+
34+
$this->assertNotEmpty($this->getProperty($translator, 'parsed'));
35+
36+
$this->listenerFactory()->handle($event_mock);
37+
38+
$this->assertEmpty($this->getProperty($translator, 'parsed'));
39+
}
40+
41+
/**
42+
* @return FlushTranslatorCacheListener
43+
*/
44+
protected function listenerFactory(): FlushTranslatorCacheListener
45+
{
46+
return new FlushTranslatorCacheListener();
47+
}
48+
}

0 commit comments

Comments
 (0)