- <?php
- /**
- * Loader
- *
- * PSR-0 Compliant autoloader which can be set to load Namespaced classes from
- * alternative locations for added flexibility. Simply register the path that
- * *should* be used for the namespace using `Loader::[namespace]($path)`.
- *
- * @package MicroMVC / UnitEngine
- * @author David Pennington
- * @copyright (c) 2012 David Pennington
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class Loader
- {
- public static $namespaces;
- public static $classes;
- /**
- * Add a namespace path to the autoloader
- *
- * @param string $namespace The namespace the path is for
- * @param array $args The file path is the first element
- */
- public static function __callStatic($namespace, $args)
- {
- static::$namespaces[$namespace] = rtrim($args[0], '/');
- }
- /**
- * Map a class to a direct file location
- *
- * @param string $class The class the path is for
- * @param string $path The file path of the class
- */
- public static function map($class, $path)
- {
- static::$classes[$class] = $path;
- }
- /**
- * Called by spl_autoload_register() when using classes. PSR-0 compliant.
- * Each "_" character in the CLASS NAME is converted to a directory
- * separator. The "_" character has no special meaning in the namespace.
- *
- * @param string $class The full, namespaced class name.
- */
- public static function autoload($class)
- {
- if(isset(static::$classes[$class]))
- {
- require static::$classes[$class];
- return;
- }
- $directory = explode('/', str_replace('\\', '/', ltrim($class, '\\')));
- $class = array_pop($directory);
- $directory = array_merge($directory, explode('/', str_replace('_', '/', $class)));
- $class = array_pop($directory);
- $lower = strtolower($class);
- if( ! $directory AND isset(static::$namespaces[$class]))
- {
- $directory = static::$namespaces[$class];
- }
- elseif($directory)
- {
- $namespace = array_shift($directory);
- if(isset(static::$namespaces[$namespace]))
- {
- $namespace = static::$namespaces[$namespace];
- }
- else
- {
- $namespace = VENDOR_PATH . $namespace;
- }
- $directory = rtrim($namespace . '/'. join('/', $directory), '/');
- }
- else
- {
- $directory = VENDOR_PATH . $class;
- }
- if (is_file($path = $directory.'/'.$class.EXT))
- {
- return require $path;
- }
- elseif (is_file($path = $directory.'/'.$lower.EXT))
- {
- return require $path;
- }
- throw new Exception('Could not locate '. func_get_arg(0));
- }
- }
- /*
- * Usage
- * You should have a "/vendor" folder with a checkout of each of these projects:
- *
- * $ mkdir vendor
- * $ cd vendor
- * $ git clone git://github.com/laravel/laravel.git Laravel
- */
- define('VENDOR_PATH', __DIR__ . '/vendor/');
- spl_autoload_register(array('Loader', 'autoload'));
- Loader::Laravel(VENDOR_PATH . 'Laravel/laravel');
- Loader::Zend(VENDOR_PATH . 'Zend/library/Zend');
- Loader::Slim(VENDOR_PATH . 'Slim/Slim');
- Loader::Atom(VENDOR_PATH . 'Atom/src/Atom');
- Loader::Fuel(VENDOR_PATH . 'Fuel/classes');
- Loader::Kohana(VENDOR_PATH . 'Kohana/classes/kohana');
- // Or map problem classes manually...
- Loader::map('Slim', VENDOR_PATH . 'Slim/Slim/Slim.php');
- Loader::map('Fuel\Core\Arr', VENDOR_PATH . 'Fuel/classes/arr.php');
- $o = new Phunction;
- ph();
- $o = new Laravel\Database;
- $o = new Zend\Version;
- $o = new Slim;
- $o = new Slim_View;
- $o = new Kohana_Arr;
- $o = new Fuel\Core\Arr;
- print "Profit!";