Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 369 lines (336 sloc) 9.716 kb
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
1 <?php
2 /**
608c1da Michael Babker CodeSniff on base libraries files
mbabker authored
3 * @package Joomla.Platform
0a72863 Louis Landry Fixing file headers for both unit test and library files.
LouisLandry authored
4 *
4329ba0 Christophe Demko Tagging 11.4
chdemko authored
5 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
608c1da Michael Babker CodeSniff on base libraries files
mbabker authored
6 * @license GNU General Public License version 2 or later; see LICENSE
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
7 */
8
9 defined('JPATH_PLATFORM') or die;
10
11 /**
12 * Static class to handle loading of libraries.
13 *
14 * @package Joomla.Platform
15 * @since 11.1
16 */
17 abstract class JLoader
18 {
19 /**
20 * Container for already imported library paths.
21 *
22 * @var array
23 * @since 11.1
24 */
bade7c4 Louis Landry Add ability to register multiple lookup paths with the system
LouisLandry authored
25 protected static $classes = array();
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
26
27 /**
28 * Container for already imported library paths.
29 *
30 * @var array
31 * @since 11.1
32 */
bade7c4 Louis Landry Add ability to register multiple lookup paths with the system
LouisLandry authored
33 protected static $imported = array();
34
35 /**
36 * Container for registered library class prefixes and path lookups.
37 *
38 * @var array
39 * @since 12.1
40 */
41 protected static $prefixes = array();
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
42
43 /**
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
44 * Method to discover classes of a given type in a given path.
45 *
46 * @param string $classPrefix The class name prefix to use for discovery.
47 * @param string $parentPath Full path to the parent folder for the classes to discover.
48 * @param boolean $force True to overwrite the autoload path value for the class if it already exists.
49 * @param boolean $recurse Recurse through all child directories as well as the parent path.
50 *
51 * @return void
52 *
53 * @since 11.1
54 */
55 public static function discover($classPrefix, $parentPath, $force = true, $recurse = false)
56 {
57 try
58 {
59 if ($recurse)
60 {
61 $iterator = new RecursiveIteratorIterator(
62 new RecursiveDirectoryIterator($parentPath),
63 RecursiveIteratorIterator::SELF_FIRST
64 );
65 }
66 else
67 {
68 $iterator = new DirectoryIterator($parentPath);
69 }
70
71 foreach ($iterator as $file)
72 {
73 $fileName = $file->getFilename();
74
75 // Only load for php files.
76 // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
77 if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
78 {
79 // Get the class name and full path for each file.
80 $class = strtolower($classPrefix . preg_replace('#\.php$#', '', $fileName));
81
82 // Register the class with the autoloader if not already registered or the force flag is set.
83 if (empty(self::$classes[$class]) || $force)
84 {
2eba831 Rouven Weßling Add a sniff that makes sure to use self:: whenever possible.
realityking authored
85 self::register($class, $file->getPath() . '/' . $fileName);
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
86 }
87 }
88 }
89 }
90 catch (UnexpectedValueException $e)
91 {
92 // Exception will be thrown if the path is not a directory. Ignore it.
93 }
94 }
95
96 /**
97 * Method to get the list of registered classes and their respective file paths for the autoloader.
98 *
99 * @return array The array of class => path values for the autoloader.
100 *
101 * @since 11.1
102 */
103 public static function getClassList()
104 {
105 return self::$classes;
106 }
107
108 /**
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
109 * Loads a class from specified directories.
110 *
608c1da Michael Babker CodeSniff on base libraries files
mbabker authored
111 * @param string $key The class name to look for (dot notation).
112 * @param string $base Search this directory for the class.
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
113 *
3e7bbce elinw More doc bloc work in form fields.
elinw authored
114 * @return boolean True on success.
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
115 *
116 * @since 11.1
117 */
118 public static function import($key, $base = null)
119 {
120 // Only import the library if not already attempted.
1809579 Andrew Eddie Removed underscores from protected static methods in JLoader per code
eddieajau authored
121 if (!isset(self::$imported[$key]))
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
122 {
123 // Setup some variables.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
124 $success = false;
125 $parts = explode('.', $key);
126 $class = array_pop($parts);
133d981 Rouven Weßling Replace all occurences of dirname(__FILE__) with __DIR__.
realityking authored
127 $base = (!empty($base)) ? $base : __DIR__;
2fb19f5 Rouven Weßling Remove the remaining occurences of DS except for its definition and JPar...
realityking authored
128 $path = str_replace('.', DIRECTORY_SEPARATOR, $key);
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
129
130 // Handle special case for helper classes.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
131 if ($class == 'helper')
132 {
133 $class = ucfirst(array_pop($parts)) . ucfirst($class);
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
134 }
135 // Standard class.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
136 else
137 {
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
138 $class = ucfirst($class);
139 }
140
141 // If we are importing a library from the Joomla namespace set the class to autoload.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
142 if (strpos($path, 'joomla') === 0)
143 {
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
144 // Since we are in the Joomla namespace prepend the classname with J.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
145 $class = 'J' . $class;
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
146
147 // Only register the class for autoloading if the file exists.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
148 if (is_file($base . '/' . $path . '.php'))
149 {
1809579 Andrew Eddie Removed underscores from protected static methods in JLoader per code
eddieajau authored
150 self::$classes[strtolower($class)] = $base . '/' . $path . '.php';
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
151 $success = true;
152 }
153 }
154 /*
155 * If we are not importing a library from the Joomla namespace directly include the
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
156 * file since we cannot assert the file/folder naming conventions.
157 */
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
158 else
159 {
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
160 // If the file exists attempt to include it.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
161 if (is_file($base . '/' . $path . '.php'))
162 {
163 $success = (bool) include_once $base . '/' . $path . '.php';
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
164 }
165 }
166
167 // Add the import key to the memory cache container.
1809579 Andrew Eddie Removed underscores from protected static methods in JLoader per code
eddieajau authored
168 self::$imported[$key] = $success;
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
169 }
170
1809579 Andrew Eddie Removed underscores from protected static methods in JLoader per code
eddieajau authored
171 return self::$imported[$key];
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
172 }
173
174 /**
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
175 * Load the file for a class.
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
176 *
608c1da Michael Babker CodeSniff on base libraries files
mbabker authored
177 * @param string $class The class to be loaded.
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
178 *
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
179 * @return boolean True on success
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
180 *
181 * @since 11.1
182 */
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
183 public static function load($class)
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
184 {
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
185 // Sanitize class name.
186 $class = strtolower($class);
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
187
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
188 // If the class already exists do nothing.
189 if (class_exists($class))
190 {
191 return true;
6d20abb Andrew Eddie Converted JLoader::discover to use PHP's directory iterator.
eddieajau authored
192 }
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
193
194 // If the class is registered include the file.
195 if (isset(self::$classes[$class]))
6d20abb Andrew Eddie Converted JLoader::discover to use PHP's directory iterator.
eddieajau authored
196 {
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
197 include_once self::$classes[$class];
198 return true;
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
199 }
200
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
201 return false;
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
202 }
203
204 /**
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
205 * Directly register a class to the autoload list.
206 *
9a59bfb Louis Landry Fix up some doc block issues.
LouisLandry authored
207 * @param string $class The class name to register.
208 * @param string $path Full path to the file that holds the class to register.
209 * @param boolean $force True to overwrite the autoload path value for the class if it already exists.
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
210 *
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
211 * @return void
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
212 *
213 * @since 11.1
214 */
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
215 public static function register($class, $path, $force = true)
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
216 {
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
217 // Sanitize class name.
218 $class = strtolower($class);
219
220 // Only attempt to register the class if the name and file exist.
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
221 if (!empty($class) && is_file($path))
222 {
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
223 // Register the class with the autoloader if not already registered or the force flag is set.
1809579 Andrew Eddie Removed underscores from protected static methods in JLoader per code
eddieajau authored
224 if (empty(self::$classes[$class]) || $force)
78e6bc0 Andrew Eddie Code cleaning.
eddieajau authored
225 {
1809579 Andrew Eddie Removed underscores from protected static methods in JLoader per code
eddieajau authored
226 self::$classes[$class] = $path;
8dd6a3c Louis Landry Modified JLoader to add a discover method as well as a minor modificatio...
LouisLandry authored
227 }
228 }
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
229 }
230
231 /**
bade7c4 Louis Landry Add ability to register multiple lookup paths with the system
LouisLandry authored
232 * Register a class prefix with lookup path. This will allow developers to register library
233 * packages with different class prefixes to the system autoloader. More than one lookup path
234 * may be registered for the same class prefix, but if this method is called with the reset flag
235 * set to true then any registered lookups for the given prefix will be overwritten with the current
236 * lookup path.
237 *
5ae9fe6 Louis Landry Style fix.
LouisLandry authored
238 * @param string $prefix The class prefix to register.
239 * @param string $path Absolute file path to the library root where classes with the given prefix can be found.
240 * @param boolean $reset True to reset the prefix with only the given lookup path.
bade7c4 Louis Landry Add ability to register multiple lookup paths with the system
LouisLandry authored
241 *
242 * @return void
243 *
244 * @since 12.1
245 */
246 public static function registerPrefix($prefix, $path, $reset = false)
247 {
248 // Verify the library path exists.
249 if (!file_exists($path))
250 {
251 throw new RuntimeException('Library path ' . $path . ' cannot be found.', 500);
252 }
253
254 // If the prefix is not yet registered or we have an explicit reset flag then set set the path.
255 if (!isset(self::$prefixes[$prefix]) || $reset)
256 {
257 self::$prefixes[$prefix] = array($path);
258 }
259 // Otherwise we want to simply add the path to the prefix.
260 else
261 {
262 self::$prefixes[$prefix][] = $path;
263 }
264 }
265
266 /**
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
267 * Method to setup the autoloaders for the Joomla Platform. Since the SPL autoloaders are
268 * called in a queue we will add our explicit, class-registration based loader first, then
269 * fall back on the autoloader based on conventions. This will allow people to register a
270 * class in a specific location and override platform libraries as was previously possible.
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
271 *
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
272 * @return void
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
273 *
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
274 * @since 11.3
cb8c4d8 Louis Landry Moving files around.
LouisLandry authored
275 */
1a7caa4 Louis Landry Some minor adjustments to the Joomla Loader class and import file.
LouisLandry authored
276 public static function setup()
cb8c4d8 Louis Landry