Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A class for lazy-loading other classes
- *
- * This class enables lazy-loading of php classes. The benefit of this is
- * three-fold. First, there is a memory benefit, since not all classes are
- * loaded until they are needed. Second, there is a time benefit, since not all
- * classes are loaded. Third, it produces cleaner code, since there is no need
- * to litter files with require_once() calls.
- *
- * @category Libraries
- * @package Libraries
- * @author Me
- */
- abstract class Loader
- {
- /**
- * @var array An array of class to path mappings
- */
- protected static $classes = array();
- /**
- * @var boolean Has the loader been initialized already
- */
- protected static $initialized = false;
- /**
- * @var array An array of auto-search paths
- */
- protected static $namedPaths = array(
- 'exception',
- 'interface',
- 'iterator',
- );
- /**
- * @var array An array of include paths to search
- */
- protected static $paths = array(
- PATH_LIBS,
- );
- /**
- * Tell the auto-loader where to find an un-loaded class
- *
- * This can be used to "register" new classes that are unknown to the
- * system. It can also be used to "overload" a class (redefine it
- * elsewhere)
- *
- * @param string $class The class name to overload
- * @param string $path The path to the new class
- *
- * @throws InvalidArgumentException Upon an Invalid path submission
- * @return void
- */
- public static function _($class, $path)
- {
- $class = strtolower($class);
- if (!file_exists($path)) {
- throw new InvalidArgumentException('Invalid Path Specified');
- }
- self::$classes[$class] = $path;
- }
- /**
- * Add a path to the include path list
- *
- * This adds a path to the list of paths to search for an included file.
- * This should not be used to overload classes, since the default include
- * directory will always be searched first. This can be used to extend
- * the search path to include new parts of the system
- *
- * @param string $path The path to add to the search list
- *
- * @throws InvalidArgumentException when an invalid path is specified
- * @return void
- */
- public static function addPath($path)
- {
- if (!is_dir($path)) {
- throw new InvalidArgumentException('Invalid Include Path Added');
- }
- $path = rtrim($path, DS);
- if (!in_array($path, self::$paths)) {
- self::$paths[] = $path;
- }
- }
- /**
- * Add a path to the auto-search paths (for trailing extensions)
- *
- * The path should end with an 's'. Default files should not.
- *
- * @param string $path The name of the new auto-search path
- *
- * @return void
- */
- public static function addNamedPath($path)
- {
- $path = strtolower($path);
- if (substr($path, -1) == 's') {
- $path = substr($path, 0, -1);
- }
- if (!in_array($path, self::$namedPaths)) {
- self::$namedPaths[] = $path;
- }
- }
- /**
- * Initialize and register the autoloader.
- *
- * This method will setup the autoloader. This should only be called once.
- *
- * @return void
- */
- public static function initialize()
- {
- if (!self::$initialized) {
- self::$initialized = true;
- spl_autoload_register(array('Loader', 'load'));
- }
- }
- /**
- * The actual auto-loading function.
- *
- * This is automatically called by PHP whenever a class name is used that
- * doesn't exist yet. There should be no need to manually call this method.
- *
- * @param string $class The class name to load
- *
- * @return void
- */
- public static function load($class)
- {
- $className = strtolower($class);
- if (isset(self::$classes[$className])) {
- $file = self::$classes[$className];
- } else {
- $file = self::findFile($class);
- }
- if (file_exists($file)) {
- include_once $file;
- }
- }
- /**
- * Find the file to include based upon its name
- *
- * This splits the class name by uppercase letter, and then rejoins them
- * to attain the file system path. So FooBarBaz will be turned into
- * foo/bar/baz. It then searches the include paths for that chain. If baz
- * is a directory, it searches that directory for a file called baz.php.
- * Otherwise, it looks for baz.php under the bar directory.
- *
- * @param string $class The name of the class to find
- *
- * @return string The path to the file defining that class
- */
- protected static function findFile($class)
- {
- $regex = '#([A-Z]{1}[a-z0-9_]+)#';
- $options = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
- $parts = preg_split($regex, $class, null, $options);
- $subpath = '';
- $file = strtolower(end($parts));
- $test = strtolower(reset($parts));
- if ($test == 'is') {
- array_shift($parts);
- return self::findNamedFile($class, $parts, 'interface');
- }
- foreach ($parts as $part) {
- $subpath .= DS . strtolower($part);
- }
- foreach (self::$paths as $path) {
- $newpath = $path . $subpath;
- if (is_file($newpath . '.php')) {
- return $newpath . '.php';
- } elseif (is_file($newpath . DS . $file . '.php')) {
- return $newpath . DS . $file . '.php';
- }
- }
- if (in_array($file, self::$namedPaths)) {
- //Get rid of the trailing part
- array_pop($parts);
- return self::findNamedFile($class, $parts, $file);
- }
- return '';
- }
- /**
- * Find a file for named directories (interfaces, exceptions, iterators, etc)
- *
- * @param string $class The class name of the exception to find
- * @param array $parts The parts of the class name pre-split
- * @param string $name The name of the named directory to search in
- *
- * @return string The found path, or '' if not found
- */
- protected static function findNamedFile($class, array $parts, $name)
- {
- if (empty($parts)) {
- return '';
- }
- $name = strtolower($name);
- //Add a trailing s, since individual files are not plural
- $filename = $name;
- $name .= 's';
- //Try the global path first
- $subpath = DS . $name . DS . strtolower(implode('', $parts)) . '.php';
- foreach (self::$paths as $path) {
- $newpath = $path . $subpath;
- if (is_file($newpath)) {
- return $newpath;
- }
- }
- //Try to build a full sub path for package specific named files
- $package = array_shift($parts);
- $subpath = DS . strtolower($package) . DS . $name . DS;
- if (!empty($parts)) {
- $subpath .= strtolower(implode('', $parts)) . '.php';
- } else {
- $subpath .= $filename . '.php';
- }
- foreach (self::$paths as $path) {
- $newpath = $path . $subpath;
- if (is_file($newpath)) {
- return $newpath;
- }
- }
- return '';
- }
- }
- <Vendor Name>(<Namespace>)*<Class Name>
- $classLoader = new SplClassLoader('DoctrineCommon', '/libs/doctrine');
- $classLoader->register();
- $classLoader = new SplClassLoader('IrcmexellMyApplication', 'libs/internal');
- $classLoader->register();
- pwd
- /var/www/project/app
- ls -lah PHPE*
- PHPExcel -> ../external/PHPExcel/PHPExcel
- PHPExcel.php -> ../external/PHPExcel/PHPExcel.php
- set_include_path (PATH . 'app');
- require 'somepath/Autoloader.php';
- Autoloader::registerAutoload ();
- class Autoloader
- {
- public static function registerAutoload ()
- {
- spl_autoload_register (array (__CLASS__, 'autoload'));
- }
- /**
- * @static
- * @param string $class
- * @return void
- */
- public static function autoload ($class)
- {
- require str_replace (array ('_', '\'), '/', $class) . '.php';
- }
- public static function registerAutoload ()
- {
- spl_autoload_register (array (__CLASS__, 'autoload'));
- }
- /**
- * @static
- * @param string $class
- * @return void
- */
- public static function autoload ($class)
- {
- require str_replace (array ('_', '\'), '/', $class) . '.php';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement