PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/loader.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 367 lines | 158 code | 32 blank | 177 comment | 23 complexity | a61d33b7e564efe6a92801e278d535a9 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  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. defined('JPATH_PLATFORM') or die;
  9. /**
  10. * Static class to handle loading of libraries.
  11. *
  12. * @package Joomla.Platform
  13. * @since 11.1
  14. */
  15. abstract class JLoader
  16. {
  17. /**
  18. * Container for already imported library paths.
  19. *
  20. * @var array
  21. * @since 11.1
  22. */
  23. protected static $classes = array();
  24. /**
  25. * Container for already imported library paths.
  26. *
  27. * @var array
  28. * @since 11.1
  29. */
  30. protected static $imported = array();
  31. /**
  32. * Container for registered library class prefixes and path lookups.
  33. *
  34. * @var array
  35. * @since 12.1
  36. */
  37. protected static $prefixes = array();
  38. /**
  39. * Method to discover classes of a given type in a given path.
  40. *
  41. * @param string $classPrefix The class name prefix to use for discovery.
  42. * @param string $parentPath Full path to the parent folder for the classes to discover.
  43. * @param boolean $force True to overwrite the autoload path value for the class if it already exists.
  44. * @param boolean $recurse Recurse through all child directories as well as the parent path.
  45. *
  46. * @return void
  47. *
  48. * @since 11.1
  49. */
  50. public static function discover($classPrefix, $parentPath, $force = true, $recurse = false)
  51. {
  52. try
  53. {
  54. if ($recurse)
  55. {
  56. $iterator = new RecursiveIteratorIterator(
  57. new RecursiveDirectoryIterator($parentPath),
  58. RecursiveIteratorIterator::SELF_FIRST
  59. );
  60. }
  61. else
  62. {
  63. $iterator = new DirectoryIterator($parentPath);
  64. }
  65. foreach ($iterator as $file)
  66. {
  67. $fileName = $file->getFilename();
  68. // Only load for php files.
  69. // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
  70. if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
  71. {
  72. // Get the class name and full path for each file.
  73. $class = strtolower($classPrefix . preg_replace('#\.php$#', '', $fileName));
  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. self::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. * Method to get the list of registered classes and their respective file paths for the autoloader.
  89. *
  90. * @return array The array of class => path values for the autoloader.
  91. *
  92. * @since 11.1
  93. */
  94. public static function getClassList()
  95. {
  96. return self::$classes;
  97. }
  98. /**
  99. * Loads a class from specified directories.
  100. *
  101. * @param string $key The class name to look for (dot notation).
  102. * @param string $base Search this directory for the class.
  103. *
  104. * @return boolean True on success.
  105. *
  106. * @since 11.1
  107. */
  108. public static function import($key, $base = null)
  109. {
  110. // Only import the library if not already attempted.
  111. if (!isset(self::$imported[$key]))
  112. {
  113. // Setup some variables.
  114. $success = false;
  115. $parts = explode('.', $key);
  116. $class = array_pop($parts);
  117. $base = (!empty($base)) ? $base : dirname(__FILE__);
  118. $path = str_replace('.', DIRECTORY_SEPARATOR, $key);
  119. // Handle special case for helper classes.
  120. if ($class == 'helper')
  121. {
  122. $class = ucfirst(array_pop($parts)) . ucfirst($class);
  123. }
  124. // Standard class.
  125. else
  126. {
  127. $class = ucfirst($class);
  128. }
  129. // If we are importing a library from the Joomla namespace set the class to autoload.
  130. if (strpos($path, 'joomla') === 0)
  131. {
  132. // Since we are in the Joomla namespace prepend the classname with J.
  133. $class = 'J' . $class;
  134. // Only register the class for autoloading if the file exists.
  135. if (is_file($base . '/' . $path . '.php'))
  136. {
  137. self::$classes[strtolower($class)] = $base . '/' . $path . '.php';
  138. $success = true;
  139. }
  140. }
  141. /*
  142. * If we are not importing a library from the Joomla namespace directly include the
  143. * file since we cannot assert the file/folder naming conventions.
  144. */
  145. else
  146. {
  147. // If the file exists attempt to include it.
  148. if (is_file($base . '/' . $path . '.php'))
  149. {
  150. $success = (bool) include_once $base . '/' . $path . '.php';
  151. }
  152. }
  153. // Add the import key to the memory cache container.
  154. self::$imported[$key] = $success;
  155. }
  156. return self::$imported[$key];
  157. }
  158. /**
  159. * Load the file for a class.
  160. *
  161. * @param string $class The class to be loaded.
  162. *
  163. * @return boolean True on success
  164. *
  165. * @since 11.1
  166. */
  167. public static function load($class)
  168. {
  169. // Sanitize class name.
  170. $class = strtolower($class);
  171. // If the class already exists do nothing.
  172. if (class_exists($class))
  173. {
  174. return true;
  175. }
  176. // If the class is registered include the file.
  177. if (isset(self::$classes[$class]))
  178. {
  179. include_once self::$classes[$class];
  180. return true;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Directly register a class to the autoload list.
  186. *
  187. * @param string $class The class name to register.
  188. * @param string $path Full path to the file that holds the class to register.
  189. * @param boolean $force True to overwrite the autoload path value for the class if it already exists.
  190. *
  191. * @return void
  192. *
  193. * @since 11.1
  194. */
  195. public static function register($class, $path, $force = true)
  196. {
  197. // Sanitize class name.
  198. $class = strtolower($class);
  199. // Only attempt to register the class if the name and file exist.
  200. if (!empty($class) && is_file($path))
  201. {
  202. // Register the class with the autoloader if not already registered or the force flag is set.
  203. if (empty(self::$classes[$class]) || $force)
  204. {
  205. self::$classes[$class] = $path;
  206. }
  207. }
  208. }
  209. /**
  210. * Register a class prefix with lookup path. This will allow developers to register library
  211. * packages with different class prefixes to the system autoloader. More than one lookup path
  212. * may be registered for the same class prefix, but if this method is called with the reset flag
  213. * set to true then any registered lookups for the given prefix will be overwritten with the current
  214. * lookup path.
  215. *
  216. * @param string $prefix The class prefix to register.
  217. * @param string $path Absolute file path to the library root where classes with the given prefix can be found.
  218. * @param boolean $reset True to reset the prefix with only the given lookup path.
  219. *
  220. * @return void
  221. *
  222. * @since 12.1
  223. */
  224. public static function registerPrefix($prefix, $path, $reset = false)
  225. {
  226. // Verify the library path exists.
  227. if (!file_exists($path))
  228. {
  229. throw new RuntimeException('Library path ' . $path . ' cannot be found.', 500);
  230. }
  231. // If the prefix is not yet registered or we have an explicit reset flag then set set the path.
  232. if (!isset(self::$prefixes[$prefix]) || $reset)
  233. {
  234. self::$prefixes[$prefix] = array($path);
  235. }
  236. // Otherwise we want to simply add the path to the prefix.
  237. else
  238. {
  239. self::$prefixes[$prefix][] = $path;
  240. }
  241. }
  242. /**
  243. * Method to setup the autoloaders for the Joomla Platform. Since the SPL autoloaders are
  244. * called in a queue we will add our explicit, class-registration based loader first, then
  245. * fall back on the autoloader based on conventions. This will allow people to register a
  246. * class in a specific location and override platform libraries as was previously possible.
  247. *
  248. * @return void
  249. *
  250. * @since 11.3
  251. */
  252. public static function setup()
  253. {
  254. // Register the base path for Joomla platform libraries.
  255. self::registerPrefix('J', JPATH_PLATFORM . '/joomla');
  256. // Register the autoloader functions.
  257. spl_autoload_register(array('JLoader', 'load'));
  258. spl_autoload_register(array('JLoader', '_autoload'));
  259. }
  260. /**
  261. * Autoload a class based on name.
  262. *
  263. * @param string $class The class to be loaded.
  264. *
  265. * @return void
  266. *
  267. * @since 11.3
  268. */
  269. private static function _autoload($class)
  270. {
  271. foreach (self::$prefixes as $prefix => $lookup)
  272. {
  273. if (strpos($class, $prefix) === 0)
  274. {
  275. return self::_load(substr($class, strlen($prefix)), $lookup);
  276. }
  277. }
  278. }
  279. /**
  280. * Load a class based on name and lookup array.
  281. *
  282. * @param string $class The class to be loaded (wihtout prefix).
  283. * @param array $lookup The array of base paths to use for finding the class file.
  284. *
  285. * @return void
  286. *
  287. * @since 12.1
  288. */
  289. private static function _load($class, $lookup)
  290. {
  291. // Split the class name into parts separated by camelCase.
  292. $parts = preg_split('/(?<=[a-z0-9])(?=[A-Z])/x', $class);
  293. // If there is only one part we want to duplicate that part for generating the path.
  294. $parts = (count($parts) === 1) ? array($parts[0], $parts[0]) : $parts;
  295. foreach ($lookup as $base)
  296. {
  297. // Generate the path based on the class name parts.
  298. $path = $base . '/' . implode('/', array_map('strtolower', $parts)) . '.php';
  299. // Load the file if it exists.
  300. if (file_exists($path))
  301. {
  302. return include $path;
  303. }
  304. }
  305. }
  306. }
  307. /**
  308. * Global application exit.
  309. *
  310. * This function provides a single exit point for the platform.
  311. *
  312. * @param mixed $message Exit code or string. Defaults to zero.
  313. *
  314. * @return void
  315. *
  316. * @codeCoverageIgnore
  317. * @since 11.1
  318. */
  319. function jexit($message = 0)
  320. {
  321. exit($message);
  322. }
  323. /**
  324. * Intelligent file importer.
  325. *
  326. * @param string $path A dot syntax path.
  327. *
  328. * @return boolean True on success.
  329. *
  330. * @since 11.1
  331. */
  332. function jimport($path)
  333. {
  334. return JLoader::import($path);
  335. }