9 defined(
'JPATH_PLATFORM') or die;
25 protected static $classes = array();
33 protected static $imported = array();
41 protected static $prefixes = array();
49 protected static $classAliases = array();
57 protected static $namespaces = array();
71 public static function discover($classPrefix, $parentPath, $force =
true, $recurse =
false)
77 $iterator =
new RecursiveIteratorIterator(
78 new RecursiveDirectoryIterator($parentPath),
79 RecursiveIteratorIterator::SELF_FIRST
84 $iterator =
new DirectoryIterator($parentPath);
87 foreach ($iterator as $file)
89 $fileName = $file->getFilename();
93 if ($file->isFile() && substr($fileName, strrpos($fileName,
'.') + 1) ==
'php')
96 $class = strtolower($classPrefix . preg_replace(
'#\.php$#',
'', $fileName));
99 if (empty(self::$classes[$class]) || $force)
101 self::register($class, $file->getPath() .
'/' . $fileName);
106 catch (UnexpectedValueException $e)
119 public static function getClassList()
121 return self::$classes;
131 public static function getNamespaces()
133 return self::$namespaces;
146 public static function import($key, $base = null)
149 if (!isset(self::$imported[$key]))
153 $parts = explode(
'.', $key);
154 $class = array_pop($parts);
155 $base = (!empty($base)) ? $base : __DIR__;
156 $path = str_replace(
'.', DIRECTORY_SEPARATOR, $key);
159 if ($class ==
'helper')
161 $class = ucfirst(array_pop($parts)) . ucfirst($class);
166 $class = ucfirst($class);
170 if (strpos($path,
'joomla') === 0)
173 $class =
'J' . $class;
176 if (is_file($base .
'/' . $path .
'.php'))
178 self::$classes[strtolower($class)] = $base .
'/' . $path .
'.php';
189 if (is_file($base .
'/' . $path .
'.php'))
191 $success = (bool) include_once $base .
'/' . $path .
'.php';
196 self::$imported[$key] = $success;
199 return self::$imported[$key];
211 public static function load($class)
214 $class = strtolower($class);
217 if (class_exists($class,
false))
223 if (isset(self::$classes[$class]))
225 include_once self::$classes[$class];
244 public static function register($class, $path, $force =
true)
247 $class = strtolower($class);
250 if (!empty($class) && is_file($path))
253 if (empty(self::$classes[$class]) || $force)
255 self::$classes[$class] = $path;
278 public static function registerPrefix($prefix, $path, $reset =
false, $prepend =
false)
281 if (!file_exists($path))
283 throw new RuntimeException(
'Library path ' . $path .
' cannot be found.', 500);
287 if (!isset(self::$prefixes[$prefix]) || $reset)
289 self::$prefixes[$prefix] = array($path);
296 array_unshift(self::$prefixes[$prefix], $path);
300 self::$prefixes[$prefix][] = $path;
316 public static function registerAlias($alias, $original)
318 if (!isset(self::$classAliases[$alias]))
320 self::$classAliases[$alias] = $original;
342 public static function registerNamespace($namespace, $path, $reset =
false, $prepend =
false)
345 if (!file_exists($path))
347 throw new RuntimeException(
'Library path ' . $path .
' cannot be found.', 500);
351 if (!isset(self::$namespaces[$namespace]) || $reset)
353 self::$namespaces[$namespace] = array($path);
361 array_unshift(self::$namespaces[$namespace], $path);
365 self::$namespaces[$namespace][] = $path;
385 public static function setup($enablePsr =
true, $enablePrefixes =
true, $enableClasses =
true)
390 spl_autoload_register(array(
'JLoader',
'load'));
396 self::registerPrefix(
'J', JPATH_PLATFORM .
'/joomla');
399 spl_autoload_register(array(
'JLoader',
'_autoload'));
405 spl_autoload_register(array(
'JLoader',
'loadByPsr0'));
406 spl_autoload_register(array(
'JLoader',
'loadByAlias'));
419 public static function loadByPsr0($class)
422 if ($class[0] ==
'\\')
424 $class = substr($class, 1);
428 $pos = strrpos($class,
'\\');
433 $classPath = str_replace(
'\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
434 $className = substr($class, $pos + 1);
443 $classPath .= str_replace(
'_', DIRECTORY_SEPARATOR, $className) .
'.php';
446 foreach (self::$namespaces as $ns => $paths)
448 if (strpos($class, $ns) === 0)
451 foreach ($paths as $path)
453 $classFilePath = $path . DIRECTORY_SEPARATOR . $classPath;
456 if (file_exists($classFilePath) && !class_exists($class,
false))
458 return (
bool) include_once $classFilePath;
476 public static function loadByAlias($class)
479 if ($class[0] ==
'\\')
481 $class = substr($class, 1);
484 if (isset(self::$classAliases[$class]))
486 class_alias(self::$classAliases[$class], $class);
499 private static function _autoload($class)
501 foreach (self::$prefixes as $prefix => $lookup)
503 $chr = strlen($prefix) < strlen($class) ? $class[strlen($prefix)] : 0;
505 if (strpos($class, $prefix) === 0 && ($chr === strtoupper($chr)))
507 return self::_load(substr($class, strlen($prefix)), $lookup);
524 private static function _load($class, $lookup)
527 $parts = preg_split(
'/(?<=[a-z0-9])(?=[A-Z])/x', $class);
530 $parts = (count($parts) === 1) ? array($parts[0], $parts[0]) : $parts;
532 foreach ($lookup as $base)
535 $path = $base .
'/' . implode(
'/', array_map(
'strtolower', $parts)) .
'.php';
538 if (file_exists($path))
540 return include $path;