Index: library/Zend/Log/Writer/Mail.php =================================================================== --- library/Zend/Log/Writer/Mail.php (revision 22826) +++ library/Zend/Log/Writer/Mail.php (working copy) @@ -117,17 +117,108 @@ $this->_layout = $layout; $this->_formatter = new Zend_Log_Formatter_Simple(); } - + /** * Create a new instance of Zend_Log_Writer_Mail - * + * * @param array|Zend_Config $config * @return Zend_Log_Writer_Mail * @throws Zend_Log_Exception */ static public function factory($config) { - throw new Zend_Exception('Zend_Log_Writer_Mail does not currently implement a factory'); + $config = self::_parseConfig($config); + array_change_key_case($config, CASE_LOWER); + + // create mail (required) + if (!array_key_exists('mail', $config)) { + $config['mail'] = 'Zend_Mail'; + } + if (!array_key_exists('mailparams', $config) || !is_array($config['mailparams'])) { + $config['mailparams'] = array(); + } + $mail = self::_factoryMail($config['mail'], $config['mailparams']); + + // create layout (optional) + if (array_key_exists('layout', $config)) { + if (!array_key_exists('layoutparams', $config) || !is_array($config['layoutparams'])) { + $config['layoutparams'] = array(); + } + $layout = self::_factoryLayout($config['layout'], $config['layoutparams']); + } else { + $layout = null; + } + + // create writer + return new self($mail, $layout); + } + + /** + * @param string $mail + * @param array $config + * @throws Zend_Log_Exception When $mail is not valid + * @return Zend_Mail + */ + protected static function _factoryMail($mail, array $config) + { + // create object + if (!is_object($mail)) { + /** + * @see Zend_Loader + */ + require_once 'Zend/Loader.php'; + Zend_Loader::loadClass($mail); + $mail = new $mail; + } + + // check object + if (!$mail instanceof Zend_Mail) { + throw new Zend_Log_Exception( + "Invalid mail class, it is not a sub class of Zend_Mail"); + } + + // setup object + foreach ($config as $name => $value) { + foreach (array('add', 'set') as $operation) { + $method = $operation . $name; + if (is_callable(array($mail, $method))) { + $mail->$method($value); + break; + } + } + } + + return $mail; + } + + /** + * @param string $layout + * @param Zend_Config|array|string $config + * @throws Zend_Log_Exception When $layout is not valid + * @return Zend_Layout + */ + protected static function _factoryLayout($layout, $config) + { + // create object + if (!is_object($layout)) { + /** + * @see Zend_Loader + */ + require_once 'Zend/Loader.php'; + Zend_Loader::loadClass($layout); + $layout = new $layout(); + } + + // check object + if (!$layout instanceof Zend_Layout) { + throw new Zend_Log_Exception( + "Invalid layout class, it is not a sub class of Zend_Layout"); + } + + // setup object + $layout->setOptions($config); + + return $layout; } /**