src/ClientPluginManagerFactory.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 ClientPluginManagerFactory 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,
): ClientPluginManager {
$pluginManager = new ClientPluginManager($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 client configuration, nothing more to do
if (
!isset($config['monolog_service_clients'])
|| !is_array($config['monolog_service_clients'])
) {
return $pluginManager;
}
// Wire service configuration for client
(new Config($config['monolog_service_clients']))->configureServiceManager($pluginManager);
return $pluginManager;
}
}