[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/log4php/helpers/ -> LoggerOptionConverter.php (source)

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


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7