src/MonologHandlerPluginManagerFactory.php
<?php
/**
* This file is part of the mimmi20/monolog-factory package.
*
* Copyright (c) 2022-2024, Thomas Mueller <mimmi20@live.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace Mimmi20\MonologFactory;
use Laminas\ServiceManager\Config;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Override;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use function assert;
use function is_array;
use function sprintf;
final class MonologHandlerPluginManagerFactory implements FactoryInterface
{
/**
* @param string $requestedName
* @param array<mixed>|null $options
*
* @throws ServiceNotFoundException if unable to resolve the service
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
*/
#[Override]
public function __invoke(
ContainerInterface $container,
$requestedName,
array | null $options = null,
): MonologHandlerPluginManager {
$pluginManager = new MonologHandlerPluginManager($container, $options ?: []);
// If this is in a laminas-mvc application, the ServiceListener will inject
// merged configuration during bootstrap.
if ($container->has('ServiceListener')) {
return $pluginManager;
}
// If we do not have a config service, nothing more to do
if (!$container->has('config')) {
return $pluginManager;
}
try {
$config = $container->get('config');
} catch (ContainerExceptionInterface $e) {
throw new ServiceNotFoundException(sprintf('Could not find service %s', 'config'), 0, $e);
}
assert(is_array($config));
// If we do not have handler configuration, nothing more to do
if (!isset($config['monolog_handlers']) || !is_array($config['monolog_handlers'])) {
return $pluginManager;
}
// Wire service configuration for handler
(new Config($config['monolog_handlers']))->configureServiceManager($pluginManager);
return $pluginManager;
}
}