source: branches/0.4/web/addons/job_monarch/dwoo/Dwoo/Loader.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 5.3 KB
Line 
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 */
17class 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($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
49
50                // include class paths or rebuild paths if the cache file isn't there
51                $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php';
52                if (file_exists($cacheFile)) {
53                        $classpath = file_get_contents($cacheFile);
54                        $this->classPath = unserialize($classpath) + $this->classPath;
55                } else {
56                        $this->rebuildClassPathCache($this->corePluginDir, $cacheFile);
57                }
58        }
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                if ($cacheFile!==false) {
69                        $tmp = $this->classPath;
70                        $this->classPath = array();
71                }
72
73                // iterates over all files/folders
74                $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*');
75                if (is_array($list)) {
76                        foreach ($list as $f) {
77                                if (is_dir($f)) {
78                                        $this->rebuildClassPathCache($f, false);
79                                } else {
80                                        $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f;
81                                }
82                        }
83                }
84
85                // save in file if it's the first call (not recursed)
86                if ($cacheFile!==false) {
87                        if (!file_put_contents($cacheFile, serialize($this->classPath))) {
88                                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                        $this->classPath += $tmp;
91                }
92        }
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                if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) {
104                        if ($forceRehash) {
105                                $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php');
106                                foreach ($this->paths as $path=>$file) {
107                                        $this->rebuildClassPathCache($path, $file);
108                                }
109                                if (isset($this->classPath[$class])) {
110                                        include $this->classPath[$class];
111                                } else {
112                                        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                        } else {
115                                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                }
118        }
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                $pluginDir = realpath($pluginDirectory);
135                if (!$pluginDir) {
136                        throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory);
137                }
138                $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo_Core::RELEASE_TAG.'.php';
139                $this->paths[$pluginDir] = $cacheFile;
140                if (file_exists($cacheFile)) {
141                        $classpath = file_get_contents($cacheFile);
142                        $this->classPath = unserialize($classpath) + $this->classPath;
143                } else {
144                        $this->rebuildClassPathCache($pluginDir, $cacheFile);
145                }
146        }
147}
Note: See TracBrowser for help on using the repository browser.