PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/SyracavaPHP/syracava/ext/log4php/config/LoggerPropertySetter.php

http://syracava.googlecode.com/
PHP | 161 lines | 58 code | 15 blank | 88 comment | 12 complexity | 5d241d77caee4dc424c7bbf2030c5ed0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage config
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  24. require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
  25. /**
  26. * General purpose Object property setter. Clients repeatedly invokes
  27. * {@link setProperty()} in order to invoke setters
  28. * on the Object specified in the constructor.
  29. *
  30. * Usage:
  31. * <code>
  32. * $ps = new LoggerPropertySetter($anObject);
  33. * $ps->set("name", "Joe");
  34. * $ps->set("age", 32);
  35. * $ps->set("isMale", true);
  36. * </code>
  37. * will cause the invocations
  38. * <code>
  39. * $anObject->setName("Joe");
  40. * $anObject->setAge(32);
  41. * $anObject->setMale(true)
  42. * </code>
  43. * if such methods exist.
  44. *
  45. * @author VxR <vxr@vxr.it>
  46. * @version $Revision: 125 $
  47. * @package log4php
  48. * @subpackage config
  49. * @since 0.5
  50. */
  51. class LoggerPropertySetter {
  52. /**
  53. * @var object the target object
  54. * @access private
  55. */
  56. var $obj;
  57. /**
  58. * Create a new LoggerPropertySetter for the specified Object.
  59. * This is done in prepartion for invoking {@link setProperty()}
  60. * one or more times.
  61. * @param object &$obj the object for which to set properties
  62. */
  63. function LoggerPropertySetter(&$obj)
  64. {
  65. $this->obj =& $obj;
  66. }
  67. /**
  68. * Set the properties of an object passed as a parameter in one
  69. * go. The <code>properties</code> are parsed relative to a
  70. * <code>prefix</code>.
  71. *
  72. * @param object &$obj The object to configure.
  73. * @param array $properties An array containing keys and values.
  74. * @param string $prefix Only keys having the specified prefix will be set.
  75. * @static
  76. */
  77. function setPropertiesByObject(&$obj, $properties, $prefix)
  78. {
  79. $pSetter = new LoggerPropertySetter($obj);
  80. return $pSetter->setProperties($properties, $prefix);
  81. }
  82. /**
  83. * Set the properites for the object that match the
  84. * <code>prefix</code> passed as parameter.
  85. *
  86. * @param array $properties An array containing keys and values.
  87. * @param string $prefix Only keys having the specified prefix will be set.
  88. */
  89. function setProperties($properties, $prefix)
  90. {
  91. LoggerLog::debug("LoggerOptionConverter::setProperties():prefix=[{$prefix}]");
  92. $len = strlen($prefix);
  93. while (list($key,) = each($properties)) {
  94. if (strpos($key, $prefix) === 0) {
  95. if (strpos($key, '.', ($len + 1)) > 0)
  96. continue;
  97. $value = LoggerOptionConverter::findAndSubst($key, $properties);
  98. $key = substr($key, $len);
  99. if ($key == 'layout' and is_a($this->obj, 'loggerappender')) {
  100. continue;
  101. }
  102. $this->setProperty($key, $value);
  103. }
  104. }
  105. $this->activate();
  106. }
  107. /**
  108. * Set a property on this PropertySetter's Object. If successful, this
  109. * method will invoke a setter method on the underlying Object. The
  110. * setter is the one for the specified property name and the value is
  111. * determined partly from the setter argument type and partly from the
  112. * value specified in the call to this method.
  113. *
  114. * <p>If the setter expects a String no conversion is necessary.
  115. * If it expects an int, then an attempt is made to convert 'value'
  116. * to an int using new Integer(value). If the setter expects a boolean,
  117. * the conversion is by new Boolean(value).
  118. *
  119. * @param string $name name of the property
  120. * @param string $value String value of the property
  121. */
  122. function setProperty($name, $value)
  123. {
  124. LoggerLog::debug("LoggerOptionConverter::setProperty():name=[{$name}]:value=[{$value}]");
  125. if ($value === null) return;
  126. $method = "set" . ucfirst($name);
  127. if (!method_exists($this->obj, $method)) {
  128. LoggerLog::warn(
  129. "LoggerOptionConverter::setProperty() No such setter method for [{$name}] property in " .
  130. get_class($this->obj) . "."
  131. );
  132. } else {
  133. return call_user_func(array(&$this->obj, $method), $value);
  134. }
  135. }
  136. function activate()
  137. {
  138. LoggerLog::debug("LoggerOptionConverter::activate()");
  139. if (method_exists($this->obj, 'activateoptions')) {
  140. return call_user_func(array(&$this->obj, 'activateoptions'));
  141. } else {
  142. LoggerLog::debug("LoggerOptionConverter::activate() Nothing to activate.");
  143. }
  144. }
  145. }
  146. ?>