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

Commit

Permalink
Modified JLoader to add a discover method as well as a minor modifica…
Browse files Browse the repository at this point in the history
…tion to the register method for handling force register or not.
  • Loading branch information
LouisLandry committed Apr 25, 2011
1 parent afcc049 commit 8dd6a3c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
76 changes: 67 additions & 9 deletions libraries/loader.php
Expand Up @@ -98,25 +98,83 @@ public static function import($key, $base = null)
return self::$_imported[$key];
}

/**
* 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 bool $force True to overwrite the autoload path value for the class if it already exists.
*
* @return void
*
* @since 11.1
*/
public static function discover($classPrefix, $parentPath, $force = true)
{
// Ignore the operation if the folder doesn't exist.
if (is_dir($parentPath)) {

// Open the folder.
$d = dir($parentPath);

// Iterate through the folder contents to search for input classes.
while (false !== ($entry = $d->read()))
{
// Only load for php files.
if (file_exists($parentPath.'/'.$entry) && (substr($entry, strrpos($entry, '.') + 1) == 'php')) {

// Get the class name and full path for each file.
$class = strtolower($classPrefix.preg_replace('#\.[^.]*$#', '', $entry));
$path = $parentPath.'/'.$entry;

// 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, $path);
}
}
}

// Close the folder.
$d->close();
}
}

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

/**
* Directly register a class to the autoload list.
*
* @param string $class The class name
* @param string $path Full path to the file that holds the class
* @param string $class The class name to register.
* @param string $path Full path to the file that holds the class to register.
* @param bool $force True to overwrite the autoload path value for the class if it already exists.
*
* @return bool True on success.
* @return void
*
* @since 11.1
*/
public static function register ($class, $path)
public static function register($class, $path, $force = true)
{
// Only register the class if the class and file exist.
// Sanitize class name.
$class = strtolower($class);

// Only attempt to register the class if the name and file exist.
if (!empty($class) && is_file($path)) {
self::$_classes[strtolower($class)] = $path;
return true;
}

return false;
// Register the class with the autoloader if not already registered or the force flag is set.
if (empty(self::$_classes[$class]) || $force) {
self::$_classes[$class] = $path;
}
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/suite/JLoaderTest.php
Expand Up @@ -145,7 +145,9 @@ public function testImport( $filePath, $base, $libraries, $expect, $message, $us
*/
public function testRegistersAGoodClass()
{
$this->assertTrue(JLoader::register('BogusLoad', $this->bogusFullPath));
JLoader::register('BogusLoad', $this->bogusFullPath);

$this->assertTrue(in_array($this->bogusFullPath, JLoader::getClassList()));
}

/**
Expand All @@ -157,7 +159,9 @@ public function testRegistersAGoodClass()
*/
public function testFailsToRegisterABadClass()
{
$this->assertFalse(JLoader::register("fred", "fred.php"));
JLoader::register("fred", "fred.php");

$this->assertFalse(in_array('fred.php', JLoader::getClassList()));
}

/**
Expand Down

0 comments on commit 8dd6a3c

Please sign in to comment.