| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Platform 4 * 5 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 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 */ 25 protected static $imported = array(); 26 27 /** 28 * Container for already imported library paths. 29 * 30 * @var array 31 * @since 11.1 32 */ 33 protected static $classes = array(); 34 35 /** 36 * Method to discover classes of a given type in a given path. 37 * 38 * @param string $classPrefix The class name prefix to use for discovery. 39 * @param string $parentPath Full path to the parent folder for the classes to discover. 40 * @param boolean $force True to overwrite the autoload path value for the class if it already exists. 41 * @param boolean $recurse Recurse through all child directories as well as the parent path. 42 * 43 * @return void 44 * 45 * @since 11.1 46 */ 47 public static function discover($classPrefix, $parentPath, $force = true, $recurse = false) 48 { 49 try 50 { 51 if ($recurse) 52 { 53 $iterator = new RecursiveIteratorIterator( 54 new RecursiveDirectoryIterator($parentPath), 55 RecursiveIteratorIterator::SELF_FIRST 56 ); 57 } 58 else 59 { 60 $iterator = new DirectoryIterator($parentPath); 61 } 62 63 foreach ($iterator as $file) 64 { 65 $fileName = $file->getFilename(); 66 67 // Only load for php files. 68 // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6 69 if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php') 70 { 71 // Get the class name and full path for each file. 72 $class = strtolower($classPrefix . preg_replace('#\.php$#', '', $fileName)); 73 74 // Register the class with the autoloader if not already registered or the force flag is set. 75 if (empty(self::$classes[$class]) || $force) 76 { 77 JLoader::register($class, $file->getPath() . '/' . $fileName); 78 } 79 } 80 } 81 } 82 catch (UnexpectedValueException $e) 83 { 84 // Exception will be thrown if the path is not a directory. Ignore it. 85 } 86 } 87 88 /** 89 * Method to get the list of registered classes and their respective file paths for the autoloader. 90 * 91 * @return array The array of class => path values for the autoloader. 92 * 93 * @since 11.1 94 */ 95 public static function getClassList() 96 { 97 return self::$classes; 98 } 99 100 /** 101 * Loads a class from specified directories. 102 * 103 * @param string $key The class name to look for (dot notation). 104 * @param string $base Search this directory for the class. 105 * 106 * @return boolean True on success. 107 * 108 * @since 11.1 109 */ 110 public static function import($key, $base = null) 111 { 112 // Only import the library if not already attempted. 113 if (!isset(self::$imported[$key])) 114 { 115 // Setup some variables. 116 $success = false; 117 $parts = explode('.', $key); 118 $class = array_pop($parts); 119 $base = (!empty($base)) ? $base : dirname(__FILE__); 120 $path = str_replace('.', DIRECTORY_SEPARATOR, $key); 121 122 // Handle special case for helper classes. 123 if ($class == 'helper') 124 { 125 $class = ucfirst(array_pop($parts)) . ucfirst($class); 126 } 127 // Standard class. 128 else 129 { 130 $class = ucfirst($class); 131 } 132 133 // If we are importing a library from the Joomla namespace set the class to autoload. 134 if (strpos($path, 'joomla') === 0) 135 { 136 137 // Since we are in the Joomla namespace prepend the classname with J. 138 $class = 'J' . $class; 139 140 // Only register the class for autoloading if the file exists. 141 if (is_file($base . '/' . $path . '.php')) 142 { 143 self::$classes[strtolower($class)] = $base . '/' . $path . '.php'; 144 $success = true; 145 } 146 } 147 /* 148 * If we are not importing a library from the Joomla namespace directly include the 149 * file since we cannot assert the file/folder naming conventions. 150 */ 151 else 152 { 153 154 // If the file exists attempt to include it. 155 if (is_file($base . '/' . $path . '.php')) 156 { 157 $success = (bool) include_once $base . '/' . $path . '.php'; 158 } 159 } 160 161 // Add the import key to the memory cache container. 162 self::$imported[$key] = $success; 163 } 164 165 return self::$imported[$key]; 166 } 167 168 /** 169 * Load the file for a class. 170 * 171 * @param string $class The class to be loaded. 172 * 173 * @return boolean True on success 174 * 175 * @since 11.1 176 */ 177 public static function load($class) 178 { 179 // Sanitize class name. 180 $class = strtolower($class); 181 182 // If the class already exists do nothing. 183 if (class_exists($class)) 184 { 185 return true; 186 } 187 188 // If the class is registered include the file. 189 if (isset(self::$classes[$class])) 190 { 191 include_once self::$classes[$class]; 192 return true; 193 } 194 195 return false; 196 } 197 198 /** 199 * Directly register a class to the autoload list. 200 * 201 * @param string $class The class name to register. 202 * @param string $path Full path to the file that holds the class to register. 203 * @param boolean $force True to overwrite the autoload path value for the class if it already exists. 204 * 205 * @return void 206 * 207 * @since 11.1 208 */ 209 public static function register($class, $path, $force = true) 210 { 211 // Sanitize class name. 212 $class = strtolower($class); 213 214 // Only attempt to register the class if the name and file exist. 215 if (!empty($class) && is_file($path)) 216 { 217 // Register the class with the autoloader if not already registered or the force flag is set. 218 if (empty(self::$classes[$class]) || $force) 219 { 220 self::$classes[$class] = $path; 221 } 222 } 223 } 224 225 /** 226 * Method to setup the autoloaders for the Joomla Platform. Since the SPL autoloaders are 227 * called in a queue we will add our explicit, class-registration based loader first, then 228 * fall back on the autoloader based on conventions. This will allow people to register a 229 * class in a specific location and override platform libraries as was previously possible. 230 * 231 * @return void 232 * 233 * @since 11.3 234 */ 235 public static function setup() 236 { 237 spl_autoload_register(array('JLoader', 'load')); 238 spl_autoload_register(array('JLoader', '_autoload')); 239 } 240 241 /** 242 * Autoload a Joomla Platform class based on name. 243 * 244 * @param string $class The class to be loaded. 245 * 246 * @return void 247 * 248 * @since 11.3 249 */ 250 private static function _autoload($class) 251 { 252 // Only attempt to autoload if the class does not already exist. 253 if (class_exists($class)) 254 { 255 return; 256 } 257 258 // Only attempt autoloading if we are dealing with a Joomla Platform class. 259 if ($class[0] == 'J') 260 { 261 // Split the class name (without the J) into parts separated by camelCase. 262 $parts = preg_split('/(?<=[a-z])(?=[A-Z])/x', substr($class, 1)); 263 264 // If there is only one part we want to duplicate that part for generating the path. 265 $parts = (count($parts) === 1) ? array($parts[0], $parts[0]) : $parts; 266 267 // Generate the path based on the class name parts. 268 $path = JPATH_PLATFORM . '/joomla/' . implode('/', array_map('strtolower', $parts)) . '.php'; 269 270 // Load the file if it exists. 271 if (file_exists($path)) 272 { 273 include $path; 274 } 275 } 276 } 277 } 278 279 /** 280 * Global application exit. 281 * 282 * This function provides a single exit point for the platform. 283 * 284 * @param mixed $message Exit code or string. Defaults to zero. 285 * 286 * @return void 287 * 288 * @codeCoverageIgnore 289 * @since 11.1 290 */ 291 function jexit($message = 0) 292 { 293 exit($message); 294 } 295 296 /** 297 * Intelligent file importer. 298 * 299 * @param string $path A dot syntax path. 300 * 301 * @return boolean True on success. 302 * 303 * @since 11.1 304 */ 305 function jimport($path) 306 { 307 return JLoader::import($path); 308 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |