Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Adding a new autoloader method to handle automatic loading of classes…
Browse files Browse the repository at this point in the history
… without having to use jimport() when an appropriate nameing/path convention is used.
  • Loading branch information
LouisLandry committed Nov 9, 2011
1 parent fefdadb commit 93ca70f
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion libraries/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

defined('JPATH_PLATFORM') or die;

// Register JLoader::load as an autoload class handler.
// Register relevant autoload methods.
spl_autoload_register(array('JLoader', 'load'));
spl_autoload_register(array('JLoader', 'autoload'));

/**
* Static class to handle loading of libraries.
Expand Down Expand Up @@ -224,6 +225,46 @@ public static function load($class)

return false;
}

/**
* Autoload a Joomla Platform class based on name.
*
* @param string $class The class to be loaded.
*
* @return boolean True on success
*
* @since 11.3
*/
public 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')
{
// Split the class name (without the J) into parts separated by camelCase.
$parts = preg_split('/(?<=[a-z])(?=[A-Z])/x',substr($class, 1));

// 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;

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

// Load the file.
if (file_exists($path))
{
include_once $path;
return true;
}
}

return false;
}
}

/**
Expand Down

0 comments on commit 93ca70f

Please sign in to comment.