Browse Source

Resolves #ZF-12125
Added support to use a PHP 5.3 namespaced writer

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24703 44c647ce-9c0f-0410-b52a-842ac1e357ba

andries 14 năm trước cách đây
mục cha
commit
252a75a245

+ 22 - 12
library/Zend/Log.php

@@ -214,12 +214,12 @@ class Zend_Log
     }
 
    /**
-     * Construct formatter object from configuration array or Zend_Config object
-     *
-     * @param  array|Zend_Config $config Zend_Config or Array
-     * @return Zend_Log_Formatter_Interface
-     * @throws Zend_Log_Exception
-     */
+    * Construct formatter object from configuration array or Zend_Config object
+    *
+    * @param  array|Zend_Config $config Zend_Config or Array
+    * @return Zend_Log_Formatter_Interface
+    * @throws Zend_Log_Exception
+    */
     protected function _constructFormatterFromConfig($config)
     {
         $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace);
@@ -287,19 +287,29 @@ class Zend_Log
      */
     protected function getClassName($config, $type, $defaultNamespace)
     {
-        if (!isset($config[ $type . 'Name' ])) {
+        if (!isset($config[$type . 'Name'])) {
             require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception("Specify {$type}Name in the configuration array");
         }
-        $className = $config[ $type . 'Name' ];
 
+        $className = $config[$type . 'Name'];
         $namespace = $defaultNamespace;
-        if (isset($config[ $type . 'Namespace' ])) {
-            $namespace = $config[ $type . 'Namespace' ];
+
+        if (isset($config[$type . 'Namespace'])) {
+            $namespace = $config[$type . 'Namespace'];
+        }
+
+        // PHP >= 5.3.0 namespace given?
+        if (substr($namespace, -1) == '\\') {
+            return $namespace . $className;
+        }
+
+        // emtpy namespace given?
+        if (strlen($namespace) === 0) {
+            return $className;
         }
 
-        $fullClassName = $namespace . '_' . $className;
-        return $fullClassName;
+        return $namespace . '_' . $className;
     }
 
     /**

+ 4 - 0
tests/TestHelper.php

@@ -83,3 +83,7 @@ if (defined('TESTS_ZEND_OB_ENABLED') && constant('TESTS_ZEND_OB_ENABLED')) {
  * Unset global variables that are no longer needed.
  */
 unset($zfRoot, $zfCoreLibrary, $zfCoreTests, $path);
+
+// Suppress DateTime warnings
+date_default_timezone_set(@date_default_timezone_get());
+

+ 26 - 1
tests/Zend/Log/LogTest.php

@@ -523,6 +523,31 @@ class Zend_Log_LogTest extends PHPUnit_Framework_TestCase
 
         $this->assertEquals('c', $logger->getTimestampFormat());
     }
+
+    public function testFactorySupportsPHP53Namespaces()
+    {
+        if (version_compare(PHP_VERSION, '5.3.0') < 0) {
+            $this->markTestSkipped('PHP < 5.3.0 does not support namespaces');
+        }
+
+        // preload namespaced class from custom path
+        Zend_Loader::loadClass('\Zfns\Writer', array(dirname(__FILE__) . '/_files'));
+
+        try {
+            $config = array(
+                'mine' => array(
+                    'writerName'      => 'Writer',
+                    'writerNamespace' => '\Zfns\\',
+                )
+            );
+
+            $logger = Zend_log::factory($config);
+            $logger->info('this is a test');
+
+        } catch (Zend_Log_Exception $e) {
+            $this->fail('Unable to load namespaced class');
+        }
+    }
 }
 
 class Zend_Log_Writer_NotExtendedWriterAbstract implements Zend_Log_FactoryInterface
@@ -553,4 +578,4 @@ class Custom_Formatter_Mock extends Zend_Log_Formatter_Abstract
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Log_LogTest::main') {
     Zend_Log_LogTest::main();
-}
+}

+ 28 - 0
tests/Zend/Log/_files/Zfns/Writer.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Zfns;
+
+class Writer extends \Zend_Log_Writer_Abstract
+{
+    /**
+     * Construct a Zend_Log driver
+     *
+     * @param  array|\Zend_Config $config
+     * @return \Zend_Log_FactoryInterface
+     */
+    static public function factory($config)
+    {
+        return new self();
+    }
+
+    /**
+     * Write a message to the log.
+     *
+     * @param  array  $event  log data event
+     * @return void
+     */
+    protected function _write($event)
+    {
+        // Nothing here
+    }
+}