Author: grobmeier
Date: Fri May 22 14:25:47 2009
New Revision: 777543
URL: http://svn.apache.org/viewvc?rev=777543&view=rev
Log:
removed LoggerPropertySetter in favour to ReflectionUtils. Redirected calls
Removed:
incubator/log4php/trunk/src/main/php/config/
incubator/log4php/trunk/src/test/php/config/
Modified:
incubator/log4php/trunk/CHANGELOG
incubator/log4php/trunk/src/main/php/LoggerManager.php
incubator/log4php/trunk/src/main/php/LoggerReflectionUtils.php
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorIni.php
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorXml.php
incubator/log4php/trunk/src/test/php/LoggerReflectionUtilsTest.php
Modified: incubator/log4php/trunk/CHANGELOG
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/CHANGELOG?rev=777543&r1=777542&r2=777543&view=diff
==============================================================================
--- incubator/log4php/trunk/CHANGELOG (original)
+++ incubator/log4php/trunk/CHANGELOG Fri May 22 14:25:47 2009
@@ -36,6 +36,7 @@
- Enh: Initial port to PHP 5 (Knut Urdalen)
- Enh: Established new unit test suite (Knut Urdalen)
- Enh: Added a range of examples (Knut Urdalen)
+- Enh: Created common ReflectionUtils class and moved factory calls to there
(Christian Grobmeier)
Version 0.9 December 10th, 2003
-------------------------------
Modified: incubator/log4php/trunk/src/main/php/LoggerManager.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/LoggerManager.php?rev=777543&r1=777542&r2=777543&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/LoggerManager.php (original)
+++ incubator/log4php/trunk/src/main/php/LoggerManager.php Fri May 22 14:25:47
2009
@@ -74,7 +74,6 @@
'LoggerAppenderRollingFile' =>
'/appenders/LoggerAppenderRollingFile.php',
'LoggerAppenderSocket' => '/appenders/LoggerAppenderSocket.php',
'LoggerAppenderSyslog' => '/appenders/LoggerAppenderSyslog.php',
- 'LoggerPropertySetter' => '/config/LoggerPropertySetter.php',
'LoggerFormattingInfo' => '/helpers/LoggerFormattingInfo.php',
'LoggerOptionConverter' => '/helpers/LoggerOptionConverter.php',
'LoggerPatternConverter' =>
'/helpers/LoggerPatternConverter.php',
Modified: incubator/log4php/trunk/src/main/php/LoggerReflectionUtils.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/LoggerReflectionUtils.php?rev=777543&r1=777542&r2=777543&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/LoggerReflectionUtils.php (original)
+++ incubator/log4php/trunk/src/main/php/LoggerReflectionUtils.php Fri May 22
14:25:47 2009
@@ -23,6 +23,104 @@
* Provides methods for reflective use on php objects
*/
class LoggerReflectionUtils {
+ /** the target object */
+ private $obj;
+
+ /**
+ * Create a new LoggerReflectionUtils for the specified Object.
+ * This is done in prepartion for invoking {...@link setProperty()}
+ * one or more times.
+ * @param object &$obj the object for which to set properties
+ */
+ public function __construct(&$obj) {
+ $this->obj =& $obj;
+ }
+
+ /**
+ * Set the properties of an object passed as a parameter in one
+ * go. The <code>properties</code> are parsed relative to a
+ * <code>prefix</code>.
+ *
+ * @param object &$obj The object to configure.
+ * @param array $properties An array containing keys and values.
+ * @param string $prefix Only keys having the specified prefix will be
set.
+ * @static
+ */
+ // TODO: check, if this is really useful
+ public static function setPropertiesByObject(&$obj, $properties,
$prefix) {
+ $pSetter = new LoggerReflectionUtils($obj);
+ return $pSetter->setProperties($properties, $prefix);
+ }
+
+
+ /**
+ * Set the properites for the object that match the
+ * <code>prefix</code> passed as parameter.
+ *
+ * Example:
+ *
+ * $arr['xxxname'] = 'Joe';
+ * $arr['xxxmale'] = true;
+ * and prefix xxx causes setName and setMale.
+ *
+ * @param array $properties An array containing keys and values.
+ * @param string $prefix Only keys having the specified prefix will be
set.
+ */
+ // TODO: check, if this is really useful
+ public function setProperties($properties, $prefix) {
+ $len = strlen($prefix);
+ while(list($key,) = each($properties)) {
+ if(strpos($key, $prefix) === 0) {
+ if(strpos($key, '.', ($len + 1)) > 0) {
+ continue;
+ }
+ $value =
LoggerOptionConverter::findAndSubst($key, $properties);
+ $key = substr($key, $len);
+ if($key == 'layout' and ($this->obj instanceof
LoggerAppender)) {
+ continue;
+ }
+ $this->setProperty($key, $value);
+ }
+ }
+ $this->activate();
+ }
+
+ /**
+ * Set a property on this PropertySetter's Object. If successful, this
+ * method will invoke a setter method on the underlying Object. The
+ * setter is the one for the specified property name and the value is
+ * determined partly from the setter argument type and partly from the
+ * value specified in the call to this method.
+ *
+ * <p>If the setter expects a String no conversion is necessary.
+ * If it expects an int, then an attempt is made to convert 'value'
+ * to an int using new Integer(value). If the setter expects a boolean,
+ * the conversion is by new Boolean(value).
+ *
+ * @param string $name name of the property
+ * @param string $value String value of the property
+ */
+ public function setProperty($name, $value) {
+ if($value === null) {
+ return;
+ }
+
+ $method = "set" . ucfirst($name);
+
+ if(!method_exists($this->obj, $method)) {
+ // no such setter method
+ return;
+ } else {
+ return call_user_func(array(&$this->obj, $method),
$value);
+ }
+ }
+
+ public function activate() {
+ if(method_exists($this->obj, 'activateoptions')) {
+ return call_user_func(array(&$this->obj,
'activateoptions'));
+ }
+ }
+
/**
* Creates an instances from the given class name.
*
Modified:
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorIni.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorIni.php?rev=777543&r1=777542&r2=777543&view=diff
==============================================================================
---
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorIni.php
(original)
+++
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorIni.php
Fri May 22 14:25:47 2009
@@ -377,7 +377,7 @@
$loggerFactory = $this->loggerFactory;
}
-
LoggerPropertySetter::setPropertiesByObject($loggerFactory, $props,
LOG4PHP_LOGGER_PROPERTY_CONFIGURATOR_FACTORY_PREFIX . ".");
+
LoggerReflectionUtils::setPropertiesByObject($loggerFactory, $props,
LOG4PHP_LOGGER_PROPERTY_CONFIGURATOR_FACTORY_PREFIX . ".");
}
}
@@ -557,11 +557,11 @@
}
}
- LoggerPropertySetter::setPropertiesByObject($layout,
$props, $layoutPrefix . ".");
+ LoggerReflectionUtils::setPropertiesByObject($layout,
$props, $layoutPrefix . ".");
$appender->setLayout($layout);
}
- LoggerPropertySetter::setPropertiesByObject($appender, $props,
$prefix . ".");
+ LoggerReflectionUtils::setPropertiesByObject($appender, $props,
$prefix . ".");
return $appender;
}
Modified:
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorXml.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorXml.php?rev=777543&r1=777542&r2=777543&view=diff
==============================================================================
---
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorXml.php
(original)
+++
incubator/log4php/trunk/src/main/php/configurators/LoggerConfiguratorXml.php
Fri May 22 14:25:47 2009
@@ -477,7 +477,7 @@
*/
function setter(&$object, $name, $value)
{
- // TODO: check if this can be replaced with LoggerPropertySetter
+ // TODO: check if this can be replaced with LoggerReflectionUtils
if (empty($name)) {
return false;
}
Modified: incubator/log4php/trunk/src/test/php/LoggerReflectionUtilsTest.php
URL:
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/LoggerReflectionUtilsTest.php?rev=777543&r1=777542&r2=777543&view=diff
==============================================================================
--- incubator/log4php/trunk/src/test/php/LoggerReflectionUtilsTest.php
(original)
+++ incubator/log4php/trunk/src/test/php/LoggerReflectionUtilsTest.php Fri May
22 14:25:47 2009
@@ -22,11 +22,64 @@
* @link http://logging.apache.org/log4php
*/
+
+class Simple {
+ private $name;
+ private $male;
+
+ public function getName() {
+ return $this->name;
+ }
+
+ public function isMale() {
+ return $this->male;
+ }
+
+ public function setName($name) {
+ $this->name = $name;
+ }
+
+ public function setMale($male) {
+ $this->male = $male;
+ }
+}
/**
* Tests the LoggerReflectionUtils class
*/
class LoggerReflectionUtilsTest extends PHPUnit_Framework_TestCase {
+ public function testSimpleSet() {
+ $s = new Simple();
+ $ps = new LoggerReflectionUtils($s);
+ $ps->setProperty("name", "Joe");
+ $ps->setProperty("male", true);
+
+ $this->assertEquals($s->isMale(), true);
+ $this->assertEquals($s->getName(), 'Joe');
+ }
+
+ public function testSimpleArraySet() {
+ $arr['xxxname'] = 'Joe';
+ $arr['xxxmale'] = true;
+
+ $s = new Simple();
+ $ps = new LoggerReflectionUtils($s);
+ $ps->setProperties($arr, "xxx");
+
+ $this->assertEquals($s->getName(), 'Joe');
+ $this->assertEquals($s->isMale(), true);
+ }
+
+ public function testStaticArraySet() {
+ $arr['xxxname'] = 'Joe';
+ $arr['xxxmale'] = true;
+
+ $s = new Simple();
+ LoggerReflectionUtils::setPropertiesByObject($s,$arr,"xxx");
+
+ $this->assertEquals($s->getName(), 'Joe');
+ $this->assertEquals($s->isMale(), true);
+ }
public function testCreateObject() {
$object =
LoggerReflectionUtils::createObject('LoggerLayoutSimple');
$name = get_class($object);