Cascade\Config\Loader\ClassLoader\LoggerLoader::resolveHandlers PHP Method

resolveHandlers() public method

Resolve handlers for that Logger (if any provided) against an array of previously set up handlers. Returns an array of valid handlers.
public resolveHandlers ( array $loggerOptions, array $handlers ) : Monolog\Handler\HandlerInterface[]
$loggerOptions array Array of logger options
$handlers array Available Handlers to resolve against
return Monolog\Handler\HandlerInterface[] Array of Monolog handlers
    public function resolveHandlers(array $loggerOptions, array $handlers)
    {
        $handlerArray = array();
        if (isset($loggerOptions['handlers'])) {
            // If handlers have been specified and, they do exist in the provided handlers array
            // We return an array of handler objects
            foreach ($loggerOptions['handlers'] as $handlerId) {
                if (isset($handlers[$handlerId])) {
                    $handlerArray[] = $handlers[$handlerId];
                } else {
                    throw new \InvalidArgumentException(sprintf('Cannot add handler "%s" to the logger "%s". Handler not found.', $handlerId, $this->logger->getName()));
                }
            }
        }
        // If nothing is set there is nothing to resolve, Handlers will be Monolog's default
        return $handlerArray;
    }

Usage Example

 /**
  * @expectedException InvalidArgumentException
  */
 public function testResolveHandlersWithMismatch()
 {
     $options = array('handlers' => array('unexisting_handler', 'test_handler_2'));
     $handlers = array('test_handler_1' => new TestHandler(), 'test_handler_2' => new TestHandler());
     $loader = new LoggerLoader('testLogger', $options, $handlers);
     // This should throw an InvalidArgumentException
     $loader->resolveHandlers($options, $handlers);
 }