Skip to content

Commit

Permalink
JLoader alis load class support
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Mar 22, 2017
1 parent 55cc1a9 commit d322ad5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
5 changes: 0 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
"forum":"https://forum.joomla.org/",
"docs":"https://docs.joomla.org"
},
"autoload": {
"psr-4": {
"Joomla\\": "libraries/src/Joomla"
}
},
"require": {
"php": ">=5.3.10",
"joomla/application": "~1.5",
Expand Down
72 changes: 42 additions & 30 deletions libraries/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static function import($key, $base = null)
public static function load($class)
{
// Sanitize class name.
$class = strtolower($class);
$key = strtolower($class);

// If the class already exists do nothing.
if (class_exists($class, false))
Expand All @@ -255,9 +255,21 @@ public static function load($class)
}

// If the class is registered include the file.
if (isset(self::$classes[$class]))
if (isset(self::$classes[$key]))
{
include_once self::$classes[$class];
include_once self::$classes[$key];

// If the class doesn't exists, we probably have a class alias available
if (!class_exists($class, false))
{
// Search the alias class, first none namespaced and then namespaced
$original = array_search($class, self::$classAliases) ? : array_search('\\' . $class, self::$classAliases);

if ($original)
{
class_alias($original, $class);
}
}

return true;
}
Expand All @@ -278,6 +290,12 @@ public static function load($class)
*/
public static function register($class, $path, $force = true)
{
// When an alias exists, register it as well
if (key_exists($class, self::$classAliases))
{
self::register(self::stripFirstBackslash(self::$classAliases[$class]), $path, $force);
}

// Sanitize class name.
$class = strtolower($class);

Expand Down Expand Up @@ -357,11 +375,7 @@ public static function registerAlias($alias, $original, $version = false)
{
self::$classAliases[$alias] = $original;

// Remove the root backslash if present.
if ($original[0] == '\\')
{
$original = substr($original, 1);
}
$original = self::stripFirstBackslash($original);

if (!isset(self::$classAliasesInverse[$original]))
{
Expand Down Expand Up @@ -469,7 +483,7 @@ public static function setup($enablePsr = true, $enablePrefixes = true, $enableC

if ($enablePsr)
{
// Register the PSR-0 based autoloader.
// Register the PSR based autoloader.
spl_autoload_register(array('JLoader', 'loadByPsr0'));
spl_autoload_register(array('JLoader', 'loadByPsr4'));
spl_autoload_register(array('JLoader', 'loadByAlias'));
Expand All @@ -487,11 +501,7 @@ public static function setup($enablePsr = true, $enablePrefixes = true, $enableC
*/
public static function loadByPsr4($class)
{
// Remove the root backslash if present.
if ($class[0] == '\\')
{
$class = substr($class, 1);
}
$class = self::stripFirstBackslash($class);

// Find the location of the last NS separator.
$pos = strrpos($class, '\\');
Expand Down Expand Up @@ -548,11 +558,7 @@ public static function loadByPsr4($class)
*/
public static function loadByPsr0($class)
{
// Remove the root backslash if present.
if ($class[0] == '\\')
{
$class = substr($class, 1);
}
$class = self::stripFirstBackslash($class);

// Find the location of the last NS separator.
$pos = strrpos($class, '\\');
Expand Down Expand Up @@ -605,11 +611,7 @@ public static function loadByPsr0($class)
*/
public static function loadByAlias($class)
{
// Remove the root backslash if present.
if ($class[0] == '\\')
{
$class = substr($class, 1);
}
$class = self::stripFirstBackslash($class);

if (isset(self::$classAliases[$class]))
{
Expand All @@ -636,11 +638,7 @@ class_alias(self::$classAliases[$class], $class);
*/
public static function applyAliasFor($class)
{
// Remove the root backslash if present.
if ($class[0] == '\\')
{
$class = substr($class, 1);
}
$class = self::stripFirstBackslash($class);

if (isset(self::$classAliasesInverse[$class]))
{
Expand Down Expand Up @@ -678,7 +676,7 @@ public static function _autoload($class)
/**
* Load a class based on name and lookup array.
*
* @param string $class The class to be loaded (wihtout prefix).
* @param string $class The class to be loaded (without prefix).
* @param array $lookup The array of base paths to use for finding the class file.
*
* @return boolean True if the class was loaded, false otherwise.
Expand Down Expand Up @@ -720,6 +718,20 @@ private static function _load($class, $lookup)

return false;
}

/**
* Strips the first backslash from the given class if present.
*
* @param string $class The class to strip the first prefix from.
*
* @return string The striped class name.
*
* @since __DEPLOY_VERSION__
*/
private static function stripFirstBackslash($class)
{
return $class && $class[0] == '\\' ? substr($class, 1) : $class;
}
}

// Check if jexit is defined first (our unit tests mock this)
Expand Down

0 comments on commit d322ad5

Please sign in to comment.