PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/inc/lib/phpdocx/lib/log4php/helpers/LoggerOptionConverter.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 322 lines | 129 code | 21 blank | 172 comment | 48 complexity | 5459e520541c28283e906c1533019e23 MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @package log4php
  19. */
  20. /**
  21. * A convenience class to convert property values to specific types.
  22. *
  23. * @version $Revision: 822464 $
  24. * @package log4php
  25. * @subpackage helpers
  26. * @static
  27. * @since 0.5
  28. */
  29. class LoggerOptionConverter {
  30. const DELIM_START = '${';
  31. const DELIM_STOP = '}';
  32. const DELIM_START_LEN = 2;
  33. const DELIM_STOP_LEN = 1;
  34. /**
  35. * Read a predefined var.
  36. *
  37. * It returns a value referenced by <var>$key</var> using this search criteria:
  38. * - if <var>$key</var> is a constant then return it. Else
  39. * - if <var>$key</var> is set in <var>$_ENV</var> then return it. Else
  40. * - return <var>$def</var>.
  41. *
  42. * @param string $key The key to search for.
  43. * @param string $def The default value to return.
  44. * @return string the string value of the system property, or the default
  45. * value if there is no property with that key.
  46. *
  47. * @static
  48. */
  49. public static function getSystemProperty($key, $def) {
  50. if(defined($key)) {
  51. return (string)constant($key);
  52. } else if(isset($_SERVER[$key])) {
  53. return (string)$_SERVER[$key];
  54. } else if(isset($_ENV[$key])) {
  55. return (string)$_ENV[$key];
  56. } else {
  57. return $def;
  58. }
  59. }
  60. /**
  61. * If <var>$value</var> is <i>true</i>, then <i>true</i> is
  62. * returned. If <var>$value</var> is <i>false</i>, then
  63. * <i>true</i> is returned. Otherwise, <var>$default</var> is
  64. * returned.
  65. *
  66. * <p>Case of value is unimportant.</p>
  67. *
  68. * @param string $value
  69. * @param boolean $default
  70. * @return boolean
  71. *
  72. * @static
  73. */
  74. public static function toBoolean($value, $default=true) {
  75. if (is_null($value)) {
  76. return $default;
  77. } elseif (is_string($value)) {
  78. $trimmedVal = strtolower(trim($value));
  79. if("1" == $trimmedVal or "true" == $trimmedVal or "yes" == $trimmedVal or "on" == $trimmedVal) {
  80. return true;
  81. } else if ("" == $trimmedVal or "0" == $trimmedVal or "false" == $trimmedVal or "no" == $trimmedVal or "off" == $trimmedVal) {
  82. return false;
  83. }
  84. } elseif (is_bool($value)) {
  85. return $value;
  86. } elseif (is_int($value)) {
  87. return !($value == 0); // true is everything but 0 like in C
  88. }
  89. trigger_error("Could not convert ".var_export($value,1)." to boolean!", E_USER_WARNING);
  90. return $default;
  91. }
  92. /**
  93. * @param string $value
  94. * @param integer $default
  95. * @return integer
  96. * @static
  97. */
  98. public static function toInt($value, $default) {
  99. $value = trim($value);
  100. if(is_numeric($value)) {
  101. return (int)$value;
  102. } else {
  103. return $default;
  104. }
  105. }
  106. /**
  107. * Converts a standard or custom priority level to a Level
  108. * object.
  109. *
  110. * <p> If <var>$value</var> is of form "<b>level#full_file_classname</b>",
  111. * where <i>full_file_classname</i> means the class filename with path
  112. * but without php extension, then the specified class' <i>toLevel()</i> method
  113. * is called to process the specified level string; if no '#'
  114. * character is present, then the default {@link LoggerLevel}
  115. * class is used to process the level value.</p>
  116. *
  117. * <p>As a special case, if the <var>$value</var> parameter is
  118. * equal to the string "NULL", then the value <i>null</i> will
  119. * be returned.</p>
  120. *
  121. * <p>If any error occurs while converting the value to a level,
  122. * the <var>$defaultValue</var> parameter, which may be
  123. * <i>null</i>, is returned.</p>
  124. *
  125. * <p>Case of <var>$value</var> is insignificant for the level level, but is
  126. * significant for the class name part, if present.</p>
  127. *
  128. * @param string $value
  129. * @param LoggerLevel $defaultValue
  130. * @return LoggerLevel a {@link LoggerLevel} or null
  131. * @static
  132. */
  133. public static function toLevel($value, $defaultValue) {
  134. if($value === null) {
  135. return $defaultValue;
  136. }
  137. $hashIndex = strpos($value, '#');
  138. if($hashIndex === false) {
  139. if("NULL" == strtoupper($value)) {
  140. return null;
  141. } else {
  142. // no class name specified : use standard Level class
  143. return LoggerLevel::toLevel($value, $defaultValue);
  144. }
  145. }
  146. $result = $defaultValue;
  147. $clazz = substr($value, ($hashIndex + 1));
  148. $levelName = substr($value, 0, $hashIndex);
  149. // This is degenerate case but you never know.
  150. if("NULL" == strtoupper($levelName)) {
  151. return null;
  152. }
  153. $clazz = basename($clazz);
  154. if(class_exists($clazz)) {
  155. $result = @call_user_func(array($clazz, 'toLevel'), $levelName, $defaultValue);
  156. if(!$result instanceof LoggerLevel) {
  157. $result = $defaultValue;
  158. }
  159. }
  160. return $result;
  161. }
  162. /**
  163. * @param string $value
  164. * @param float $default
  165. * @return float
  166. *
  167. * @static
  168. */
  169. public static function toFileSize($value, $default) {
  170. if($value === null) {
  171. return $default;
  172. }
  173. $s = strtoupper(trim($value));
  174. $multiplier = (float)1;
  175. if(($index = strpos($s, 'KB')) !== false) {
  176. $multiplier = 1024;
  177. $s = substr($s, 0, $index);
  178. } else if(($index = strpos($s, 'MB')) !== false) {
  179. $multiplier = 1024 * 1024;
  180. $s = substr($s, 0, $index);
  181. } else if(($index = strpos($s, 'GB')) !== false) {
  182. $multiplier = 1024 * 1024 * 1024;
  183. $s = substr($s, 0, $index);
  184. }
  185. if(is_numeric($s)) {
  186. return (float)$s * $multiplier;
  187. }
  188. return $default;
  189. }
  190. /**
  191. * Find the value corresponding to <var>$key</var> in
  192. * <var>$props</var>. Then perform variable substitution on the
  193. * found value.
  194. *
  195. * @param string $key
  196. * @param array $props
  197. * @return string
  198. *
  199. * @static
  200. */
  201. public static function findAndSubst($key, $props) {
  202. $value = @$props[$key];
  203. // If coming from the LoggerConfiguratorIni, some options were
  204. // already mangled by parse_ini_file:
  205. //
  206. // not specified => never reaches this code
  207. // ""|off|false|null => string(0) ""
  208. // "1"|on|true => string(1) "1"
  209. // "true" => string(4) "true"
  210. // "false" => string(5) "false"
  211. //
  212. // As the integer 1 and the boolean true are therefore indistinguable
  213. // it's up to the setter how to deal with it, they can not be cast
  214. // into a boolean here. {@see toBoolean}
  215. // Even an empty value has to be given to the setter as it has been
  216. // explicitly set by the user and is different from an option which
  217. // has not been specified and therefore keeps its default value.
  218. //
  219. // if(!empty($value)) {
  220. return LoggerOptionConverter::substVars($value, $props);
  221. // }
  222. }
  223. /**
  224. * Perform variable substitution in string <var>$val</var> from the
  225. * values of keys found with the {@link getSystemProperty()} method.
  226. *
  227. * <p>The variable substitution delimeters are <b>${</b> and <b>}</b>.
  228. *
  229. * <p>For example, if the "MY_CONSTANT" contains "value", then
  230. * the call
  231. * <code>
  232. * $s = LoggerOptionConverter::substituteVars("Value of key is ${MY_CONSTANT}.");
  233. * </code>
  234. * will set the variable <i>$s</i> to "Value of key is value.".</p>
  235. *
  236. * <p>If no value could be found for the specified key, then the
  237. * <var>$props</var> parameter is searched, if the value could not
  238. * be found there, then substitution defaults to the empty string.</p>
  239. *
  240. * <p>For example, if {@link getSystemProperty()} cannot find any value for the key
  241. * "inexistentKey", then the call
  242. * <code>
  243. * $s = LoggerOptionConverter::substVars("Value of inexistentKey is [${inexistentKey}]");
  244. * </code>
  245. * will set <var>$s</var> to "Value of inexistentKey is []".</p>
  246. *
  247. * <p>A warn is thrown if <var>$val</var> contains a start delimeter "${"
  248. * which is not balanced by a stop delimeter "}" and an empty string is returned.</p>
  249. *
  250. * @author Avy Sharell
  251. *
  252. * @param string $val The string on which variable substitution is performed.
  253. * @param array $props
  254. * @return string
  255. *
  256. * @static
  257. */
  258. // TODO: this method doesn't work correctly with key = true, it needs key = "true" which is odd
  259. public static function substVars($val, $props = null) {
  260. $sbuf = '';
  261. $i = 0;
  262. while(true) {
  263. $j = strpos($val, self::DELIM_START, $i);
  264. if($j === false) {
  265. // no more variables
  266. if($i == 0) { // this is a simple string
  267. return $val;
  268. } else { // add the tail string which contails no variables and return the result.
  269. $sbuf .= substr($val, $i);
  270. return $sbuf;
  271. }
  272. } else {
  273. $sbuf .= substr($val, $i, $j-$i);
  274. $k = strpos($val, self::DELIM_STOP, $j);
  275. if($k === false) {
  276. // LoggerOptionConverter::substVars() has no closing brace. Opening brace
  277. return '';
  278. } else {
  279. $j += self::START_LEN;
  280. $key = substr($val, $j, $k - $j);
  281. // first try in System properties
  282. $replacement = LoggerOptionConverter::getSystemProperty($key, null);
  283. // then try props parameter
  284. if($replacement == null and $props !== null) {
  285. $replacement = @$props[$key];
  286. }
  287. if(!empty($replacement)) {
  288. // Do variable substitution on the replacement string
  289. // such that we can solve "Hello ${x2}" as "Hello p1"
  290. // the where the properties are
  291. // x1=p1
  292. // x2=${x1}
  293. $recursiveReplacement = LoggerOptionConverter::substVars($replacement, $props);
  294. $sbuf .= $recursiveReplacement;
  295. }
  296. $i = $k + self::DELIM_STOP_LEN;
  297. }
  298. }
  299. }
  300. }
  301. }