1<?php 2/** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10namespace Zend\Log; 11 12use Zend\ServiceManager\AbstractFactoryInterface; 13use Zend\ServiceManager\AbstractPluginManager; 14use Zend\ServiceManager\ServiceLocatorInterface; 15 16/** 17 * Logger abstract service factory. 18 * 19 * Allow to configure multiple loggers for application. 20 */ 21class LoggerAbstractServiceFactory implements AbstractFactoryInterface 22{ 23 /** 24 * @var array 25 */ 26 protected $config; 27 28 /** 29 * Configuration key holding logger configuration 30 * 31 * @var string 32 */ 33 protected $configKey = 'log'; 34 35 /** 36 * @param ServiceLocatorInterface $services 37 * @param string $name 38 * @param string $requestedName 39 * @return bool 40 */ 41 public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) 42 { 43 $config = $this->getConfig($services); 44 if (empty($config)) { 45 return false; 46 } 47 48 return isset($config[$requestedName]); 49 } 50 51 /** 52 * @param ServiceLocatorInterface $services 53 * @param string $name 54 * @param string $requestedName 55 * @return Logger 56 */ 57 public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) 58 { 59 $config = $this->getConfig($services); 60 $config = $config[$requestedName]; 61 $this->processConfig($config, $services); 62 return new Logger($config); 63 } 64 65 /** 66 * Retrieve configuration for loggers, if any 67 * 68 * @param ServiceLocatorInterface $services 69 * @return array 70 */ 71 protected function getConfig(ServiceLocatorInterface $services) 72 { 73 if ($this->config !== null) { 74 return $this->config; 75 } 76 77 if (!$services->has('Config')) { 78 $this->config = array(); 79 return $this->config; 80 } 81 82 $config = $services->get('Config'); 83 if (!isset($config[$this->configKey])) { 84 $this->config = array(); 85 return $this->config; 86 } 87 88 $this->config = $config[$this->configKey]; 89 return $this->config; 90 } 91 92 protected function processConfig(&$config, ServiceLocatorInterface $services) 93 { 94 if (isset($config['writer_plugin_manager']) 95 && is_string($config['writer_plugin_manager']) 96 && $services->has($config['writer_plugin_manager']) 97 ) { 98 $config['writer_plugin_manager'] = $services->get($config['writer_plugin_manager']); 99 } 100 101 if ((!isset($config['writer_plugin_manager']) 102 || ! $config['writer_plugin_manager'] instanceof AbstractPluginManager) 103 && $services->has('LogWriterManager') 104 ) { 105 $config['writer_plugin_manager'] = $services->get('LogWriterManager'); 106 } 107 108 if (isset($config['processor_plugin_manager']) 109 && is_string($config['processor_plugin_manager']) 110 && $services->has($config['processor_plugin_manager']) 111 ) { 112 $config['processor_plugin_manager'] = $services->get($config['processor_plugin_manager']); 113 } 114 115 if ((!isset($config['processor_plugin_manager']) 116 || ! $config['processor_plugin_manager'] instanceof AbstractPluginManager) 117 && $services->has('LogProcessorManager') 118 ) { 119 $config['processor_plugin_manager'] = $services->get('LogProcessorManager'); 120 } 121 122 if (!isset($config['writers'])) { 123 return; 124 } 125 126 foreach ($config['writers'] as $index => $writerConfig) { 127 if (!isset($writerConfig['options']['db']) 128 || !is_string($writerConfig['options']['db']) 129 ) { 130 continue; 131 } 132 if (!$services->has($writerConfig['options']['db'])) { 133 continue; 134 } 135 136 // Retrieve the DB service from the service locator, and 137 // inject it into the configuration. 138 $db = $services->get($writerConfig['options']['db']); 139 $config['writers'][$index]['options']['db'] = $db; 140 } 141 } 142} 143