Skip to content
Browse files

Some minor adjustments to the Joomla Loader class and import file.

  • Loading branch information...
1 parent 93ca70f commit 1a7caa4b9d738f2b38f21f77f39b423d9f6023aa @LouisLandry LouisLandry committed Nov 9, 2011
Showing with 99 additions and 93 deletions.
  1. +3 −0 libraries/import.php
  2. +96 −93 libraries/loader.php
View
3 libraries/import.php
@@ -47,6 +47,9 @@
require_once JPATH_PLATFORM . '/loader.php';
}
+// Setup the autoloaders.
+JLoader::setup();
+
/**
* Import the base Joomla Platform libraries.
*/
View
189 libraries/loader.php
@@ -8,10 +8,6 @@
defined('JPATH_PLATFORM') or die;
-// Register relevant autoload methods.
-spl_autoload_register(array('JLoader', 'load'));
-spl_autoload_register(array('JLoader', 'autoload'));
-
/**
* Static class to handle loading of libraries.
*
@@ -37,6 +33,71 @@
protected static $classes = array();
/**
+ * Method to discover classes of a given type in a given path.
+ *
+ * @param string $classPrefix The class name prefix to use for discovery.
+ * @param string $parentPath Full path to the parent folder for the classes to discover.
+ * @param boolean $force True to overwrite the autoload path value for the class if it already exists.
+ * @param boolean $recurse Recurse through all child directories as well as the parent path.
+ *
+ * @return void
+ *
+ * @since 11.1
+ */
+ public static function discover($classPrefix, $parentPath, $force = true, $recurse = false)
+ {
+ try
+ {
+ if ($recurse)
+ {
+ $iterator = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($parentPath),
+ RecursiveIteratorIterator::SELF_FIRST
+ );
+ }
+ else
+ {
+ $iterator = new DirectoryIterator($parentPath);
+ }
+
+ foreach ($iterator as $file)
+ {
+ $fileName = $file->getFilename();
+
+ // Only load for php files.
+ // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
+ if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
+ {
+ // Get the class name and full path for each file.
+ $class = strtolower($classPrefix . preg_replace('#\.php$#', '', $fileName));
+
+ // 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);
+ }
+ }
+ }
+ }
+ catch (UnexpectedValueException $e)
+ {
+ // Exception will be thrown if the path is not a directory. Ignore it.
+ }
+ }
+
+ /**
+ * Method to get the list of registered classes and their respective file paths for the autoloader.
+ *
+ * @return array The array of class => path values for the autoloader.
+ *
+ * @since 11.1
+ */
+ public static function getClassList()
+ {
+ return self::$classes;
+ }
+
+ /**
* Loads a class from specified directories.
*
* @param string $key The class name to look for (dot notation).
@@ -85,8 +146,8 @@ public static function import($key, $base = null)
}
/*
* If we are not importing a library from the Joomla namespace directly include the
- * file since we cannot assert the file/folder naming conventions.
- */
+ * file since we cannot assert the file/folder naming conventions.
+ */
else
{
@@ -105,68 +166,33 @@ public static function import($key, $base = null)
}
/**
- * Method to discover classes of a given type in a given path.
+ * Load the file for a class.
*
- * @param string $classPrefix The class name prefix to use for discovery.
- * @param string $parentPath Full path to the parent folder for the classes to discover.
- * @param boolean $force True to overwrite the autoload path value for the class if it already exists.
- * @param boolean $recurse Recurse through all child directories as well as the parent path.
+ * @param string $class The class to be loaded.
*
- * @return void
+ * @return boolean True on success
*
* @since 11.1
*/
- public static function discover($classPrefix, $parentPath, $force = true, $recurse = false)
+ public static function load($class)
{
- try
- {
- if ($recurse)
- {
- $iterator = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator($parentPath),
- RecursiveIteratorIterator::SELF_FIRST
- );
- }
- else
- {
- $iterator = new DirectoryIterator($parentPath);
- }
-
- foreach ($iterator as $file)
- {
- $fileName = $file->getFilename();
-
- // Only load for php files.
- // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
- if ($file->isFile() && substr($fileName, strrpos($fileName, '.') + 1) == 'php')
- {
- // Get the class name and full path for each file.
- $class = strtolower($classPrefix . preg_replace('#\.php$#', '', $fileName));
+ // Sanitize class name.
+ $class = strtolower($class);
- // 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);
- }
- }
- }
+ // If the class already exists do nothing.
+ if (class_exists($class))
+ {
+ return true;
}
- catch (UnexpectedValueException $e)
+
+ // If the class is registered include the file.
+ if (isset(self::$classes[$class]))
{
- // Exception will be thrown if the path is not a directory. Ignore it.
+ include_once self::$classes[$class];
+ return true;
}
- }
- /**
- * Method to get the list of registered classes and their respective file paths for the autoloader.
- *
- * @return array The array of class => path values for the autoloader.
- *
- * @since 11.1
- */
- public static function getClassList()
- {
- return self::$classes;
+ return false;
}
/**
@@ -197,52 +223,32 @@ public static function register($class, $path, $force = true)
}
/**
- * Load the file for a class.
+ * 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
+ * fall back on the autoloader based on conventions. This will allow people to register a
+ * class in a specific location and override platform libraries as was previously possible.
*
- * @param string $class The class to be loaded.
- *
- * @return boolean True on success
+ * @return void
*
- * @since 11.1
+ * @since 11.3
*/
- public static function load($class)
+ public static function setup()
{
- // Sanitize class name.
- $class = strtolower($class);
-
- // If the class already exists do nothing.
- if (class_exists($class))
- {
- return true;
- }
-
- // If the class is registered include the file.
- if (isset(self::$classes[$class]))
- {
- include_once self::$classes[$class];
- return true;
- }
-
- return false;
+ spl_autoload_register(array('JLoader', 'load'));
+ spl_autoload_register(array('JLoader', '_autoload'));
}
/**
* Autoload a Joomla Platform class based on name.
*
* @param string $class The class to be loaded.
*
- * @return boolean True on success
+ * @return void
*
* @since 11.3
*/
- public static function autoload($class)
+ private static function _autoload($class)
{
- // If the class already exists do nothing.
- if (class_exists($class))
- {
- return true;
- }
-
// Only attempt autoloading if we are dealing with a Joomla Platform class.
if ($class[0] == 'J')
{
@@ -255,15 +261,12 @@ public static function autoload($class)
// Generate the path based on the class name parts.
$path = JPATH_PLATFORM . '/joomla/' . implode('/', array_map('strtolower', $parts)) . '.php';
- // Load the file.
+ // Load the file if it exists.
if (file_exists($path))
{
- include_once $path;
- return true;
+ include $path;
}
}
-
- return false;
}
}

0 comments on commit 1a7caa4

Please sign in to comment.
Something went wrong with that request. Please try again.