Skip to content

Commit

Permalink
Fix PsrLogErrorHandler automatic registration
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Aug 16, 2019
1 parent 5a500e2 commit a1332ce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/DI/Plugin/CoreServicesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Apitte\Core\Router\IRouter;
use Apitte\Core\Router\SimpleRouter;
use Apitte\Core\Schema\Schema;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\MissingServiceException;
use Psr\Log\LoggerInterface;

class CoreServicesPlugin extends Plugin
Expand Down Expand Up @@ -42,16 +44,11 @@ public function loadPluginConfiguration(): void
// Catch exception only in debug mode if explicitly enabled
$catchException = !$globalConfig->debug || $globalConfig->catchException;

$errorHandler = $builder->addDefinition($this->prefix('errorHandler'))
$builder->addDefinition($this->prefix('errorHandler'))
->setFactory(SimpleErrorHandler::class)
->setType(IErrorHandler::class)
->addSetup('setCatchException', [$catchException]);

// Set handler with logging, if logger available
if ($builder->findByType(LoggerInterface::class) !== []) {
$errorHandler->setFactory(PsrLogErrorHandler::class);
}

$builder->addDefinition($this->prefix('dispatcher.wrapper'))
->setFactory(WrappedDispatcher::class, ['@' . $this->prefix('dispatcher')]);

Expand All @@ -71,4 +68,22 @@ public function loadPluginConfiguration(): void
->setFactory(Schema::class);
}

public function beforePluginCompile(): void
{
$builder = $this->getContainerBuilder();

$errorHandlerDefinition = $builder->getDefinition($this->prefix('errorHandler'));
assert($errorHandlerDefinition instanceof ServiceDefinition);

// Set error handler to PsrErrorHandler if logger is available and user didn't change logger himself
if ($errorHandlerDefinition->getFactory()->getEntity() === SimpleErrorHandler::class) {
try {
$loggerDefinition = $builder->getDefinitionByType(LoggerInterface::class);
$errorHandlerDefinition->setFactory(PsrLogErrorHandler::class, [$loggerDefinition]);
} catch (MissingServiceException $exception) {
// No need to handle
}
}
}

}
26 changes: 26 additions & 0 deletions tests/cases/DI/ApiExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Apitte\Core\Application\Application;
use Apitte\Core\DI\ApiExtension;
use Apitte\Core\Dispatcher\JsonDispatcher;
use Apitte\Core\ErrorHandler\IErrorHandler;
use Apitte\Core\ErrorHandler\PsrLogErrorHandler;
use Apitte\Core\Schema\Schema;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tests\Fixtures\Controllers\FoobarController;
use Tests\Fixtures\Utils\FakeLogger;

require_once __DIR__ . '/../../bootstrap.php';

Expand Down Expand Up @@ -64,3 +67,26 @@ test(function (): void {
Assert::equal(FoobarController::class, $schema->getEndpoints()[0]->getHandler()->getClass());
Assert::equal('baz1', $schema->getEndpoints()[0]->getHandler()->getMethod());
});

// PsrErrorHandler
test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('api', new ApiExtension());
$compiler->addConfig([
'parameters' => [
'debugMode' => true,
],
'services' => [
FakeLogger::class,
],
]);
}, 3);

/** @var Container $container */
$container = new $class();

/** @var IErrorHandler $errorHandler */
$errorHandler = $container->getService('api.core.errorHandler');
Assert::true($errorHandler instanceof PsrLogErrorHandler);
});

0 comments on commit a1332ce

Please sign in to comment.