Permalink
Browse files

Converted JLoader::discover to use PHP's directory iterator.

  • Loading branch information...
1 parent 1809579 commit 6d20abb804b88d511cadaf31c9ad9d892fbffd4e @eddieajau eddieajau committed Aug 24, 2011
Showing with 13 additions and 16 deletions.
  1. +13 −16 libraries/loader.php
View
@@ -116,33 +116,30 @@ public static function import($key, $base = null)
*/
public static function discover($classPrefix, $parentPath, $force = true)
{
- // Ignore the operation if the folder doesn't exist.
- if (is_dir($parentPath))
+ try
{
- // Open the folder.
- $d = dir($parentPath);
-
- // Iterate through the folder contents to search for input classes.
- while (false !== ($entry = $d->read()))
+ foreach (new DirectoryIterator($parentPath) as $file)
{
+ $fileName = $file->getFilename();
+
// Only load for php files.
- if (file_exists($parentPath . '/' . $entry) && (substr($entry, strrpos($entry, '.') + 1) == 'php'))
+ // 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('#\.[^.]*$#', '', $entry));
- $path = $parentPath . '/' . $entry;
+ $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, $path);
+ JLoader::register($class, $file->getPath().'/'.$fileName);
}
}
}
-
- // Close the folder.
- $d->close();
+ }
+ catch (UnexpectedValueException $e)
+ {
+ // Exception will be thrown if the path is not a directory. Ignore it.
}
}
@@ -202,7 +199,7 @@ public static function load($class)
// If the class already exists do nothing.
if (class_exists($class))
{
- return;
+ return true;
}
// If the class is registered include the file.

0 comments on commit 6d20abb

Please sign in to comment.