1 | <?php |
||
24 | class Loader implements ILoader |
||
25 | { |
||
26 | /** |
||
27 | * Stores the plugin directories. |
||
28 | * |
||
29 | * @see addDirectory |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $paths = array(); |
||
33 | |||
34 | /** |
||
35 | * Stores the plugins names/paths relationships |
||
36 | * don't edit this on your own, use addDirectory. |
||
37 | * |
||
38 | * @see addDirectory |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $classPath = array(); |
||
42 | |||
43 | /** |
||
44 | * Path where class paths cache files are written. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $cacheDir; |
||
49 | |||
50 | /** |
||
51 | * Path where builtin plugins are stored. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $corePluginDir; |
||
56 | |||
57 | /** |
||
58 | * Loader constructor. |
||
59 | * |
||
60 | * @param $cacheDir |
||
61 | */ |
||
62 | public function __construct($cacheDir) |
||
63 | { |
||
64 | $this->corePluginDir = __DIR__ . DIRECTORY_SEPARATOR . 'Plugins'; |
||
65 | $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
66 | |||
67 | // include class paths or rebuild paths if the cache file isn't there |
||
68 | $cacheFile = $this->cacheDir . 'classpath.cache.d' . Core::RELEASE_TAG . '.php'; |
||
69 | |||
70 | $chachedClassPath = null; |
||
71 | |||
72 | if (file_exists($cacheFile)) { |
||
73 | |||
74 | $chachedClassPath = @unserialize(file_get_contents($cacheFile)); |
||
75 | |||
76 | if (is_array($chachedClassPath)) { |
||
77 | $this->classPath = $chachedClassPath + $this->classPath; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | if (!is_array($chachedClassPath)) { |
||
82 | $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Rebuilds class paths, scans the given directory recursively and saves all paths in the given file. |
||
88 | * |
||
89 | * @param string $path the plugin path to scan |
||
90 | * @param string|boolean $cacheFile the file where to store the plugin paths cache, it will be overwritten |
||
91 | * |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | protected function rebuildClassPathCache($path, $cacheFile) |
||
121 | |||
122 | /** |
||
123 | * Loads a plugin file. |
||
124 | * |
||
125 | * @param string $class the plugin name, without the `Plugin` prefix |
||
126 | * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it |
||
127 | * has just been added, defaults to true |
||
128 | * |
||
129 | * @throws Exception |
||
130 | */ |
||
131 | public function loadPlugin($class, $forceRehash = true) |
||
160 | |||
161 | /** |
||
162 | * Adds a plugin directory, the plugins found in the new plugin directory |
||
163 | * will take precedence over the other directories (including the default |
||
164 | * dwoo plugin directory), you can use this for example to override plugins |
||
165 | * in a specific directory for a specific application while keeping all your |
||
166 | * usual plugins in the same place for all applications. |
||
167 | * TOCOM don't forget that php functions overrides are not rehashed so you |
||
168 | * need to clear the classpath caches by hand when adding those. |
||
169 | * |
||
170 | * @param string $pluginDirectory the plugin path to scan |
||
171 | * |
||
172 | * @throws Exception |
||
173 | */ |
||
174 | public function addDirectory($pluginDirectory) |
||
196 | } |
||
197 |