Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Added listeners for FilesystemManager and LogManager (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Jan 13, 2022
1 parent d8643d6 commit 58d5c0c
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.

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

## UNRELEASED

### Added

- Give the current App instance to `FilesystemManager` (listener `RebindFilesystemManagerListener`) [#77]
- Monolog state resetting between requests (listener `FlushMonologStateListener`) [#77]

[#77]:https://github.com/spiral/roadrunner-laravel/pull/77

## v5.5.0

### Added
Expand Down
2 changes: 2 additions & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function beforeLoopIteration(): array
Listeners\RebindBroadcastManagerListener::class,
Listeners\RebindDatabaseManagerListener::class,
Listeners\RebindDatabaseSessionHandlerListener::class,
Listeners\RebindFilesystemManagerListener::class,
Listeners\RebindMailManagerListener::class,
Listeners\RebindNotificationChannelManagerListener::class,
Listeners\RebindPipelineHubListener::class,
Expand Down Expand Up @@ -95,6 +96,7 @@ public static function afterLoopIteration(): array
Listeners\FlushDumperStackListener::class,
Listeners\FlushLogContextListener::class,
Listeners\FlushArrayCacheListener::class,
Listeners\FlushMonologStateListener::class,
Listeners\FlushTranslatorCacheListener::class,
Listeners\ResetDatabaseRecordModificationStateListener::class,
Listeners\FlushDatabaseQueryLogListener::class,
Expand Down
38 changes: 38 additions & 0 deletions src/Listeners/FlushMonologStateListener.php
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();
}
}
}
}
}
}
42 changes: 42 additions & 0 deletions src/Listeners/RebindFilesystemManagerListener.php
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);
}
}
}
}
68 changes: 68 additions & 0 deletions tests/Unit/Listeners/FlushMonologStateListenerTest.php
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 tests/Unit/Listeners/RebindFilesystemManagerListenerTest.php
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();
}
}

0 comments on commit 58d5c0c

Please sign in to comment.