~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Zend/Loader/PluginLoader.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Loader
 
17
 * @subpackage PluginLoader
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
/** Zend_Loader_PluginLoader_Interface */
 
23
require_once 'Zend/Loader/PluginLoader/Interface.php';
 
24
 
 
25
/** Zend_Loader */
 
26
require_once 'Zend/Loader.php';
 
27
 
 
28
/**
 
29
 * Generic plugin class loader
 
30
 *
 
31
 * @category   Zend
 
32
 * @package    Zend_Loader
 
33
 * @subpackage PluginLoader
 
34
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
35
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
36
 */
 
37
class Zend_Loader_PluginLoader implements Zend_Loader_PluginLoader_Interface
 
38
{
 
39
    /**
 
40
     * Static registry property
 
41
     *
 
42
     * @var array
 
43
     */
 
44
    static protected $_staticPrefixToPaths = array();
 
45
 
 
46
    /**
 
47
     * Instance registry property
 
48
     *
 
49
     * @var array
 
50
     */
 
51
    protected $_prefixToPaths = array();
 
52
 
 
53
    /**
 
54
     * Statically loaded plugins
 
55
     *
 
56
     * @var array
 
57
     */
 
58
    static protected $_staticLoadedPlugins = array();
 
59
 
 
60
    /**
 
61
     * Instance loaded plugins
 
62
     *
 
63
     * @var array
 
64
     */
 
65
    protected $_loadedPlugins = array();
 
66
 
 
67
    /**
 
68
     * Whether to use a statically named registry for loading plugins
 
69
     *
 
70
     * @var string|null
 
71
     */
 
72
    protected $_useStaticRegistry = null;
 
73
 
 
74
    /**
 
75
     * Constructor
 
76
     *
 
77
     * @param array $prefixToPaths
 
78
     * @param string $staticRegistryName OPTIONAL
 
79
     */
 
80
    public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
 
81
    {
 
82
        if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
 
83
            $this->_useStaticRegistry = $staticRegistryName;
 
84
            self::$_staticPrefixToPaths[$staticRegistryName] = array();
 
85
            self::$_staticLoadedPlugins[$staticRegistryName] = array();
 
86
        }
 
87
 
 
88
        foreach ($prefixToPaths as $prefix => $path) {
 
89
            $this->addPrefixPath($prefix, $path);
 
90
        }
 
91
    }
 
92
 
 
93
    /**
 
94
     * Format prefix for internal use
 
95
     *
 
96
     * @param  string $prefix
 
97
     * @return string
 
98
     */
 
99
    protected function _formatPrefix($prefix)
 
100
    {
 
101
        return rtrim($prefix, '_') . '_';
 
102
    }
 
103
 
 
104
    /**
 
105
     * Add prefixed paths to the registry of paths
 
106
     *
 
107
     * @param string $prefix
 
108
     * @param string $path
 
109
     * @return Zend_Loader_PluginLoader
 
110
     */
 
111
    public function addPrefixPath($prefix, $path)
 
112
    {
 
113
        if (!is_string($prefix) || !is_string($path)) {
 
114
            require_once 'Zend/Loader/PluginLoader/Exception.php';
 
115
            throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
 
116
        }
 
117
 
 
118
        $prefix = $this->_formatPrefix($prefix);
 
119
        $path   = rtrim($path, '/\\') . '/';
 
120
 
 
121
        if ($this->_useStaticRegistry) {
 
122
            self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
 
123
        } else {
 
124
            $this->_prefixToPaths[$prefix][] = $path;
 
125
        }
 
126
        return $this;
 
127
    }
 
128
 
 
129
    /**
 
130
     * Get path stack
 
131
     *
 
132
     * @param  string $prefix
 
133
     * @return false|array False if prefix does not exist, array otherwise
 
134
     */
 
135
    public function getPaths($prefix = null)
 
136
    {
 
137
        if ((null !== $prefix) && is_string($prefix)) {
 
138
            $prefix = $this->_formatPrefix($prefix);
 
139
            if ($this->_useStaticRegistry) {
 
140
                if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
 
141
                    return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
 
142
                }
 
143
 
 
144
                return false;
 
145
            }
 
146
 
 
147
            if (isset($this->_prefixToPaths[$prefix])) {
 
148
                return $this->_prefixToPaths[$prefix];
 
149
            }
 
150
 
 
151
            return false;
 
152
        }
 
153
 
 
154
        if ($this->_useStaticRegistry) {
 
155
            return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
 
156
        }
 
157
 
 
158
        return $this->_prefixToPaths;
 
159
    }
 
160
 
 
161
    /**
 
162
     * Clear path stack
 
163
     *
 
164
     * @param  string $prefix
 
165
     * @return bool False only if $prefix does not exist
 
166
     */
 
167
    public function clearPaths($prefix = null)
 
168
    {
 
169
        if ((null !== $prefix) && is_string($prefix)) {
 
170
            $prefix = $this->_formatPrefix($prefix);
 
171
            if ($this->_useStaticRegistry) {
 
172
                if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
 
173
                    unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
 
174
                    return true;
 
175
                }
 
176
 
 
177
                return false;
 
178
            }
 
179
 
 
180
            if (isset($this->_prefixToPaths[$prefix])) {
 
181
                unset($this->_prefixToPaths[$prefix]);
 
182
                return true;
 
183
            }
 
184
 
 
185
            return false;
 
186
        }
 
187
 
 
188
        if ($this->_useStaticRegistry) {
 
189
            self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
 
190
        } else {
 
191
            $this->_prefixToPaths = array();
 
192
        }
 
193
 
 
194
        return true;
 
195
    }
 
196
 
 
197
    /**
 
198
     * Remove a prefix (or prefixed-path) from the registry
 
199
     *
 
200
     * @param string $prefix
 
201
     * @param string $path OPTIONAL
 
202
     * @return Zend_Loader_PluginLoader
 
203
     */
 
204
    public function removePrefixPath($prefix, $path = null)
 
205
    {
 
206
        $prefix = $this->_formatPrefix($prefix);
 
207
        if ($this->_useStaticRegistry) {
 
208
            $registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
 
209
        } else {
 
210
            $registry =& $this->_prefixToPaths;
 
211
        }
 
212
 
 
213
        if (!isset($registry[$prefix])) {
 
214
            require_once 'Zend/Loader/PluginLoader/Exception.php';
 
215
            throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
 
216
        }
 
217
 
 
218
        if ($path != null) {
 
219
            $pos = array_search($path, $registry[$prefix]);
 
220
            if ($pos === null) {
 
221
                throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
 
222
            }
 
223
            unset($registry[$prefix][$pos]);
 
224
        } else {
 
225
            unset($registry[$prefix]);
 
226
        }
 
227
 
 
228
        return $this;
 
229
    }
 
230
 
 
231
    /**
 
232
     * Normalize plugin name
 
233
     *
 
234
     * @param  string $name
 
235
     * @return string
 
236
     */
 
237
    protected function _formatName($name)
 
238
    {
 
239
        return ucfirst((string) $name);
 
240
    }
 
241
 
 
242
    /**
 
243
     * Whether or not a Plugin by a specific name is loaded
 
244
     *
 
245
     * @param string $name
 
246
     * @return Zend_Loader_PluginLoader
 
247
     */
 
248
    public function isLoaded($name)
 
249
    {
 
250
        $name = $this->_formatName($name);
 
251
        if ($this->_useStaticRegistry) {
 
252
            return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
 
253
        }
 
254
 
 
255
        return isset($this->_loadedPlugins[$name]);
 
256
    }
 
257
 
 
258
    /**
 
259
     * Return full class name for a named plugin
 
260
     *
 
261
     * @param string $name
 
262
     * @return string|false False if class not found, class name otherwise
 
263
     */
 
264
    public function getClassName($name)
 
265
    {
 
266
        $name = $this->_formatName($name);
 
267
        if ($this->_useStaticRegistry &&
 
268
            isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]))
 
269
        {
 
270
            return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
 
271
        } elseif (isset($this->_loadedPlugins[$name])) {
 
272
            return $this->_loadedPlugins[$name];
 
273
        }
 
274
 
 
275
        return false;
 
276
    }
 
277
 
 
278
    /**
 
279
     * Load a plugin via the name provided
 
280
     *
 
281
     * @param  string $name
 
282
     * @return string
 
283
     */
 
284
    public function load($name)
 
285
    {
 
286
        $name = $this->_formatName($name);
 
287
        if ($this->_useStaticRegistry) {
 
288
            $registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
 
289
        } else {
 
290
            $registry = $this->_prefixToPaths;
 
291
        }
 
292
 
 
293
        if ($this->isLoaded($name)) {
 
294
            return $this->getClassName($name);
 
295
        }
 
296
 
 
297
        $found = false;
 
298
 
 
299
        $registry = array_reverse($registry, true);
 
300
        foreach ($registry as $prefix => $paths) {
 
301
            $paths = array_reverse($paths, true);
 
302
            foreach ($paths as $path) {
 
303
 
 
304
                $classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
 
305
                $className = $prefix . $name;
 
306
 
 
307
                if (class_exists($className, false)) {
 
308
                    $found = true;
 
309
                    break 2;
 
310
                }
 
311
 
 
312
                if (Zend_Loader::isReadable($path . $classFile)) {
 
313
                    include_once $path . $classFile;
 
314
 
 
315
                    if (!class_exists($className, false)) {
 
316
                        throw new Zend_Loader_PluginLoader_Exception('File ' . $classFile . ' was loaded but class named ' . $className . ' was not found within it.');
 
317
                    }
 
318
 
 
319
                    $found = true;
 
320
                    break 2;
 
321
                }
 
322
            }
 
323
        }
 
324
 
 
325
        if ($found) {
 
326
            if ($this->_useStaticRegistry) {
 
327
                self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
 
328
            } else {
 
329
                $this->_loadedPlugins[$name] = $className;
 
330
            }
 
331
            return $className;
 
332
        }
 
333
 
 
334
        require_once 'Zend/Loader/PluginLoader/Exception.php';
 
335
        throw new Zend_Loader_PluginLoader_Exception('Plugin by name ' . $name . ' was not found in the registry.');
 
336
    }
 
337
}