Resque\Logger\Handler\Connector::resolve PHP Method

resolve() public method

Resolves a Monolog handler from string input
public resolve ( $logFormat ) : Monolog\Handler\HandlerInterface
return Monolog\Handler\HandlerInterface
    public function resolve($logFormat)
    {
        // Loop over connectionMap and see if the log format matches any of them
        foreach ($this->connectionMap as $connection => $match) {
            // Because the last connection stream is an effective catch all i.e. just specifying a
            // path to a file, lets make sure the user wasn't trying to use another handler but got
            // the format wrong. If they did then show them the correct format
            if ($connection == 'Stream' and stripos($logFormat, 'stream') !== 0) {
                $pattern = '~^(?P<handler>' . implode('|', array_keys($this->connectionMap)) . ')(.*)$~i';
                if ($possible = $this->matches($pattern, $logFormat)) {
                    // Map to correct key case
                    $handler = str_replace(array_map('strtolower', array_keys($this->connectionMap)), array_keys($this->connectionMap), strtolower($possible['handler']));
                    // Tell them the error of their ways
                    $format = str_replace(array('(?:', ')?', '\\)'), '', $this->connectionMap[$handler]);
                    $cb = function ($m) {
                        return $m[1] == 'ignore' ? '' : '<' . $m[1] . '>';
                    };
                    $format = preg_replace_callback('/\\(\\?P<([a-z_]+)>(?:.+?)\\)/', $cb, $format);
                    throw new \InvalidArgumentException('Invalid format "' . $logFormat . '" for "' . $handler . '" handler. Should be of format "' . $format . '"');
                }
            }
            if ($args = $this->matches('~^' . $match . '$~i', $logFormat)) {
                $connectorClass = new \ReflectionClass('Resque\\Logger\\Handler\\Connector\\' . $connection . 'Connector');
                $connectorClass = $connectorClass->newInstance();
                $handler = $connectorClass->resolve($this->command, $this->input, $this->output, $args);
                $handler->pushProcessor($connectorClass->processor($this->command, $this->input, $this->output, $args));
                return $handler;
            }
        }
        throw new \InvalidArgumentException('Log format "' . $logFormat . '" is invalid');
    }

Usage Example

Example #1
0
 /**
  * Initialises the command just after the input has been validated.
  *
  * This is mainly useful when a lot of commands extends one main command
  * where some things need to be initialised based on the input arguments and options.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  * @return void
  */
 protected function initialize(InputInterface $input, OutputInterface $output)
 {
     $this->parseConfig($input->getOptions(), $this->getNativeDefinition()->getOptionDefaults());
     $config = $this->getConfig();
     // Configure Redis
     Resque\Redis::setConfig(array('scheme' => $config['scheme'], 'host' => $config['host'], 'port' => $config['port'], 'namespace' => $config['namespace'], 'password' => $config['password']));
     // Set the verbosity
     if (array_key_exists('verbose', $config)) {
         if (!$input->getOption('verbose') and !$input->getOption('quiet') and is_int($config['verbose'])) {
             $output->setVerbosity($config['verbose']);
         } else {
             $this->config['verbose'] = $output->getVerbosity();
         }
     }
     // Set the monolog loggers, it's possible to speficfy multiple handlers
     $logs = array_key_exists('log', $config) ? array_unique($config['log']) : array();
     empty($logs) and $logs[] = 'console';
     $handlerConnector = new Resque\Logger\Handler\Connector($this, $input, $output);
     $handlers = array();
     foreach ($logs as $log) {
         $handlers[] = $handlerConnector->resolve($log);
     }
     $this->logger = $logger = new Resque\Logger($handlers);
     // Unset some variables so as not to pass to include file
     unset($logs, $handlerConnector, $handlers);
     // Include file?
     if (array_key_exists('include', $config) and strlen($include = $config['include'])) {
         if (!($includeFile = realpath(dirname($include) . '/' . basename($include))) or !is_readable($includeFile) or !is_file($includeFile) or substr($includeFile, -4) !== '.php') {
             throw new \InvalidArgumentException('The include file "' . $include . '" is not a readable php file.');
         }
         try {
             require_once $includeFile;
         } catch (\Exception $e) {
             throw new \RuntimeException('The include file "' . $include . '" threw an exception: "' . $e->getMessage() . '" on line ' . $e->getLine());
         }
     }
     // This outputs all the events that are fired, useful for learning
     // about when events are fired in the command flow
     if (array_key_exists('events', $config) and $config['events'] === true) {
         Resque\Event::listen('*', function ($event) use($output) {
             $data = array_map(function ($d) {
                 $d instanceof \Exception and $d = '"' . $d->getMessage() . '"';
                 is_array($d) and $d = '[' . implode(',', $d) . ']';
                 return (string) $d;
             }, array_slice(func_get_args(), 1));
             $output->writeln('<comment>-> event:' . Resque\Event::eventName($event) . '(' . implode(',', $data) . ')</comment>');
         });
     }
 }