3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
12 use Zend\ServiceManager\AbstractFactoryInterface
;
13 use Zend\ServiceManager\ServiceLocatorInterface
;
16 * Logger abstract service factory.
18 * Allow to configure multiple loggers for application.
20 class LoggerAbstractServiceFactory
implements AbstractFactoryInterface
28 * Configuration key holding logger configuration
32 protected $configKey = 'log';
35 * @param ServiceLocatorInterface $services
37 * @param string $requestedName
40 public function canCreateServiceWithName(ServiceLocatorInterface
$services, $name, $requestedName)
42 $config = $this->getConfig($services);
47 return isset($config[$requestedName]);
51 * @param ServiceLocatorInterface $services
53 * @param string $requestedName
56 public function createServiceWithName(ServiceLocatorInterface
$services, $name, $requestedName)
58 $config = $this->getConfig($services);
59 $config = $config[$requestedName];
60 $this->processConfig($config, $services);
61 return new Logger($config);
65 * Retrieve configuration for loggers, if any
67 * @param ServiceLocatorInterface $services
70 protected function getConfig(ServiceLocatorInterface
$services)
72 if ($this->config
!== null) {
76 if (!$services->has('Config')) {
77 $this->config
= array();
81 $config = $services->get('Config');
82 if (!isset($config[$this->configKey
])) {
83 $this->config
= array();
87 $this->config
= $config[$this->configKey
];
91 protected function processConfig(&$config, ServiceLocatorInterface
$services)
93 if (!isset($config['writers'])) {
97 foreach ($config['writers'] as $index => $writerConfig) {
98 if (!isset($writerConfig['name'])
99 ||
strtolower($writerConfig['name']) != 'db'
103 if (!isset($writerConfig['options'])
104 ||
!isset($writerConfig['options']['db'])
108 if (!is_string($writerConfig['options']['db'])) {
111 if (!$services->has($writerConfig['options']['db'])) {
115 // Retrieve the DB service from the service locator, and
116 // inject it into the configuration.
117 $db = $services->get($writerConfig['options']['db']);
118 $config['writers'][$index]['options']['db'] = $db;