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 <j.boggiano@seld.be>
  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.     protected $corePluginDir;
  49.  
  50.     public function __construct($cacheDir)
  51.     {
  52.         $this->corePluginDir = DWOO_DIRECTORY 'plugins';
  53.         $this->cacheDir = $cacheDir DIRECTORY_SEPARATOR;
  54.  
  55.         // include class paths or rebuild paths if the cache file isn't there
  56.         $foo @file_get_contents($this->cacheDir.'classpath.cache.php');
  57.         if ($foo{
  58.             $this->classPath = unserialize($foo$this->classPath;
  59.         else {
  60.             $this->rebuildClassPathCache($this->corePluginDir$this->cacheDir.'classpath.cache.php');
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * rebuilds class paths, scans the given directory recursively and saves all paths in the given file
  66.      *
  67.      * @param string $path the plugin path to scan
  68.      * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten
  69.      */
  70.     protected function rebuildClassPathCache($path$cacheFile)
  71.     {
  72.         if ($cacheFile!==false{
  73.             $tmp $this->classPath;
  74.             $this->classPath = array();
  75.         }
  76.  
  77.         // iterates over all files/folders
  78.         $list glob($path.DIRECTORY_SEPARATOR.'*');
  79.         if (is_array($list)) {
  80.             foreach ($list as $f{
  81.                 if (is_dir($f)) {
  82.                     $this->rebuildClassPathCache($ffalse);
  83.                 else {
  84.                     $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.')''basename($f'.php'))$f;
  85.                 }
  86.             }
  87.         }
  88.  
  89.         // save in file if it's the first call (not recursed)
  90.         if ($cacheFile!==false{
  91.             if (!file_put_contents($cacheFileserialize($this->classPath))) {
  92.                 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()');
  93.             }
  94.             $this->classPath += $tmp;
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * loads a plugin file
  100.      *
  101.      * @param string $class the plugin name, without the Dwoo_Plugin_ prefix
  102.      * @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
  103.      */
  104.     public function loadPlugin($class$forceRehash true)
  105.     {
  106.         // a new class was added or the include failed so we rebuild the cache
  107.         if (!isset($this->classPath[$class]|| !(include $this->classPath[$class])) {
  108.             if ($forceRehash{
  109.                 $this->rebuildClassPathCache($this->corePluginDir$this->cacheDir . 'classpath.cache.php');
  110.                 foreach ($this->paths as $path=>$file{
  111.                     $this->rebuildClassPathCache($path$file);
  112.                 }
  113.                 if (isset($this->classPath[$class])) {
  114.                     include $this->classPath[$class];
  115.                 else {
  116.                     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);
  117.                 }
  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.         }
  122.     }
  123.  
  124.     /**
  125.      * adds a plugin directory, the plugins found in the new plugin directory
  126.      * will take precedence over the other directories (including the default
  127.      * dwoo plugin directory), you can use this for example to override plugins
  128.      * in a specific directory for a specific application while keeping all your
  129.      * usual plugins in the same place for all applications.
  130.      *
  131.      * TOCOM don't forget that php functions overrides are not rehashed so you
  132.      * need to clear the classpath caches by hand when adding those
  133.      *
  134.      * @param string $pluginDirectory the plugin path to scan
  135.      */
  136.     public function addDirectory($pluginDirectory)
  137.     {
  138.         $pluginDir realpath($pluginDirectory);
  139.         if (!$pluginDir{
  140.             throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory);
  141.         }
  142.         $cacheFile $this->cacheDir . 'classpath-'.substr(strtr($pluginDir'/\\''--')strlen($pluginDir80 ? -80 0).'.d'.Dwoo::RELEASE_TAG.'.php';
  143.         $this->paths[$pluginDir$cacheFile;
  144.         $foo @file_get_contents($cacheFile);
  145.         if ($foo{
  146.             $this->classPath = unserialize($foo$this->classPath;
  147.         else {
  148.             $this->rebuildClassPathCache($pluginDir$cacheFile);
  149.         }
  150.     }
  151. }

Documentation generated on Sun, 07 Sep 2008 23:57:53 +0200 by phpDocumentor 1.4.0