LoggerOptionConverter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 helpers
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. require_once(LOG4PHP_DIR . '/LoggerLevel.php');
  24. define('LOG4PHP_OPTION_CONVERTER_DELIM_START', '${');
  25. define('LOG4PHP_OPTION_CONVERTER_DELIM_STOP', '}');
  26. define('LOG4PHP_OPTION_CONVERTER_DELIM_START_LEN', 2);
  27. define('LOG4PHP_OPTION_CONVERTER_DELIM_STOP_LEN', 1);
  28. /**
  29. * A convenience class to convert property values to specific types.
  30. *
  31. * @author VxR <vxr@vxr.it>
  32. * @version $Revision: 1.1 $
  33. * @package log4php
  34. * @subpackage helpers
  35. * @static
  36. * @since 0.5
  37. */
  38. class LoggerOptionConverter {
  39. /**
  40. * OptionConverter is a static class.
  41. */
  42. function OptionConverter()
  43. {
  44. return;
  45. }
  46. /**
  47. * @param array $l
  48. * @param array $r
  49. * @return array
  50. *
  51. * @static
  52. */
  53. function concatanateArrays($l, $r)
  54. {
  55. return array_merge($l, $r);
  56. }
  57. /**
  58. * Read a predefined var.
  59. *
  60. * It returns a value referenced by <var>$key</var> using this search criteria:
  61. * - if <var>$key</var> is a constant then return it. Else
  62. * - if <var>$key</var> is set in <var>$_ENV</var> then return it. Else
  63. * - return <var>$def</var>.
  64. *
  65. * @param string $key The key to search for.
  66. * @param string $def The default value to return.
  67. * @return string the string value of the system property, or the default
  68. * value if there is no property with that key.
  69. *
  70. * @static
  71. */
  72. function getSystemProperty($key, $def)
  73. {
  74. LoggerLog::debug("LoggerOptionConverter::getSystemProperty():key=[{$key}]:def=[{$def}].");
  75. if (defined($key)) {
  76. return (string)constant($key);
  77. } elseif (isset($_ENV[$key])) {
  78. return (string)$_ENV[$key];
  79. } else {
  80. return $def;
  81. }
  82. }
  83. /**
  84. * If <var>$value</var> is <i>true</i>, then <i>true</i> is
  85. * returned. If <var>$value</var> is <i>false</i>, then
  86. * <i>true</i> is returned. Otherwise, <var>$default</var> is
  87. * returned.
  88. *
  89. * <p>Case of value is unimportant.</p>
  90. *
  91. * @param string $value
  92. * @param boolean $default
  93. * @return boolean
  94. *
  95. * @static
  96. */
  97. function toBoolean($value, $default)
  98. {
  99. if($value === null)
  100. return $default;
  101. if ($value == 1)
  102. return true;
  103. $trimmedVal = strtolower(trim($value));
  104. if ("true" == $trimmedVal or "yes" == $trimmedVal)
  105. return true;
  106. if ("false" == $trimmedVal)
  107. return false;
  108. return $default;
  109. }
  110. /**
  111. * @param string $value
  112. * @param integer $default
  113. * @return integer
  114. * @static
  115. */
  116. function toInt($value, $default)
  117. {
  118. $value = trim($value);
  119. if (is_numeric($value)) {
  120. return (int)$value;
  121. } else {
  122. return $default;
  123. }
  124. }
  125. /**
  126. * Converts a standard or custom priority level to a Level
  127. * object.
  128. *
  129. * <p> If <var>$value</var> is of form "<b>level#full_file_classname</b>",
  130. * where <i>full_file_classname</i> means the class filename with path
  131. * but without php extension, then the specified class' <i>toLevel()</i> method
  132. * is called to process the specified level string; if no '#'
  133. * character is present, then the default {@link LoggerLevel}
  134. * class is used to process the level value.</p>
  135. *
  136. * <p>As a special case, if the <var>$value</var> parameter is
  137. * equal to the string "NULL", then the value <i>null</i> will
  138. * be returned.</p>
  139. *
  140. * <p>If any error occurs while converting the value to a level,
  141. * the <var>$defaultValue</var> parameter, which may be
  142. * <i>null</i>, is returned.</p>
  143. *
  144. * <p>Case of <var>$value</var> is insignificant for the level level, but is
  145. * significant for the class name part, if present.</p>
  146. *
  147. * @param string $value
  148. * @param LoggerLevel $defaultValue
  149. * @return LoggerLevel a {@link LoggerLevel} or null
  150. * @static
  151. */
  152. function toLevel($value, $defaultValue)
  153. {
  154. if($value === null)
  155. return $defaultValue;
  156. $hashIndex = strpos($value, '#');
  157. if ($hashIndex === false) {
  158. if("NULL" == strtoupper($value)) {
  159. return null;
  160. } else {
  161. // no class name specified : use standard Level class
  162. return LoggerLevel::toLevel($value, $defaultValue);
  163. }
  164. }
  165. $result = $defaultValue;
  166. $clazz = substr($value, ($hashIndex + 1));
  167. $levelName = substr($value, 0, $hashIndex);
  168. // This is degenerate case but you never know.
  169. if("NULL" == strtoupper($levelName)) {
  170. return null;
  171. }
  172. LoggerLog::debug("LoggerOptionConverter::toLevel():class=[{$clazz}]:pri=[{$levelName}]");
  173. if (!class_exists($clazz))
  174. @include_once("{$clazz}.php");
  175. $clazz = basename($clazz);
  176. if (class_exists($clazz)) {
  177. $result = @call_user_func(array($clazz, 'toLevel'), $value, $defaultValue);
  178. if (!is_a($result, 'loggerlevel')) {
  179. LoggerLog::debug("LoggerOptionConverter::toLevel():class=[{$clazz}] cannot call toLevel(). Returning default.");
  180. $result = $defaultValue;
  181. }
  182. } else {
  183. LoggerLog::warn("LoggerOptionConverter::toLevel() class '{$clazz}' doesnt exists.");
  184. }
  185. return $result;
  186. }
  187. /**
  188. * @param string $value
  189. * @param float $default
  190. * @return float
  191. *
  192. * @static
  193. */
  194. function toFileSize($value, $default)
  195. {
  196. if ($value === null)
  197. return $default;
  198. $s = strtoupper(trim($value));
  199. $multiplier = (float)1;
  200. if(($index = strpos($s, 'KB')) !== false) {
  201. $multiplier = 1024;
  202. $s = substr($s, 0, $index);
  203. } elseif(($index = strpos($s, 'MB')) !== false) {
  204. $multiplier = 1024 * 1024;
  205. $s = substr($s, 0, $index);
  206. } elseif(($index = strpos($s, 'GB')) !== false) {
  207. $multiplier = 1024 * 1024 * 1024;
  208. $s = substr($s, 0, $index);
  209. }
  210. if(is_numeric($s)) {
  211. return (float)$s * $multiplier;
  212. } else {
  213. LoggerLog::warn("LoggerOptionConverter::toFileSize() [{$s}] is not in proper form.");
  214. }
  215. return $default;
  216. }
  217. /**
  218. * Find the value corresponding to <var>$key</var> in
  219. * <var>$props</var>. Then perform variable substitution on the
  220. * found value.
  221. *
  222. * @param string $key
  223. * @param array $props
  224. * @return string
  225. *
  226. * @static
  227. */
  228. function findAndSubst($key, $props)
  229. {
  230. $value = @$props[$key];
  231. if(empty($value)) {
  232. return null;
  233. }
  234. return LoggerOptionConverter::substVars($value, $props);
  235. }
  236. /**
  237. * Perform variable substitution in string <var>$val</var> from the
  238. * values of keys found with the {@link getSystemProperty()} method.
  239. *
  240. * <p>The variable substitution delimeters are <b>${</b> and <b>}</b>.
  241. *
  242. * <p>For example, if the "MY_CONSTANT" contains "value", then
  243. * the call
  244. * <code>
  245. * $s = LoggerOptionConverter::substituteVars("Value of key is ${MY_CONSTANT}.");
  246. * </code>
  247. * will set the variable <i>$s</i> to "Value of key is value.".</p>
  248. *
  249. * <p>If no value could be found for the specified key, then the
  250. * <var>$props</var> parameter is searched, if the value could not
  251. * be found there, then substitution defaults to the empty string.</p>
  252. *
  253. * <p>For example, if {@link getSystemProperty()} cannot find any value for the key
  254. * "inexistentKey", then the call
  255. * <code>
  256. * $s = LoggerOptionConverter::substVars("Value of inexistentKey is [${inexistentKey}]");
  257. * </code>
  258. * will set <var>$s</var> to "Value of inexistentKey is []".</p>
  259. *
  260. * <p>A warn is thrown if <var>$val</var> contains a start delimeter "${"
  261. * which is not balanced by a stop delimeter "}" and an empty string is returned.</p>
  262. *
  263. * @log4j-author Avy Sharell
  264. *
  265. * @param string $val The string on which variable substitution is performed.
  266. * @param array $props
  267. * @return string
  268. *
  269. * @static
  270. */
  271. function substVars($val, $props = null)
  272. {
  273. LoggerLog::debug("LoggerOptionConverter::substVars():val=[{$val}]");
  274. $sbuf = '';
  275. $i = 0;
  276. while(true) {
  277. $j = strpos($val, LOG4PHP_OPTION_CONVERTER_DELIM_START, $i);
  278. if ($j === false) {
  279. LoggerLog::debug("LoggerOptionConverter::substVars() no more variables");
  280. // no more variables
  281. if ($i == 0) { // this is a simple string
  282. LoggerLog::debug("LoggerOptionConverter::substVars() simple string");
  283. return $val;
  284. } else { // add the tail string which contails no variables and return the result.
  285. $sbuf .= substr($val, $i);
  286. LoggerLog::debug("LoggerOptionConverter::substVars():sbuf=[{$sbuf}]. Returning sbuf");
  287. return $sbuf;
  288. }
  289. } else {
  290. $sbuf .= substr($val, $i, $j-$i);
  291. LoggerLog::debug("LoggerOptionConverter::substVars():sbuf=[{$sbuf}]:i={$i}:j={$j}.");
  292. $k = strpos($val, LOG4PHP_OPTION_CONVERTER_DELIM_STOP, $j);
  293. if ($k === false) {
  294. LoggerLog::warn(
  295. "LoggerOptionConverter::substVars() " .
  296. "'{$val}' has no closing brace. Opening brace at position {$j}."
  297. );
  298. return '';
  299. } else {
  300. $j += LOG4PHP_OPTION_CONVERTER_DELIM_START_LEN;
  301. $key = substr($val, $j, $k - $j);
  302. // first try in System properties
  303. $replacement = LoggerOptionConverter::getSystemProperty($key, null);
  304. // then try props parameter
  305. if($replacement == null and $props !== null) {
  306. $replacement = @$props[$key];
  307. }
  308. if(!empty($replacement)) {
  309. // Do variable substitution on the replacement string
  310. // such that we can solve "Hello ${x2}" as "Hello p1"
  311. // the where the properties are
  312. // x1=p1
  313. // x2=${x1}
  314. $recursiveReplacement = LoggerOptionConverter::substVars($replacement, $props);
  315. $sbuf .= $recursiveReplacement;
  316. }
  317. $i = $k + LOG4PHP_OPTION_CONVERTER_DELIM_STOP_LEN;
  318. }
  319. }
  320. }
  321. }
  322. }
  323. ?>