This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f2eabc
commit 305b552
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Listeners; | ||
|
||
use Illuminate\Translation\Translator; | ||
use Illuminate\Support\NamespacedItemResolver; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
|
||
/** | ||
* @link https://github.com/laravel/octane/pull/416/files | ||
*/ | ||
class FlushTranslatorCacheListener implements ListenerInterface | ||
{ | ||
use Traits\InvokerTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($event): void | ||
{ | ||
if ($event instanceof WithApplication) { | ||
$app = $event->application(); | ||
|
||
if (! $app->resolved($translator_abstract = 'translator')) { | ||
return; | ||
} | ||
|
||
/** @var Translator $translator */ | ||
$translator = $app->make($translator_abstract); | ||
|
||
if ($translator instanceof NamespacedItemResolver) { | ||
/** | ||
* Method `flushParsedKeys` for the Translator available since Laravel v8.70.0. | ||
* | ||
* @link https://git.io/JXd0v Source code (v8.70.0) | ||
* @see Translator::flushParsedKeys() | ||
*/ | ||
if (! $this->invokeMethod($translator, 'flushParsedKeys')) { | ||
$this->setProperty($translator, 'parsed', []); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners; | ||
|
||
use Mockery as m; | ||
use Illuminate\Translation\Translator; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
use Spiral\RoadRunnerLaravel\Listeners\FlushTranslatorCacheListener; | ||
|
||
/** | ||
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushTranslatorCacheListener | ||
*/ | ||
class FlushTranslatorCacheListenerTest extends AbstractListenerTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function testHandle(): void | ||
{ | ||
/** @var Translator $translator */ | ||
$translator = $this->app->make('translator'); | ||
|
||
$translator->setParsedKey('foo::bar.baz', ['foo', 'bar', 'baz']); | ||
|
||
/** @var m\MockInterface|WithApplication $event_mock */ | ||
$event_mock = m::mock(WithApplication::class) | ||
->makePartial() | ||
->expects('application') | ||
->andReturn($this->app) | ||
->getMock(); | ||
|
||
$this->assertNotEmpty($this->getProperty($translator, 'parsed')); | ||
|
||
$this->listenerFactory()->handle($event_mock); | ||
|
||
$this->assertEmpty($this->getProperty($translator, 'parsed')); | ||
} | ||
|
||
/** | ||
* @return FlushTranslatorCacheListener | ||
*/ | ||
protected function listenerFactory(): FlushTranslatorCacheListener | ||
{ | ||
return new FlushTranslatorCacheListener(); | ||
} | ||
} |