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
d8643d6
commit 58d5c0c
Showing
6 changed files
with
207 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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Listeners; | ||
|
||
/** | ||
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/FlushMonologState.php | ||
*/ | ||
class FlushMonologStateListener implements ListenerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($event): void | ||
{ | ||
if ($event instanceof \Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication) { | ||
$app = $event->application(); | ||
|
||
if (!$app->resolved($log_abstract = 'log')) { | ||
return; | ||
} | ||
|
||
/** @var \Illuminate\Log\LogManager $log_manager */ | ||
$log_manager = $app->make($log_abstract); | ||
|
||
foreach ($log_manager->getChannels() as $channel) { | ||
if ($channel instanceof \Illuminate\Log\Logger) { | ||
$logger = $channel->getLogger(); | ||
|
||
if ($logger instanceof \Monolog\ResettableInterface) { | ||
$logger->reset(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Listeners; | ||
|
||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
|
||
/** | ||
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/GiveNewApplicationInstanceToFilesystemManager.php | ||
*/ | ||
class RebindFilesystemManagerListener implements ListenerInterface | ||
{ | ||
use Traits\InvokerTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($event): void | ||
{ | ||
if ($event instanceof WithApplication) { | ||
$app = $event->application(); | ||
|
||
if (!$app->resolved($filesystem_abstract = 'filesystem')) { | ||
return; | ||
} | ||
|
||
/** @var \Illuminate\Filesystem\FilesystemManager $filesystem_manager */ | ||
$filesystem_manager = $app->make($filesystem_abstract); | ||
|
||
/** | ||
* Method `setApplication` for the FilesystemManager available since Laravel v8.77.0. | ||
* | ||
* @link https://github.com/laravel/framework/blob/v8.77.0/src/Illuminate/Filesystem/FilesystemManager.php#L395-L400 | ||
* @see \Illuminate\Filesystem\FilesystemManager::setApplication | ||
*/ | ||
if (! $this->invokeMethod($filesystem_manager, 'setApplication', $app)) { | ||
$this->setProperty($filesystem_manager, 'app', $app); | ||
} | ||
} | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners; | ||
|
||
use Mockery as m; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
use Spiral\RoadRunnerLaravel\Listeners\FlushMonologStateListener; | ||
|
||
/** | ||
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushMonologStateListener | ||
*/ | ||
class FlushMonologStateListenerTest extends AbstractListenerTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function testHandle(): void | ||
{ | ||
/** @var \Illuminate\Log\LogManager $log_manager */ | ||
$log_manager = $this->app->make('log'); | ||
|
||
/** @var \Illuminate\Log\Logger $driver */ | ||
$driver = $log_manager->driver(); | ||
|
||
/** @var \Monolog\Logger $logger */ | ||
$logger = $driver->getLogger(); | ||
|
||
$this->app->instance('log', m::mock($log_manager) | ||
->makePartial() | ||
->expects('getChannels') | ||
->withNoArgs() | ||
->andReturns([ | ||
m::mock($driver) | ||
->makePartial() | ||
->expects('getLogger') | ||
->withNoArgs() | ||
->andReturn( | ||
m::mock($logger) | ||
->makePartial() | ||
->expects('reset') | ||
->once() | ||
->withNoArgs() | ||
->getMock(), | ||
) | ||
->getMock(), | ||
]) | ||
->getMock()); | ||
|
||
/** @var m\MockInterface|WithApplication $event_mock */ | ||
$event_mock = m::mock(WithApplication::class) | ||
->makePartial() | ||
->expects('application') | ||
->andReturn($this->app) | ||
->getMock(); | ||
|
||
$this->listenerFactory()->handle($event_mock); | ||
} | ||
|
||
/** | ||
* @return FlushMonologStateListener | ||
*/ | ||
protected function listenerFactory(): FlushMonologStateListener | ||
{ | ||
return new FlushMonologStateListener(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Unit/Listeners/RebindFilesystemManagerListenerTest.php
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\Filesystem\FilesystemManager; | ||
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication; | ||
use Spiral\RoadRunnerLaravel\Listeners\RebindFilesystemManagerListener; | ||
|
||
/** | ||
* @covers \Spiral\RoadRunnerLaravel\Listeners\RebindFilesystemManagerListener | ||
*/ | ||
class RebindFilesystemManagerListenerTest extends AbstractListenerTestCase | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function testHandle(): void | ||
{ | ||
$app_clone = clone $this->app; | ||
|
||
/** @var FilesystemManager $filesystem_manager */ | ||
$filesystem_manager = $this->app->make('filesystem'); | ||
|
||
$this->setProperty($filesystem_manager, $app_prop = 'app', $app_clone); | ||
|
||
/** @var m\MockInterface|WithApplication $event_mock */ | ||
$event_mock = m::mock(WithApplication::class) | ||
->makePartial() | ||
->expects('application') | ||
->andReturn($this->app) | ||
->getMock(); | ||
|
||
$this->listenerFactory()->handle($event_mock); | ||
|
||
$this->assertSame($this->app, $this->getProperty($filesystem_manager, $app_prop)); | ||
} | ||
|
||
/** | ||
* @return RebindFilesystemManagerListener | ||
*/ | ||
protected function listenerFactory(): RebindFilesystemManagerListener | ||
{ | ||
return new RebindFilesystemManagerListener(); | ||
} | ||
} |