LoggerReflectionUtils.php
Current file: /home/ihabunek/apache/log4php/src/main/php/LoggerReflectionUtils.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00% 0 / 1
28.57% 2 / 7 CRAP
67.57% 25 / 37
LoggerReflectionUtils
0.00% 0 / 1
28.57% 2 / 7 29.05
67.57% 25 / 37
 __construct($obj)
100.00% 1 / 1 1
100.00% 2 / 2
 setPropertiesByObject($obj, $properties, $prefix)
100.00% 1 / 1 1
100.00% 2 / 2
 setProperties($properties, $prefix)
0.00% 0 / 1 6.09
86.67% 13 / 15
 setProperty($name, $value)
0.00% 0 / 1 3.33
66.67% 4 / 6
 activate()
0.00% 0 / 1 2.15
66.67% 2 / 3
 createObject($class)
0.00% 0 / 1 2.15
66.67% 2 / 3
 setter($object, $name, $value)
0.00% 0 / 1 12
0.00% 0 / 6


       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               3 :         $this->obj = $obj;                                                                                                               
      37               3 :     }                                                                                                                                    
      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               1 :         $pSetter = new LoggerReflectionUtils($obj);                                                                                      
      51               1 :         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                 :      // TODO: check, if this is really useful                                                                                            
      69                 :     public function setProperties($properties, $prefix) {                                                                                
      70               2 :         $len = strlen($prefix);                                                                                                          
      71               2 :         reset($properties);                                                                                                              
      72               2 :         while(list($key,) = each($properties)) {                                                                                         
      73               2 :             if(strpos($key, $prefix) === 0) {                                                                                            
      74               2 :                 if(strpos($key, '.', ($len + 1)) > 0) {                                                                                  
      75               0 :                     continue;                                                                                                            
      76                 :                 }                                                                                                                        
      77               2 :                 $value = LoggerOptionConverter::findAndSubst($key, $properties);                                                         
      78               2 :                 $key = substr($key, $len);                                                                                               
      79               2 :                 if($key == 'layout' and ($this->obj instanceof LoggerAppender)) {                                                        
      80               0 :                     continue;                                                                                                            
      81                 :                 }                                                                                                                        
      82               2 :                 $this->setProperty($key, $value);                                                                                        
      83               2 :             }                                                                                                                            
      84               2 :         }                                                                                                                                
      85               2 :         $this->activate();                                                                                                               
      86               2 :     }                                                                                                                                    
      87                 :                                                                                                                                          
      88                 :     /**                                                                                                                                  
      89                 :      * Set a property on this PropertySetter's Object. If successful, this                                                               
      90                 :      * method will invoke a setter method on the underlying Object. The                                                                  
      91                 :      * setter is the one for the specified property name and the value is                                                                
      92                 :      * determined partly from the setter argument type and partly from the                                                               
      93                 :      * value specified in the call to this method.                                                                                       
      94                 :      *                                                                                                                                   
      95                 :      * <p>If the setter expects a String no conversion is necessary.                                                                     
      96                 :      * If it expects an int, then an attempt is made to convert 'value'                                                                  
      97                 :      * to an int using new Integer(value). If the setter expects a boolean,                                                              
      98                 :      * the conversion is by new Boolean(value).                                                                                          
      99                 :      *                                                                                                                                   
     100                 :      * @param string $name    name of the property                                                                                       
     101                 :      * @param string $value    String value of the property                                                                              
     102                 :      */                                                                                                                                  
     103                 :     public function setProperty($name, $value) {                                                                                         
     104               3 :         if($value === null) {                                                                                                            
     105               0 :             return;                                                                                                                      
     106                 :         }                                                                                                                                
     107                 :                                                                                                                                          
     108               3 :         $method = "set" . ucfirst($name);                                                                                                
     109                 :                                                                                                                                          
     110               3 :         if(!method_exists($this->obj, $method)) {                                                                                        
     111               0 :             throw new Exception("Error setting log4php property $name to $value: no method $method in class ".get_class($this->obj)."!");
     112                 :         } else {                                                                                                                         
     113               3 :             return call_user_func(array($this->obj, $method), $value);                                                                   
     114                 :         }                                                                                                                                
     115                 :     }                                                                                                                                    
     116                 :                                                                                                                                          
     117                 :     public function activate() {                                                                                                         
     118               2 :         if(method_exists($this->obj, 'activateoptions')) {                                                                               
     119               0 :             return call_user_func(array($this->obj, 'activateoptions'));                                                                 
     120                 :         }                                                                                                                                
     121               2 :     }                                                                                                                                    
     122                 :                                                                                                                                          
     123                 :     /**                                                                                                                                  
     124                 :      * Creates an instances from the given class name.                                                                                   
     125                 :      *                                                                                                                                   
     126                 :      * @param string $classname                                                                                                          
     127                 :      * @return an object from the class with the given classname                                                                         
     128                 :      */                                                                                                                                  
     129                 :     public static function createObject($class) {                                                                                        
     130               8 :         if(!empty($class)) {                                                                                                             
     131               8 :             return new $class();                                                                                                         
     132                 :         }                                                                                                                                
     133               0 :         return null;                                                                                                                     
     134                 :     }                                                                                                                                    
     135                 :                                                                                                                                          
     136                 :     /**                                                                                                                                  
     137                 :      * @param object $object                                                                                                             
     138                 :      * @param string $name                                                                                                               
     139                 :      * @param mixed $value                                                                                                               
     140                 :      */                                                                                                                                  
     141                 :     public static function setter($object, $name, $value) {                                                                              
     142               0 :         if (empty($name)) {                                                                                                              
     143               0 :             return false;                                                                                                                
     144                 :         }                                                                                                                                
     145               0 :         $methodName = 'set'.ucfirst($name);                                                                                              
     146               0 :         if (method_exists($object, $methodName)) {                                                                                       
     147               0 :             return call_user_func(array($object, $methodName), $value);                                                                  
     148                 :         } else {                                                                                                                         
     149               0 :             return false;                                                                                                                
     150                 :         }                                                                                                                                
     151                 :     }                                                                                                                                    
     152                 :                                                                                                                                          
     153                 : }                                                                                                                                        

Generated by PHP_CodeCoverage 1.1.1 using PHP 5.3.3-7+squeeze3 and PHPUnit 3.6.3 at Sat Feb 18 22:32:39 GMT 2012.