Skip to content

Commit

Permalink
Add PSR4 support, not just the deprecated message
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Jan 21, 2017
1 parent de215ea commit dd0219c
Showing 1 changed file with 71 additions and 9 deletions.
80 changes: 71 additions & 9 deletions libraries/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class JLoader
* @var array
* @since 12.3
*/
protected static $namespaces = array();
protected static $namespaces = array('psr0' => array(), 'psr4' => array());

/**
* Holds a reference for all deprecated aliases (mainly for use by a logging platform).
Expand Down Expand Up @@ -152,11 +152,13 @@ public static function getDeprecatedAliases()
/**
* Method to get the list of registered namespaces.
*
* @param string $type Defines the type of namespace, can be prs0 or psr4.
*
* @return array The array of namespace => path values for the autoloader.
*
* @since 12.3
*/
public static function getNamespaces()
public static function getNamespaces($type = 'psr0')
{
return self::$namespaces;
}
Expand Down Expand Up @@ -386,14 +388,15 @@ public static function registerAlias($alias, $original, $version = false)
* @param string $path A case sensitive absolute file path to the library root where classes of the given namespace can be found.
* @param boolean $reset True to reset the namespace with only the given lookup path.
* @param boolean $prepend If true, push the path to the beginning of the namespace lookup paths array.
* @param string $type Defines the type of namespace, can be prs0 or psr4.
*
* @return void
*
* @throws RuntimeException
*
* @since 12.3
*/
public static function registerNamespace($namespace, $path, $reset = false, $prepend = false)
public static function registerNamespace($namespace, $path, $reset = false, $prepend = false, $type = 'psr0')
{
// Verify the library path exists.
if (!file_exists($path))
Expand All @@ -404,21 +407,21 @@ public static function registerNamespace($namespace, $path, $reset = false, $pre
}

// If the namespace is not yet registered or we have an explicit reset flag then set the path.
if (!isset(self::$namespaces[$namespace]) || $reset)
if (!isset(self::$namespaces[$namespace][$type]) || $reset)
{
self::$namespaces[$namespace] = array($path);
self::$namespaces[$namespace][$type] = array($path);
}

// Otherwise we want to simply add the path to the namespace.
else
{
if ($prepend)
{
array_unshift(self::$namespaces[$namespace], $path);
array_unshift(self::$namespaces[$namespace][$type], $path);
}
else
{
self::$namespaces[$namespace][] = $path;
self::$namespaces[$namespace][$type][] = $path;
}
}
}
Expand Down Expand Up @@ -459,10 +462,69 @@ public static function setup($enablePsr = true, $enablePrefixes = true, $enableC
{
// Register the PSR-0 based autoloader.
spl_autoload_register(array('JLoader', 'loadByPsr0'));
spl_autoload_register(array('JLoader', 'loadByPsr4'));
spl_autoload_register(array('JLoader', 'loadByAlias'));
}
}

/**
* Method to autoload classes that are namespaced to the PSR-4 standard.
*
* @param string $class The fully qualified class name to autoload.
*
* @return boolean True on success, false otherwise.
*
* @since 13.1
*/
public static function loadByPsr4($class)
{
// Remove the root backslash if present.
if ($class[0] == '\\')
{
$class = substr($class, 1);
}

// Find the location of the last NS separator.
$pos = strrpos($class, '\\');

// If one is found, we're dealing with a NS'd class.
if ($pos !== false)
{
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
$className = substr($class, $pos + 1);
}
// If not, no need to parse path.
else
{
$classPath = null;
$className = $class;
}

$classPath .= $className . '.php';

// Loop through registered namespaces until we find a match.
foreach (self::$namespaces['psr4'] as $ns => $paths)
{
$nsPath = trim(str_replace('\\', DIRECTORY_SEPARATOR, $ns), DIRECTORY_SEPARATOR);
if (strpos($class, $ns) === 0)
{
// Loop through paths registered to this namespace until we find a match.
foreach ($paths as $path)
{
$classFilePath = $path . DIRECTORY_SEPARATOR . str_replace($nsPath, '', $classPath);

// We check for class_exists to handle case-sensitive file systems
if (file_exists($classFilePath) && !class_exists($class, false))
{
return (bool) include_once $classFilePath;
}
}
}
}

return false;
}

/**
* Method to autoload classes that are namespaced to the PSR-0 standard.
*
Expand All @@ -472,7 +534,7 @@ public static function setup($enablePsr = true, $enablePrefixes = true, $enableC
*
* @since 13.1
*
* @deprecated 4.0 this method will be renamed to loadByPsr4 in favour to support PSR-4
* @deprecated 4.0 this method will be removed
*/
public static function loadByPsr0($class)
{
Expand Down Expand Up @@ -501,7 +563,7 @@ public static function loadByPsr0($class)
$classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

// Loop through registered namespaces until we find a match.
foreach (self::$namespaces as $ns => $paths)
foreach (self::$namespaces['psr0'] as $ns => $paths)
{
if (strpos($class, $ns) === 0)
{
Expand Down

0 comments on commit dd0219c

Please sign in to comment.