Просмотр исходного кода

Zend_Log can be extended when using factory

Fixes #85
Supersedes and closes #86
Martin Hujer 12 лет назад
Родитель
Сommit
4c14c96a5a

+ 3 - 1
documentation/manual/en/module_specs/Zend_Log-Factory.xml

@@ -8,7 +8,9 @@
         <methodname>factory()</methodname> method to instantiate a Log instance, as well as to
         configure attached writers and their filters. Using the factory, you can attach zero or
         more writers. Configuration may be passed as either an array or a
-        <classname>Zend_Config</classname> instance.
+        <classname>Zend_Config</classname> instance. If you want to create an instance of
+        a custom class (extending Zend_Log), you can pass a <varname>className</varname>
+        option to the <methodname>factory()</methodname> method.
     </para>
 
     <para>

+ 14 - 1
library/Zend/Log.php

@@ -136,7 +136,20 @@ class Zend_Log
             throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config');
         }
 
-        $log = new self;
+        if (array_key_exists('className', $config)) {
+            $class = $config['className'];
+            unset($config['className']);
+        } else {
+            $class = __CLASS__;
+        }
+
+        $log = new $class;
+
+        if (!$log instanceof Zend_Log) {
+            /** @see Zend_Log_Exception */
+            require_once 'Zend/Log/Exception.php';
+            throw new Zend_Log_Exception('Passed className does not belong to a descendant of Zend_Log');
+        }
 
         if (array_key_exists('timestampFormat', $config)) {
             if (null != $config['timestampFormat'] && '' != $config['timestampFormat']) {

+ 37 - 0
tests/Zend/Log/LogTest.php

@@ -548,6 +548,35 @@ class Zend_Log_LogTest extends PHPUnit_Framework_TestCase
             $this->fail('Unable to load namespaced class');
         }
     }
+
+    /**
+     * @group #85
+     */
+    public function testZendLogCanBeExtendedWhenUsingFactory()
+    {
+        $writer = new Zend_Log_Writer_Null();
+        $log = ZLTest_My_Log::factory(
+            array(
+                'writerName' => $writer,
+                'className' => 'ZLTest_My_Log'
+            )
+        );
+        $this->assertTrue($log instanceof ZLTest_My_Log);
+    }
+
+    /**
+     * @expectedException Zend_Log_Exception
+     */
+    public function testZendLogThrowsAnExceptionWhenPassingIncorrectClassToFactory()
+    {
+        $writer = new Zend_Log_Writer_Null();
+        ZLTest_My_Log::factory(
+            array(
+                'writerName' => $writer,
+                'className' => 'ZLTest_My_LogNotExtending'
+            )
+        );
+    }
 }
 
 class Zend_Log_Writer_NotExtendedWriterAbstract implements Zend_Log_FactoryInterface
@@ -576,6 +605,14 @@ class Custom_Formatter_Mock extends Zend_Log_Formatter_Abstract
     }
 }
 
+/**
+ * Helper classes for testZendLogCanBeExtendedWhenUsingFactory()
+ *
+ * @group #85
+ */
+class ZLTest_My_Log extends Zend_Log {}
+class ZLTest_My_LogNotExtending {}
+
 if (PHPUnit_MAIN_METHOD == 'Zend_Log_LogTest::main') {
     Zend_Log_LogTest::main();
 }