Skip to content

Commit a12c337

Browse files
authored
Integration with Livewire added (#71)
1 parent 35a73dc commit a12c337

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ The format is based on [Keep a Changelog][keepachangelog] and this project adher
88

99
### Added
1010

11-
- Listener `FlushTranslatorCacheListener` for memory leak fixing on `Translator` implementation [#69]
11+
- Listener `FlushTranslatorCacheListener` for memory leak fixing on `Translator` implementation [#70]
12+
- Integration with [Livewire](https://github.com/livewire/livewire) is supported now [#71]
1213

13-
[#69]:https://github.com/spiral/roadrunner-laravel/pull/69
14+
[#70]:https://github.com/spiral/roadrunner-laravel/pull/70
15+
[#71]:https://github.com/spiral/roadrunner-laravel/pull/71
1416

1517
## v5.4.0
1618

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"laravel/telescope": "^4.5",
5151
"mockery/mockery": "^1.3.2",
5252
"phpstan/phpstan": "~0.12.80",
53-
"phpunit/phpunit": "^8.0 || ^9.3"
53+
"phpunit/phpunit": "^8.0 || ^9.3",
54+
"livewire/livewire": "^2.7"
5455
},
5556
"autoload": {
5657
"psr-4": {

src/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static function beforeLoopIteration(): array
5555
Listeners\ResetLaravelSocialiteListener::class, // for <https://github.com/laravel/socialite>
5656
Listeners\ResetInertiaListener::class, // for <https://github.com/inertiajs/inertia-laravel>
5757
Listeners\ResetZiggyListener::class, // for <https://github.com/tighten/ziggy>
58+
Listeners\ResetLivewireListener::class, // for <https://github.com/livewire/livewire>
5859
];
5960
}
6061

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
use Livewire\LivewireManager;
8+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
9+
10+
/**
11+
* Target package: <https://github.com/livewire/livewire>.
12+
*/
13+
class ResetLivewireListener implements ListenerInterface
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function handle($event): void
19+
{
20+
if (!\class_exists(LivewireManager::class)) {
21+
return;
22+
}
23+
24+
if ($event instanceof WithApplication) {
25+
$app = $event->application();
26+
27+
if (!$app->resolved($manager_abstract = LivewireManager::class)) {
28+
return;
29+
}
30+
31+
/** @var LivewireManager $manager */
32+
$manager = $app->make($manager_abstract);
33+
34+
if (\method_exists($manager, 'flushState')) {
35+
$manager->flushState();
36+
}
37+
}
38+
}
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Mockery as m;
8+
use Livewire\LivewireManager;
9+
use Spiral\RoadRunnerLaravel\Listeners\ResetLivewireListener;
10+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
11+
12+
/**
13+
* @covers \Spiral\RoadRunnerLaravel\Listeners\ResetLivewireListener
14+
*/
15+
class ResetLivewireListenerTest extends AbstractListenerTestCase
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function testHandle(): void
21+
{
22+
/** @var LivewireManager $manager */
23+
$manager = $this->app->make($manager_abstract = LivewireManager::class);
24+
25+
$manager_mock = m::mock($manager)
26+
->makePartial()
27+
->expects('flushState')
28+
->withNoArgs()
29+
->getMock();
30+
31+
$this->app->instance($manager_abstract, $manager_mock);
32+
33+
/** @var m\MockInterface|WithApplication $event_mock */
34+
$event_mock = m::mock(WithApplication::class)
35+
->makePartial()
36+
->expects('application')
37+
->andReturn($this->app)
38+
->getMock();
39+
40+
$this->listenerFactory()->handle($event_mock);
41+
}
42+
43+
/**
44+
* @return ResetLivewireListener
45+
*/
46+
protected function listenerFactory(): ResetLivewireListener
47+
{
48+
return new ResetLivewireListener();
49+
}
50+
}

0 commit comments

Comments
 (0)