Dwoo - Unit Tests Report
Current file: /dwoo/lib/Dwoo/Loader.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 4 / 4
96.30%96.30%
96.30% 52 / 54
 
Dwoo_Loader
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 4 / 4
96.30%96.30%
96.30% 52 / 54
 public function __construct($cacheDir)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 9 / 9
 protected function rebuildClassPathCache($path, $cacheFile)
100.00%100.00%
100.00% 1 / 1
94.74%94.74%
94.74% 18 / 19
 public function loadPlugin($class, $forceRehash = true)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 14 / 14
 public function addDirectory($pluginDirectory)
100.00%100.00%
100.00% 1 / 1
90.91%90.91%
90.91% 10 / 11



       1                 : <?php                                                                                                                                                                                                                                                                                                                                                              
       2                 :                                                                                                                                                                                                                                                                                                                                                                    
       3                 : /**                                                                                                                                                                                                                                                                                                                                                                
       4                 :  * handles plugin loading and caching of plugins names/paths relationships                                                                                                                                                                                                                                                                                         
       5                 :  *                                                                                                                                                                                                                                                                                                                                                                 
       6                 :  * This software is provided 'as-is', without any express or implied warranty.                                                                                                                                                                                                                                                                                     
       7                 :  * In no event will the authors be held liable for any damages arising from the use of this software.                                                                                                                                                                                                                                                              
       8                 :  *                                                                                                                                                                                                                                                                                                                                                                 
       9                 :  * @author     Jordi Boggiano <j.boggiano@seld.be>                                                                                                                                                                                                                                                                                                                 
      10                 :  * @copyright  Copyright (c) 2008, Jordi Boggiano                                                                                                                                                                                                                                                                                                                  
      11                 :  * @license    http://dwoo.org/LICENSE   Modified BSD License                                                                                                                                                                                                                                                                                                      
      12                 :  * @link       http://dwoo.org/                                                                                                                                                                                                                                                                                                                                    
      13                 :  * @version    1.1.0                                                                                                                                                                                                                                                                                                                                               
      14                 :  * @date       2009-07-18                                                                                                                                                                                                                                                                                                                                          
      15                 :  * @package    Dwoo                                                                                                                                                                                                                                                                                                                                                
      16                 :  */                                                                                                                                                                                                                                                                                                                                                                
      17               1 : class Dwoo_Loader implements Dwoo_ILoader                                                                                                                                                                                                                                                                                                                          
      18                 : {                                                                                                                                                                                                                                                                                                                                                                  
      19                 :     /**                                                                                                                                                                                                                                                                                                                                                            
      20                 :      * stores the plugin directories                                                                                                                                                                                                                                                                                                                               
      21                 :      *                                                                                                                                                                                                                                                                                                                                                             
      22                 :      * @see addDirectory                                                                                                                                                                                                                                                                                                                                           
      23                 :      * @var array                                                                                                                                                                                                                                                                                                                                                  
      24                 :      */                                                                                                                                                                                                                                                                                                                                                            
      25                 :     protected $paths = array();                                                                                                                                                                                                                                                                                                                                    
      26                 :                                                                                                                                                                                                                                                                                                                                                                    
      27                 :     /**                                                                                                                                                                                                                                                                                                                                                            
      28                 :      * stores the plugins names/paths relationships                                                                                                                                                                                                                                                                                                                
      29                 :      * don't edit this on your own, use addDirectory                                                                                                                                                                                                                                                                                                               
      30                 :      *                                                                                                                                                                                                                                                                                                                                                             
      31                 :      * @see addDirectory                                                                                                                                                                                                                                                                                                                                           
      32                 :      * @var array                                                                                                                                                                                                                                                                                                                                                  
      33                 :      */                                                                                                                                                                                                                                                                                                                                                            
      34                 :     protected $classPath = array();                                                                                                                                                                                                                                                                                                                                
      35                 :                                                                                                                                                                                                                                                                                                                                                                    
      36                 :     /**                                                                                                                                                                                                                                                                                                                                                            
      37                 :      * path where class paths cache files are written                                                                                                                                                                                                                                                                                                              
      38                 :      *                                                                                                                                                                                                                                                                                                                                                             
      39                 :      * @var string                                                                                                                                                                                                                                                                                                                                                 
      40                 :      */                                                                                                                                                                                                                                                                                                                                                            
      41                 :     protected $cacheDir;                                                                                                                                                                                                                                                                                                                                           
      42                 :                                                                                                                                                                                                                                                                                                                                                                    
      43                 :     protected $corePluginDir;                                                                                                                                                                                                                                                                                                                                      
      44                 :                                                                                                                                                                                                                                                                                                                                                                    
      45                 :     public function __construct($cacheDir)                                                                                                                                                                                                                                                                                                                         
      46                 :     {                                                                                                                                                                                                                                                                                                                                                              
      47              69 :         $this->corePluginDir = DWOO_DIRECTORY . 'plugins';                                                                                                                                                                                                                                                                                                         
      48              69 :         $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;                                                                                                                                                                                                                                                                             
      49                 :                                                                                                                                                                                                                                                                                                                                                                    
      50                 :         // include class paths or rebuild paths if the cache file isn't there                                                                                                                                                                                                                                                                                      
      51              69 :         $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php';                                                                                                                                                                                                                                                                                 
      52              69 :         if (file_exists($cacheFile)) {                                                                                                                                                                                                                                                                                                                             
      53              67 :             $classpath = file_get_contents($cacheFile);                                                                                                                                                                                                                                                                                                            
      54              67 :             $this->classPath = unserialize($classpath) + $this->classPath;                                                                                                                                                                                                                                                                                         
      55              67 :         } else {                                                                                                                                                                                                                                                                                                                                                   
      56               2 :             $this->rebuildClassPathCache($this->corePluginDir, $cacheFile);                                                                                                                                                                                                                                                                                        
      57                 :         }                                                                                                                                                                                                                                                                                                                                                          
      58              69 :     }                                                                                                                                                                                                                                                                                                                                                              
      59                 :                                                                                                                                                                                                                                                                                                                                                                    
      60                 :     /**                                                                                                                                                                                                                                                                                                                                                            
      61                 :      * rebuilds class paths, scans the given directory recursively and saves all paths in the given file                                                                                                                                                                                                                                                           
      62                 :      *                                                                                                                                                                                                                                                                                                                                                             
      63                 :      * @param string $path the plugin path to scan                                                                                                                                                                                                                                                                                                                 
      64                 :      * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten                                                                                                                                                                                                                                                             
      65                 :      */                                                                                                                                                                                                                                                                                                                                                            
      66                 :     protected function rebuildClassPathCache($path, $cacheFile)                                                                                                                                                                                                                                                                                                    
      67                 :     {                                                                                                                                                                                                                                                                                                                                                              
      68               9 :         if ($cacheFile!==false) {                                                                                                                                                                                                                                                                                                                                  
      69               9 :             $tmp = $this->classPath;                                                                                                                                                                                                                                                                                                                               
      70               9 :             $this->classPath = array();                                                                                                                                                                                                                                                                                                                            
      71               9 :         }                                                                                                                                                                                                                                                                                                                                                          
      72                 :                                                                                                                                                                                                                                                                                                                                                                    
      73                 :         // iterates over all files/folders                                                                                                                                                                                                                                                                                                                         
      74               9 :         $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*');                                                                                                                                                                                                                                                                               
      75               9 :         if (is_array($list)) {                                                                                                                                                                                                                                                                                                                                     
      76               9 :             foreach ($list as $f) {                                                                                                                                                                                                                                                                                                                                
      77               9 :                 if (is_dir($f)) {                                                                                                                                                                                                                                                                                                                                  
      78               8 :                     $this->rebuildClassPathCache($f, false);                                                                                                                                                                                                                                                                                                       
      79               8 :                 } else {                                                                                                                                                                                                                                                                                                                                           
      80               9 :                     $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f;                                                                                                                                       
      81                 :                 }                                                                                                                                                                                                                                                                                                                                                  
      82               9 :             }                                                                                                                                                                                                                                                                                                                                                      
      83               9 :         }                                                                                                                                                                                                                                                                                                                                                          
      84                 :                                                                                                                                                                                                                                                                                                                                                                    
      85                 :         // save in file if it's the first call (not recursed)                                                                                                                                                                                                                                                                                                      
      86               9 :         if ($cacheFile!==false) {                                                                                                                                                                                                                                                                                                                                  
      87               9 :             if (!file_put_contents($cacheFile, serialize($this->classPath))) {                                                                                                                                                                                                                                                                                     
      88               0 :                 throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()');
      89                 :             }                                                                                                                                                                                                                                                                                                                                                      
      90               9 :             $this->classPath += $tmp;                                                                                                                                                                                                                                                                                                                              
      91               9 :         }                                                                                                                                                                                                                                                                                                                                                          
      92               9 :     }                                                                                                                                                                                                                                                                                                                                                              
      93                 :                                                                                                                                                                                                                                                                                                                                                                    
      94                 :     /**                                                                                                                                                                                                                                                                                                                                                            
      95                 :      * loads a plugin file                                                                                                                                                                                                                                                                                                                                         
      96                 :      *                                                                                                                                                                                                                                                                                                                                                             
      97                 :      * @param string $class the plugin name, without the Dwoo_Plugin_ prefix                                                                                                                                                                                                                                                                                       
      98                 :      * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true                                                                                                                                                                                                        
      99                 :      */                                                                                                                                                                                                                                                                                                                                                            
     100                 :     public function loadPlugin($class, $forceRehash = true)                                                                                                                                                                                                                                                                                                        
     101                 :     {                                                                                                                                                                                                                                                                                                                                                              
     102                 :         // a new class was added or the include failed so we rebuild the cache                                                                                                                                                                                                                                                                                     
     103              68 :         if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) {                                                                                                                                                                                                                                                                             
     104              14 :             if ($forceRehash) {                                                                                                                                                                                                                                                                                                                                    
     105               6 :                 $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php');                                                                                                                                                                                                                                
     106               6 :                 foreach ($this->paths as $path=>$file) {                                                                                                                                                                                                                                                                                                           
     107               1 :                     $this->rebuildClassPathCache($path, $file);                                                                                                                                                                                                                                                                                                    
     108               6 :                 }                                                                                                                                                                                                                                                                                                                                                  
     109               6 :                 if (isset($this->classPath[$class])) {                                                                                                                                                                                                                                                                                                             
     110               1 :                     include $this->classPath[$class];                                                                                                                                                                                                                                                                                                              
     111               1 :                 } else {                                                                                                                                                                                                                                                                                                                                           
     112               5 :                     throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE);                                                                                                                                                                                                
     113                 :                 }                                                                                                                                                                                                                                                                                                                                                  
     114               1 :             } else {                                                                                                                                                                                                                                                                                                                                               
     115               8 :                 throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE);                                                                                                                                                                                                    
     116                 :             }                                                                                                                                                                                                                                                                                                                                                      
     117               1 :         }                                                                                                                                                                                                                                                                                                                                                          
     118              56 :     }                                                                                                                                                                                                                                                                                                                                                              
     119                 :                                                                                                                                                                                                                                                                                                                                                                    
     120                 :     /**                                                                                                                                                                                                                                                                                                                                                            
     121                 :      * adds a plugin directory, the plugins found in the new plugin directory                                                                                                                                                                                                                                                                                      
     122                 :      * will take precedence over the other directories (including the default                                                                                                                                                                                                                                                                                      
     123                 :      * dwoo plugin directory), you can use this for example to override plugins                                                                                                                                                                                                                                                                                    
     124                 :      * in a specific directory for a specific application while keeping all your                                                                                                                                                                                                                                                                                   
     125                 :      * usual plugins in the same place for all applications.                                                                                                                                                                                                                                                                                                       
     126                 :      *                                                                                                                                                                                                                                                                                                                                                             
     127                 :      * TOCOM don't forget that php functions overrides are not rehashed so you                                                                                                                                                                                                                                                                                     
     128                 :      * need to clear the classpath caches by hand when adding those                                                                                                                                                                                                                                                                                                
     129                 :      *                                                                                                                                                                                                                                                                                                                                                             
     130                 :      * @param string $pluginDirectory the plugin path to scan                                                                                                                                                                                                                                                                                                      
     131                 :      */                                                                                                                                                                                                                                                                                                                                                            
     132                 :     public function addDirectory($pluginDirectory)                                                                                                                                                                                                                                                                                                                 
     133                 :     {                                                                                                                                                                                                                                                                                                                                                              
     134               2 :         $pluginDir = realpath($pluginDirectory);                                                                                                                                                                                                                                                                                                                   
     135               2 :         if (!$pluginDir) {                                                                                                                                                                                                                                                                                                                                         
     136               0 :             throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory);                                                                                                                                                                                                                                                    
     137                 :         }                                                                                                                                                                                                                                                                                                                                                          
     138               2 :         $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo::RELEASE_TAG.'.php';                                                                                                                                                                                     
     139               2 :         $this->paths[$pluginDir] = $cacheFile;                                                                                                                                                                                                                                                                                                                     
     140               2 :         if (file_exists($cacheFile)) {                                                                                                                                                                                                                                                                                                                             
     141               1 :             $classpath = file_get_contents($cacheFile);                                                                                                                                                                                                                                                                                                            
     142               1 :             $this->classPath = unserialize($classpath) + $this->classPath;                                                                                                                                                                                                                                                                                         
     143               1 :         } else {                                                                                                                                                                                                                                                                                                                                                   
     144               1 :             $this->rebuildClassPathCache($pluginDir, $cacheFile);                                                                                                                                                                                                                                                                                                  
     145                 :         }                                                                                                                                                                                                                                                                                                                                                          
     146               2 :     }                                                                                                                                                                                                                                                                                                                                                              
     147                 : }                                                                                                                                                                                                                                                                                                                                                                  


Generated by PHPUnit @package_version@ and Xdebug 2.1.0beta2-dev using PHP 5.3.0 at Sun Feb 7 16:30:52 UTC 2010.