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

Documentation generated on Sat, 28 Jun 2008 01:38:29 +0200 by phpDocumentor 1.4.0