add addLoggerSubscription method
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Potter\Logger;
|
||||
|
||||
use \Potter\Container;
|
||||
use \Potter\Event\Event;
|
||||
|
||||
trait Behaviour
|
||||
{
|
||||
@@ -23,6 +24,7 @@ trait Behaviour
|
||||
private static function addLoggerBehaviour(Container $container): void
|
||||
{
|
||||
self::addLogger($container);
|
||||
self::addLoggerSubscription($container);
|
||||
}
|
||||
|
||||
private static function addLoggerHandler(Container $container): void
|
||||
@@ -34,6 +36,30 @@ trait Behaviour
|
||||
->addArgument(\Potter\Console\Logger::class);
|
||||
}
|
||||
|
||||
private static function addLoggerSubscription(Container $container): void
|
||||
{
|
||||
if (!static::hasDispatcher($container)) {
|
||||
return;
|
||||
}
|
||||
$dispatcher = static::getDispatcher($container);
|
||||
|
||||
$dispatcher->subscribeTo('applicationStart', function (Event $event) {
|
||||
$container = $event->getArgument('container');
|
||||
if (!self::hasLogger($container)) {
|
||||
return; }
|
||||
self::getLogger($container)->notice('Application start.');
|
||||
});
|
||||
|
||||
$dispatcher->subscribeTo('applicationStop', function (Event $event) {
|
||||
$container = $event->getArgument('container');
|
||||
if (!self::hasLogger($container)) {
|
||||
return; }
|
||||
self::getLogger($container)->notice('Application stop.');
|
||||
});
|
||||
}
|
||||
|
||||
abstract private static function getDispatcher(Container $container): \Potter\Event\Dispatcher;
|
||||
|
||||
private static function getLogger(Container $container): Logger
|
||||
{
|
||||
return $container->get(Logger::class);
|
||||
@@ -44,4 +70,10 @@ trait Behaviour
|
||||
return [$container->get(Handler::class)];
|
||||
}
|
||||
|
||||
abstract private static function hasDispatcher(Container $container): bool;
|
||||
|
||||
private static function hasLogger(Container $container): bool
|
||||
{
|
||||
return $container->has(Logger::class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user