Web Commons
Current file: /home/antoine/zavaee/Web Commons/WebCommons/utils.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
  
   
66.67%66.67%
66.67% 2 / 3
89.29%89.29%
89.29% 25 / 28
 
Functions
  
   
66.67%66.67%
66.67% 2 / 3
100.00%100.00%
100.00% 0 / 0
 function array_get_default(&$array, $key, $default)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3
 function load_extensible_ini_file($filename)
0.00%0.00%
0.00% 0 / 1
83.33%83.33%
83.33% 15 / 18
 function array_merge_two_dimensional($array1, &$array2)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 7 / 7


       1                 : <?php                                                                         
       2                 : /**                                                                           
       3                 :  * This file offers various utility functions and classes.                    
       4                 :  *                                                                             
       5                 :  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>              
       6                 :  * @license http://www.opensource.org/licenses/mit-license.php                
       7                 :  * @version 0.1dev                                                            
       8                 :  */                                                                           
       9                 :                                                                               
      10                 : /**                                                                           
      11                 :  * Returns the value held by an $array at a given $key, or $default           
      12                 :  * if this $key does not exist.                                               
      13                 :  * @param array $array                                                        
      14                 :  * @param mixed $key                                                          
      15                 :  * @param mixed $default                                                      
      16                 :  * @return mixed                                                              
      17                 :  * @since 0.1                                                                 
      18                 :  */                                                                           
      19                 : function array_get_default(&$array, $key, $default) {                         
      20              15 :     if (array_key_exists($key, $array))                                       
      21              15 :         return $array[$key];                                                  
      22                 :     else                                                                      
      23              11 :         return $default;                                                      
      24                 : }                                                                             
      25                 :                                                                               
      26                 : /**                                                                           
      27                 :  * Loads an ini file using {@link parse_ini_file()}, merging its data         
      28                 :  * with a parent ini file if "extends" is defined at the root of the ini file.
      29                 :  * "extends" value is the relative or absolute path to the extended file.     
      30                 :  * "extends" will not appear in resulting array.                              
      31                 :  *                                                                             
      32                 :  * Returns False on failure (file missing, unreadable or unparseable)         
      33                 :  *                                                                             
      34                 :  * @param string $filename                                                    
      35                 :  * @return array                                                              
      36                 :  * @since 0.1                                                                 
      37                 :  */                                                                           
      38                 : function load_extensible_ini_file($filename) {                                
      39               6 :     if (!is_file($filename)) {                                                
      40               1 :         return False;                                                         
      41                 :     }                                                                         
      42                 :                                                                                   
      43               6 :     $data = parse_ini_file($filename, True);                                  
      44                 :                                                                                   
      45               6 :     if ($data === False)                                                      
      46               6 :         return False;                                                         
      47                 :                                                                                   
      48               6 :     $extends = array_get_default($data, 'extends', Null);                     
      49               6 :     unset($data['extends']);                                                  
      50                 :                                                                                   
      51               6 :     if ($extends) {                                                           
      52               6 :         if ($extends[0] === '/') {                                            
      53                 :             /* absolute path */                                               
      54               0 :             $path = $extends;                                                 
      55               0 :         }                                                                     
      56                 :         else {                                                                
      57                 :             /* relative path */                                               
      58               6 :             $dir = dirname($filename);                                        
      59               6 :             $path = realpath($dir . DIRECTORY_SEPARATOR . $extends);          
      60                 :         }                                                                     
      61                 :                                                                                       
      62               6 :         $extendedData = load_extensible_ini_file($path);                      
      63                 :                                                                                       
      64               6 :         if ($extendedData !== False) {                                        
      65               6 :             return array_merge_two_dimensional($extendedData, $data);         
      66                 :         }                                                                     
      67                 :         else {                                                                
      68               0 :             return False;                                                     
      69                 :         }                                                                     
      70                 :     }                                                                         
      71                 :     else {                                                                    
      72               6 :         return $data;                                                         
      73                 :     }                                                                         
      74                 : }                                                                             
      75                 :                                                                               
      76                 : /**                                                                           
      77                 :  * Merges two two-dimensional arrays.                                         
      78                 :  * If keys overlap, array1 will be overriden by array2.                       
      79                 :  * Both arrays are untouched.                                                 
      80                 :  * @param array $array1                                                       
      81                 :  * @param array &$array2                                                      
      82                 :  * @return array                                                              
      83                 :  * @since 0.1                                                                 
      84                 :  */                                                                           
      85                 : function array_merge_two_dimensional($array1, &$array2) {                     
      86               7 :     foreach ($array2 as $key=>$subarray) {                                    
      87               7 :         if (array_key_exists($key, $array1)) {                                
      88               7 :             $array1[$key] = array_merge($array1[$key], $subarray);            
      89               7 :         }                                                                     
      90                 :         else {                                                                
      91               1 :             $array1[$key] = $subarray;                                        
      92                 :         }                                                                     
      93               7 :     }                                                                         
      94                 :                                                                                   
      95               7 :     return $array1;                                                           
      96                 : }                                                                             

Generated by PHPUnit 3.4.5 and Xdebug 2.1.0 at Fri Jul 16 0:48:38 CEST 2010.