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

Documentation generated on Sat, 18 Jul 2009 21:05:12 +0200 by phpDocumentor 1.4.0