Source for file Loader.php

Documentation is available at Loader.php

  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.  * This file is released under the LGPL
  10.  * "GNU Lesser General Public License"
  11.  * More information can be found here:
  12.  * {@link http://www.gnu.org/copyleft/lesser.html}
  13.  *
  14.  * @author     Jordi Boggiano <[email protected]>
  15.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  16.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  17.  * @link       http://dwoo.org/
  18.  * @version    0.9.1
  19.  * @date       2008-05-30
  20.  * @package    Dwoo
  21.  */
  22. class Dwoo_Loader implements Dwoo_ILoader
  23. {
  24.     /**
  25.      * stores the plugin directories
  26.      *
  27.      * @see addDirectory
  28.      * @var array 
  29.      */
  30.     protected $paths = array();
  31.  
  32.     /**
  33.      * stores the plugins names/paths relationships
  34.      * don't edit this on your own, use addDirectory
  35.      *
  36.      * @see addDirectory
  37.      * @var array 
  38.      */
  39.     protected $classPath = array();
  40.  
  41.     /**
  42.      * path where class paths cache files are written
  43.      *
  44.      * @var string 
  45.      */
  46.     protected $cacheDir;
  47.  
  48.     /**
  49.      * legacy/transitional var for BC with old classpath.cache files, do NOT rely on it
  50.      *
  51.      * will be deleted sooner or later
  52.      *
  53.      * TODO remove this and compat code in addDirectory
  54.      */
  55.     public static $classpath array();
  56.  
  57.     public function __construct($cacheDir)
  58.     {
  59.         $this->cacheDir = $cacheDir DIRECTORY_SEPARATOR;
  60.  
  61.         // include class paths or rebuild paths if the cache file isn't there
  62.         if ((file_exists($this->cacheDir.'classpath.cache.php'&& include $this->cacheDir.'classpath.cache.php'== false{
  63.             $this->rebuildClassPathCache(DWOO_DIRECTORY.'plugins'$this->cacheDir.'classpath.cache.php');
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * rebuilds class paths, scans the given directory recursively and saves all paths in the given file
  69.      *
  70.      * @param string $path the plugin path to scan
  71.      * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten
  72.      */
  73.     protected function rebuildClassPathCache($path$cacheFile)
  74.     {
  75.         if ($cacheFile!==false{
  76.             $tmp $this->classPath;
  77.             $this->classPath = array();
  78.         }
  79.  
  80.         // iterates over all files/folders
  81.         $list glob($path.DIRECTORY_SEPARATOR.'*');
  82.         if (is_array($list)) {
  83.             foreach ($list as $f{
  84.                 if (is_dir($f)) {
  85.                     $this->rebuildClassPathCache($ffalse);
  86.                 else {
  87.                     $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.')''basename($f'.php'))$f;
  88.                 }
  89.             }
  90.         }
  91.  
  92.         // save in file if it's the first call (not recursed)
  93.         if ($cacheFile!==false{
  94.             if (!file_put_contents($cacheFile'<?php $this->classPath = '.var_export($this->classPathtrue).' + $this->classPath;')) {
  95.                 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()');
  96.             }
  97.             $this->classPath += $tmp;
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * loads a plugin file
  103.      *
  104.      * @param string $class the plugin name, without the Dwoo_Plugin_ prefix
  105.      * @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
  106.      */
  107.     public function loadPlugin($class$forceRehash true)
  108.     {
  109.         // a new class was added or the include failed so we rebuild the cache
  110.         if (!isset($this->classPath[$class]|| !include $this->classPath[$class]{
  111.             if ($forceRehash{
  112.                 $this->rebuildClassPathCache(DWOO_DIRECTORY 'plugins'$this->cacheDir . 'classpath.cache.php');
  113.                 foreach ($this->paths as $path=>$file{
  114.                     $this->rebuildClassPathCache($path$file);
  115.                 }
  116.                 if (isset($this->classPath[$class])) {
  117.                     include $this->classPath[$class];
  118.                 else {
  119.                     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);
  120.                 }
  121.             else {
  122.                 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);
  123.             }
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * adds a plugin directory, the plugins found in the new plugin directory
  129.      * will take precedence over the other directories (including the default
  130.      * dwoo plugin directory), you can use this for example to override plugins
  131.      * in a specific directory for a specific application while keeping all your
  132.      * usual plugins in the same place for all applications.
  133.      *
  134.      * TOCOM don't forget that php functions overrides are not rehashed so you
  135.      * need to clear the classpath caches by hand when adding those
  136.      *
  137.      * @param string $pluginDir the plugin path to scan
  138.      */
  139.     public function addDirectory($pluginDir)
  140.     {
  141.         $cacheFile $this->cacheDir . 'classpath-'.substr(strtr($pluginDir':/\\.''----')strlen($pluginDir80 ? -80 0).'.cache.php';
  142.         $this->paths[$pluginDir$cacheFile;
  143.         if (file_exists($cacheFile)) {
  144.             include $cacheFile;
  145.             // BC code, will be removed
  146.             if (!empty(Dwoo_Loader::$classpath)) {
  147.                 $this->classPath = Dwoo_Loader::$classpath $this->classPath;
  148.                 Dwoo_Loader::$classpath array();
  149.                 $this->rebuildClassPathCache($pluginDir$cacheFile);
  150.             }
  151.             // end
  152.         else {
  153.             $this->rebuildClassPathCache($pluginDir$cacheFile);
  154.         }
  155.     }
  156. }

Documentation generated on Sun, 03 Aug 2008 15:12:40 +0200 by phpDocumentor 1.4.0