logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ihabu...@apache.org
Subject git commit: LOG4PHP-200: Improved superglobal access in converters
Date Tue, 08 Jan 2013 09:27:13 GMT
Updated Branches:
  refs/heads/develop e40ac7fad -> 8abee3ad8


LOG4PHP-200: Improved superglobal access in converters

Removed the current, slightly hackish access to superglobal variables,
i.e. ${$this->name}. Instead added a function in each sub-class which
returns the contents of the required superglobal. This fixes the issue and
replaces hacky accessing with more vanilla code.

Signed-off-by: Ivan Habunek <ihabunek@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/logging-log4php/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4php/commit/8abee3ad
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4php/tree/8abee3ad
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4php/diff/8abee3ad

Branch: refs/heads/develop
Commit: 8abee3ad8dc3d6bec46549849457b263e18e12bd
Parents: e40ac7f
Author: Ivan Habunek <ihabunek@apache.org>
Authored: Mon Jan 7 15:48:39 2013 +0100
Committer: Ivan Habunek <ihabunek@apache.org>
Committed: Mon Jan 7 15:48:39 2013 +0100

----------------------------------------------------------------------
 .../php/pattern/LoggerPatternConverterCookie.php   |    6 ++-
 .../pattern/LoggerPatternConverterEnvironment.php  |    4 +-
 .../php/pattern/LoggerPatternConverterRequest.php  |    6 ++-
 .../php/pattern/LoggerPatternConverterServer.php   |    4 +-
 .../php/pattern/LoggerPatternConverterSession.php  |    4 +-
 .../pattern/LoggerPatternConverterSuperglobal.php  |   35 +--------------
 .../php/pattern/LoggerPatternConverterTest.php     |   14 ------
 7 files changed, 19 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/main/php/pattern/LoggerPatternConverterCookie.php
----------------------------------------------------------------------
diff --git a/src/main/php/pattern/LoggerPatternConverterCookie.php b/src/main/php/pattern/LoggerPatternConverterCookie.php
index 6b0e221..2118193 100644
--- a/src/main/php/pattern/LoggerPatternConverterCookie.php
+++ b/src/main/php/pattern/LoggerPatternConverterCookie.php
@@ -30,5 +30,7 @@
  * @since 2.3
  */
 class LoggerPatternConverterCookie extends LoggerPatternConverterSuperglobal {
-	protected $name = '_COOKIE';
-}
\ No newline at end of file
+	protected function getSource() {
+		return $_COOKIE;
+	}
+}

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/main/php/pattern/LoggerPatternConverterEnvironment.php
----------------------------------------------------------------------
diff --git a/src/main/php/pattern/LoggerPatternConverterEnvironment.php b/src/main/php/pattern/LoggerPatternConverterEnvironment.php
index 3096b20..0b3aabe 100644
--- a/src/main/php/pattern/LoggerPatternConverterEnvironment.php
+++ b/src/main/php/pattern/LoggerPatternConverterEnvironment.php
@@ -30,5 +30,7 @@
  * @since 2.3
  */
 class LoggerPatternConverterEnvironment extends LoggerPatternConverterSuperglobal {
-	protected $name = '_ENV';
+	protected function getSource() {
+		return $_ENV;
+	}
 }

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/main/php/pattern/LoggerPatternConverterRequest.php
----------------------------------------------------------------------
diff --git a/src/main/php/pattern/LoggerPatternConverterRequest.php b/src/main/php/pattern/LoggerPatternConverterRequest.php
index ac45e6b..939805a 100644
--- a/src/main/php/pattern/LoggerPatternConverterRequest.php
+++ b/src/main/php/pattern/LoggerPatternConverterRequest.php
@@ -30,5 +30,7 @@
  * @since 2.3
  */
 class LoggerPatternConverterRequest extends LoggerPatternConverterSuperglobal {
-	protected $name = '_REQUEST';
-}
\ No newline at end of file
+	protected function getSource() {
+		return $_REQUEST;
+	}
+}

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/main/php/pattern/LoggerPatternConverterServer.php
----------------------------------------------------------------------
diff --git a/src/main/php/pattern/LoggerPatternConverterServer.php b/src/main/php/pattern/LoggerPatternConverterServer.php
index 27a983d..636f848 100644
--- a/src/main/php/pattern/LoggerPatternConverterServer.php
+++ b/src/main/php/pattern/LoggerPatternConverterServer.php
@@ -30,5 +30,7 @@
  * @since 2.3
  */
 class LoggerPatternConverterServer extends LoggerPatternConverterSuperglobal {
-	protected $name = '_SERVER';
+	protected function getSource() {
+		return $_SERVER;
+	}
 }

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/main/php/pattern/LoggerPatternConverterSession.php
----------------------------------------------------------------------
diff --git a/src/main/php/pattern/LoggerPatternConverterSession.php b/src/main/php/pattern/LoggerPatternConverterSession.php
index a73e27a..8f572c7 100644
--- a/src/main/php/pattern/LoggerPatternConverterSession.php
+++ b/src/main/php/pattern/LoggerPatternConverterSession.php
@@ -30,5 +30,7 @@
  * @since 2.3
  */
 class LoggerPatternConverterSession extends LoggerPatternConverterSuperglobal {
-	protected $name = '_SESSION';
+	protected function getSource() {
+		return $_SESSION;
+	}
 }

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/main/php/pattern/LoggerPatternConverterSuperglobal.php
----------------------------------------------------------------------
diff --git a/src/main/php/pattern/LoggerPatternConverterSuperglobal.php b/src/main/php/pattern/LoggerPatternConverterSuperglobal.php
index f1164d3..dbabeca 100644
--- a/src/main/php/pattern/LoggerPatternConverterSuperglobal.php
+++ b/src/main/php/pattern/LoggerPatternConverterSuperglobal.php
@@ -36,12 +36,6 @@
  * @since 2.3
  */
 abstract class LoggerPatternConverterSuperglobal extends LoggerPatternConverter {
-
-	/** 
-	 * Name of the superglobal variable, to be defined by subclasses. 
-	 * For example: "_SERVER" or "_ENV". 
-	 */
-	protected $name;
 	
 	protected $value = '';
 	
@@ -50,33 +44,8 @@ abstract class LoggerPatternConverterSuperglobal extends LoggerPatternConverter
 		if (isset($this->option) && $this->option !== '') {
 			$key = $this->option;
 		}
-	
-		/*
-		 * There is a bug in PHP which doesn't allow superglobals to be 
-		 * accessed when their name is stored in a variable, e.g.:
-		 * 
-		 * $name = '_SERVER';
-		 * $array = $$name;
-		 * 
-		 * This code does not work when run from within a method (only when run
-		 * in global scope). But the following code does work: 
-		 * 
-		 * $name = '_SERVER';
-		 * global $$name;
-		 * $array = $$name;
-		 * 
-		 * That's why global is used here.
-		 */
-		global ${$this->name};
-			
-		// Check the given superglobal exists. It is possible that it is not initialized.
-		if (!isset(${$this->name})) {
-			$class = get_class($this);
-			trigger_error("log4php: $class: Cannot find superglobal variable \${$this->name}.",
E_USER_WARNING);
-			return;
-		}
-		
-		$source = ${$this->name};
+
+		$source = $this->getSource();
 		
 		// When the key is set, display the matching value
 		if (isset($key)) {

http://git-wip-us.apache.org/repos/asf/logging-log4php/blob/8abee3ad/src/test/php/pattern/LoggerPatternConverterTest.php
----------------------------------------------------------------------
diff --git a/src/test/php/pattern/LoggerPatternConverterTest.php b/src/test/php/pattern/LoggerPatternConverterTest.php
index 704930e..ec09edd 100644
--- a/src/test/php/pattern/LoggerPatternConverterTest.php
+++ b/src/test/php/pattern/LoggerPatternConverterTest.php
@@ -22,11 +22,6 @@
  * @link       http://logging.apache.org/log4php
  */
 
-/** Converter referencing non-existant superglobal variable. */
-class LoggerInvalidSuperglobalConverter extends LoggerPatternConverterSuperglobal {
-	protected $name = '_FOO';
-}
-
 /**
  * @group pattern
  */
@@ -305,15 +300,6 @@ class LoggerPatternConverterTest extends PHPUnit_Framework_TestCase {
 		self::assertSame($expected, $actual);
 	}
 
-	/**
-	 * @expectedException PHPUnit_Framework_Error
-	 * @expectedExceptionMessage log4php: LoggerInvalidSuperglobalConverter: Cannot find superglobal
variable $_FOO
-	 */
-	public function testNonexistantSuperglobal() {
-		$converter = new LoggerInvalidSuperglobalConverter($this->info);
-		$actual = $converter->convert($this->event);
-	}
-
 	public function testFormattingTrimRight() {
 		$event = LoggerTestHelper::getInfoEvent('0123456789');
 


Mime
View raw message