Last active
August 29, 2015 14:04
-
-
Save pjdietz/05f1fcef4ca3bd7d6448 to your computer and use it in GitHub Desktop.
PHP class for autoloading classes from a specific directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Allow PHP to autoload classes from a specific directory. | |
* | |
* https://gist.github.com/pjdietz/05f1fcef4ca3bd7d6448 | |
* | |
* Classes MUST be one class or interface per file with the name of the class or interface matching the filename | |
* (minus .php). | |
* | |
* Classes MAY be one namespace, nested namespaces (matching the directory names), or no namespace. | |
* | |
* Based on the AutoLoader class by Jess Telford | |
* http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/ | |
*/ | |
class DirectoryAutoloader | |
{ | |
/** | |
* Associate array of class names as keys and file names as values. | |
* @var array | |
*/ | |
static private $classMap = array(); | |
/** | |
* @param string $path Path to the directory to recurse for PHP classes. | |
* @param string $namespace Namespace of the classes to autoload. | |
* @param bool $appendDirectoryToNamespace If true, directory names under $path are appended to the namespace. | |
*/ | |
public static function registerDirectory($path, $namespace = "", $appendDirectoryToNamespace = false) | |
{ | |
// Ensure the namespace has a trailing delimiter or is empty. | |
if ($namespace && substr($namespace, -1) !== "\\") { | |
$namespace .= "\\"; | |
} | |
// Iterate over the items in the path provided. | |
foreach (new DirectoryIterator($path) as $file) { | |
/** @var DirectoryIterator $file */ | |
if ($file->isDir() && !$file->isLink() && !$file->isDot()) { | |
// Directory | |
// Optionally append the directory name to the namespace and recurse. | |
if ($appendDirectoryToNamespace) { | |
$childNamespace = $namespace . $file->getFilename() . "\\"; | |
} else { | |
$childNamespace = $namespace; | |
} | |
self::registerDirectory($file->getPathname(), $childNamespace, $appendDirectoryToNamespace); | |
} elseif (substr($file->getFilename(), -4) === ".php") { | |
// PHP File. | |
$className = substr($file->getFilename(), 0, -4); | |
self::registerClass($namespace . $className, $file->getPathname()); | |
} | |
} | |
} | |
/** | |
* @param string $className Name (including namespace) of the class or interface to register | |
* @param string $filePath Path the the file defining the class or interface | |
*/ | |
public static function registerClass($className, $filePath) | |
{ | |
self::$classMap[$className] = $filePath; | |
} | |
/** | |
* @param string $className | |
*/ | |
public static function loadClass($className) | |
{ | |
if (isset(self::$classMap[$className])) { | |
/** @noinspection PhpIncludeInspection */ | |
require_once(self::$classMap[$className]); | |
} | |
} | |
} | |
spl_autoload_register(array("DirectoryAutoloader", "loadClass")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment