library/log4php/LoggerReflectionUtils.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     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 /**
       
    22  * Provides methods for reflective use on php objects
       
    23  * @package log4php
       
    24  */
       
    25 class LoggerReflectionUtils {
       
    26     /** the target object */
       
    27     private $obj;
       
    28 
       
    29     /**
       
    30      * Create a new LoggerReflectionUtils for the specified Object.
       
    31      * This is done in prepartion for invoking {@link setProperty()}
       
    32      * one or more times.
       
    33      * @param object &$obj the object for which to set properties
       
    34      */
       
    35     public function __construct($obj) {
       
    36         $this->obj = $obj;
       
    37     }
       
    38 
       
    39     /**
       
    40      * Set the properties of an object passed as a parameter in one
       
    41      * go. The <code>properties</code> are parsed relative to a
       
    42      * <code>prefix</code>.
       
    43      *
       
    44      * @param object $obj The object to configure.
       
    45      * @param array $properties An array containing keys and values.
       
    46      * @param string $prefix Only keys having the specified prefix will be set.
       
    47      */
       
    48     // TODO: check, if this is really useful
       
    49     public static function setPropertiesByObject($obj, $properties, $prefix) {
       
    50         $pSetter = new LoggerReflectionUtils($obj);
       
    51         return $pSetter->setProperties($properties, $prefix);
       
    52     }
       
    53 
       
    54 
       
    55     /**
       
    56      * Set the properites for the object that match the
       
    57      * <code>prefix</code> passed as parameter.
       
    58      *
       
    59      * Example:
       
    60      *
       
    61      * $arr['xxxname'] = 'Joe';
       
    62      * $arr['xxxmale'] = true;
       
    63      * and prefix xxx causes setName and setMale.
       
    64      *
       
    65      * @param array $properties An array containing keys and values.
       
    66      * @param string $prefix Only keys having the specified prefix will be set.
       
    67      */
       
    68     public function setProperties($properties, $prefix) {
       
    69         $len = strlen($prefix);
       
    70         reset($properties);
       
    71         while (list($key,) = each($properties)) {
       
    72             if (strpos($key, $prefix) === 0) {
       
    73                 if (strpos($key, '.', ($len + 1)) > 0) {
       
    74                     continue;
       
    75                 }
       
    76                 $value = $properties[$key];
       
    77                 $key = substr($key, $len);
       
    78                 if ($key == 'layout' and ($this->obj instanceof LoggerAppender)) {
       
    79                     continue;
       
    80                 }
       
    81                 $this->setProperty($key, $value);
       
    82             }
       
    83         }
       
    84         $this->activate();
       
    85     }
       
    86 
       
    87     /**
       
    88      * Set a property on this PropertySetter's Object. If successful, this
       
    89      * method will invoke a setter method on the underlying Object. The
       
    90      * setter is the one for the specified property name and the value is
       
    91      * determined partly from the setter argument type and partly from the
       
    92      * value specified in the call to this method.
       
    93      *
       
    94      * <p>If the setter expects a String no conversion is necessary.
       
    95      * If it expects an int, then an attempt is made to convert 'value'
       
    96      * to an int using new Integer(value). If the setter expects a boolean,
       
    97      * the conversion is by new Boolean(value).
       
    98      *
       
    99      * @param string $name name of the property
       
   100      * @param string $value String value of the property
       
   101      */
       
   102     public function setProperty($name, $value) {
       
   103         if ($value === null) {
       
   104             return;
       
   105         }
       
   106 
       
   107         $method = "set" . ucfirst($name);
       
   108 
       
   109         if (!method_exists($this->obj, $method)) {
       
   110             throw new Exception("Error setting log4php property $name to $value: no method $method in class " . get_class($this->obj) . "!");
       
   111         } else {
       
   112             return call_user_func(array($this->obj, $method), $value);
       
   113         }
       
   114     }
       
   115 
       
   116     public function activate() {
       
   117         if (method_exists($this->obj, 'activateoptions')) {
       
   118             return call_user_func(array($this->obj, 'activateoptions'));
       
   119         }
       
   120     }
       
   121 
       
   122     /**
       
   123      * Creates an instances from the given class name.
       
   124      *
       
   125      * @param string $classname
       
   126      * @return an object from the class with the given classname
       
   127      */
       
   128     public static function createObject($class) {
       
   129         if (!empty($class)) {
       
   130             return new $class();
       
   131         }
       
   132         return null;
       
   133     }
       
   134 
       
   135     /**
       
   136      * @param object $object
       
   137      * @param string $name
       
   138      * @param mixed $value
       
   139      */
       
   140     public static function setter($object, $name, $value) {
       
   141         if (empty($name)) {
       
   142             return false;
       
   143         }
       
   144         $methodName = 'set' . ucfirst($name);
       
   145         if (method_exists($object, $methodName)) {
       
   146             return call_user_func(array($object, $methodName), $value);
       
   147         } else {
       
   148             return false;
       
   149         }
       
   150     }
       
   151 
       
   152 }