Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

Commit

Permalink
# [joomla#28599] Backport the new JLoader from the platform 12.1. Th…
Browse files Browse the repository at this point in the history
…anks

Rouven
  • Loading branch information
realityking authored and infograf768 committed Jun 3, 2012
1 parent 90a22e4 commit 772e227
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 85 deletions.
1 change: 1 addition & 0 deletions administrator/components/com_admin/script.php
Expand Up @@ -317,6 +317,7 @@ public function deleteUnexistingFiles()
'/installation/sql/mysql/joomla_update_170to171.sql',
'/installation/sql/mysql/joomla_update_172to173.sql',
'/installation/sql/mysql/joomla_update_17ga.sql',
'/libraries/cms/cmsloader.php',
'/libraries/joomla/application/applicationexception.php',
'/libraries/joomla/client/http.php',
'/libraries/joomla/filter/filterinput.php',
Expand Down
1 change: 1 addition & 0 deletions installation/CHANGELOG
Expand Up @@ -35,6 +35,7 @@ $ -> Language fix or change
^ [#28609] Update joomlaupdate @SInCE tags missed in 61f5ee8. Thanks Rouven
# [#28285] E_NOTICE about array to string conversion in debug.php with PHP 5.4. Thanks Rouven
# [#28560] Backport some files completely from the current platform. Thanks Rouven
# [#28599] Backport the new JLoader from the platform 12.1. Thanks Rouven

02-June-2012 Jean-Marie Simonet
# [#28375] Remove blacklist code from FinderIndexerHelper. Thanks Michael
Expand Down
14 changes: 9 additions & 5 deletions libraries/cms.php
Expand Up @@ -13,19 +13,23 @@
define('JPATH_PLATFORM', dirname(__FILE__));
}

// Import the cms loader if necessary.
if (!class_exists('JCmsLoader')) {
require_once JPATH_PLATFORM.'/cms/cmsloader.php';
// Import the library loader if necessary.
if (!class_exists('JLoader'))
{
require_once JPATH_PLATFORM . '/loader.php';
}

// Setup the autoloader.
JCmsLoader::setup();
class_exists('JLoader') or die;

// Register the library base path for CMS libraries.
JLoader::registerPrefix('J', JPATH_PLATFORM . '/cms');

// Define the Joomla version if not already defined.
if (!defined('JVERSION')) {
$jversion = new JVersion;
define('JVERSION', $jversion->getShortVersion());
}

// Register the location of renamed classes so they can be autoloaded
// The old name are considered deprecated and this should be removed in 3.0
JLoader::register('JRule', JPATH_PLATFORM . '/joomla/access/rule.php');
Expand Down
62 changes: 0 additions & 62 deletions libraries/cms/cmsloader.php

This file was deleted.

95 changes: 77 additions & 18 deletions libraries/loader.php
Expand Up @@ -22,15 +22,23 @@ abstract class JLoader
* @var array
* @since 11.1
*/
protected static $imported = array();
protected static $classes = array();

/**
* Container for already imported library paths.
*
* @var array
* @since 11.1
*/
protected static $classes = array();
protected static $imported = array();

/**
* Container for registered library class prefixes and path lookups.
*
* @var array
* @since 12.1
*/
protected static $prefixes = array();

/**
* Method to discover classes of a given type in a given path.
Expand Down Expand Up @@ -74,7 +82,7 @@ public static function discover($classPrefix, $parentPath, $force = true, $recur
// Register the class with the autoloader if not already registered or the force flag is set.
if (empty(self::$classes[$class]) || $force)
{
JLoader::register($class, $file->getPath() . '/' . $fileName);
self::register($class, $file->getPath() . '/' . $fileName);
}
}
}
Expand Down Expand Up @@ -133,7 +141,6 @@ public static function import($key, $base = null)
// If we are importing a library from the Joomla namespace set the class to autoload.
if (strpos($path, 'joomla') === 0)
{

// Since we are in the Joomla namespace prepend the classname with J.
$class = 'J' . $class;

Expand All @@ -150,7 +157,6 @@ public static function import($key, $base = null)
*/
else
{

// If the file exists attempt to include it.
if (is_file($base . '/' . $path . '.php'))
{
Expand Down Expand Up @@ -222,6 +228,41 @@ public static function register($class, $path, $force = true)
}
}

/**
* Register a class prefix with lookup path. This will allow developers to register library
* packages with different class prefixes to the system autoloader. More than one lookup path
* may be registered for the same class prefix, but if this method is called with the reset flag
* set to true then any registered lookups for the given prefix will be overwritten with the current
* lookup path.
*
* @param string $prefix The class prefix to register.
* @param string $path Absolute file path to the library root where classes with the given prefix can be found.
* @param boolean $reset True to reset the prefix with only the given lookup path.
*
* @return void
*
* @since 12.1
*/
public static function registerPrefix($prefix, $path, $reset = false)
{
// Verify the library path exists.
if (!file_exists($path))
{
throw new RuntimeException('Library path ' . $path . ' cannot be found.', 500);
}

// If the prefix is not yet registered or we have an explicit reset flag then set set the path.
if (!isset(self::$prefixes[$prefix]) || $reset)
{
self::$prefixes[$prefix] = array($path);
}
// Otherwise we want to simply add the path to the prefix.
else
{
self::$prefixes[$prefix][] = $path;
}
}

/**
* Method to setup the autoloaders for the Joomla Platform. Since the SPL autoloaders are
* called in a queue we will add our explicit, class-registration based loader first, then
Expand All @@ -234,12 +275,16 @@ public static function register($class, $path, $force = true)
*/
public static function setup()
{
// Register the base path for Joomla platform libraries.
self::registerPrefix('J', JPATH_PLATFORM . '/joomla');

// Register the autoloader functions.
spl_autoload_register(array('JLoader', 'load'));
spl_autoload_register(array('JLoader', '_autoload'));
}

/**
* Autoload a Joomla Platform class based on name.
* Autoload a class based on name.
*
* @param string $class The class to be loaded.
*
Expand All @@ -249,28 +294,42 @@ public static function setup()
*/
private static function _autoload($class)
{
// Only attempt to autoload if the class does not already exist.
if (class_exists($class))
foreach (self::$prefixes as $prefix => $lookup)
{
return;
if (strpos($class, $prefix) === 0)
{
return self::_load(substr($class, strlen($prefix)), $lookup);
}
}
}

// Only attempt autoloading if we are dealing with a Joomla Platform class.
if ($class[0] == 'J')
{
// Split the class name (without the J) into parts separated by camelCase.
$parts = preg_split('/(?<=[a-z])(?=[A-Z])/x', substr($class, 1));
/**
* Load a class based on name and lookup array.
*
* @param string $class The class to be loaded (wihtout prefix).
* @param array $lookup The array of base paths to use for finding the class file.
*
* @return void
*
* @since 12.1
*/
private static function _load($class, $lookup)
{
// Split the class name into parts separated by camelCase.
$parts = preg_split('/(?<=[a-z0-9])(?=[A-Z])/x', $class);

// If there is only one part we want to duplicate that part for generating the path.
$parts = (count($parts) === 1) ? array($parts[0], $parts[0]) : $parts;
// If there is only one part we want to duplicate that part for generating the path.
$parts = (count($parts) === 1) ? array($parts[0], $parts[0]) : $parts;

foreach ($lookup as $base)
{
// Generate the path based on the class name parts.
$path = JPATH_PLATFORM . '/joomla/' . implode('/', array_map('strtolower', $parts)) . '.php';
$path = $base . '/' . implode('/', array_map('strtolower', $parts)) . '.php';

// Load the file if it exists.
if (file_exists($path))
{
include $path;
return include $path;
}
}
}
Expand Down

0 comments on commit 772e227

Please sign in to comment.