Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 3.29 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Loader
  4.  *
  5.  * PSR-0 Compliant autoloader which can be set to load Namespaced classes from
  6.  * alternative locations for added flexibility. Simply register the path that
  7.  * *should* be used for the namespace using `Loader::[namespace]($path)`.
  8.  *
  9.  * @package   MicroMVC / UnitEngine
  10.  * @author    David Pennington
  11.  * @copyright (c) 2012 David Pennington
  12.  * @license   MIT License (http://www.opensource.org/licenses/mit-license.php)
  13.  */
  14. class Loader
  15. {
  16.         public static $namespaces;
  17.         public static $classes;
  18.  
  19.         /**
  20.          * Add a namespace path to the autoloader
  21.          *
  22.          * @param string $namespace The namespace the path is for
  23.          * @param array $args The file path is the first element
  24.          */
  25.         public static function __callStatic($namespace, $args)
  26.         {
  27.                 static::$namespaces[$namespace] = rtrim($args[0], '/');
  28.         }
  29.  
  30.         /**
  31.          * Map a class to a direct file location
  32.          *
  33.          * @param string $class The class the path is for
  34.          * @param string $path The file path of the class
  35.          */
  36.         public static function map($class, $path)
  37.         {
  38.                 static::$classes[$class] = $path;
  39.         }
  40.  
  41.         /**
  42.          * Called by spl_autoload_register() when using classes. PSR-0 compliant.
  43.          * Each "_" character in the CLASS NAME is converted to a directory
  44.          * separator. The "_" character has no special meaning in the namespace.
  45.          *
  46.          * @param string $class The full, namespaced class name.
  47.          */
  48.         public static function autoload($class)
  49.         {
  50.                 if(isset(static::$classes[$class]))
  51.                 {
  52.                         require static::$classes[$class];
  53.                         return;
  54.                 }
  55.  
  56.                 $directory = explode('/', str_replace('\\', '/', ltrim($class, '\\')));
  57.                 $class = array_pop($directory);
  58.                 $directory = array_merge($directory, explode('/', str_replace('_', '/', $class)));
  59.                 $class = array_pop($directory);
  60.                 $lower = strtolower($class);
  61.  
  62.                 if( ! $directory AND isset(static::$namespaces[$class]))
  63.                 {
  64.                         $directory = static::$namespaces[$class];
  65.                 }
  66.                 elseif($directory)
  67.                 {
  68.                         $namespace = array_shift($directory);
  69.  
  70.                         if(isset(static::$namespaces[$namespace]))
  71.                         {
  72.                                 $namespace = static::$namespaces[$namespace];
  73.                         }
  74.                         else
  75.                         {
  76.                                 $namespace = VENDOR_PATH . $namespace;
  77.                         }
  78.  
  79.                         $directory = rtrim($namespace . '/'. join('/', $directory), '/');
  80.                 }
  81.                 else
  82.                 {
  83.                         $directory = VENDOR_PATH . $class;
  84.                 }
  85.  
  86.                 if (is_file($path = $directory.'/'.$class.EXT))
  87.                 {
  88.                         return require $path;
  89.                 }
  90.                 elseif (is_file($path = $directory.'/'.$lower.EXT))
  91.                 {
  92.                         return require $path;
  93.                 }
  94.  
  95.                 throw new Exception('Could not locate '. func_get_arg(0));
  96.         }
  97. }
  98.  
  99.  
  100. /*
  101.  * Usage
  102.  * You should have a "/vendor" folder with a checkout of each of these projects:
  103.  *
  104.  * $ mkdir vendor
  105.  * $ cd vendor
  106.  * $ git clone git://github.com/laravel/laravel.git Laravel
  107.  */
  108.  
  109. define('VENDOR_PATH', __DIR__ . '/vendor/');
  110. spl_autoload_register(array('Loader', 'autoload'));
  111.  
  112. Loader::Laravel(VENDOR_PATH . 'Laravel/laravel');
  113. Loader::Zend(VENDOR_PATH . 'Zend/library/Zend');
  114. Loader::Slim(VENDOR_PATH . 'Slim/Slim');
  115. Loader::Atom(VENDOR_PATH . 'Atom/src/Atom');
  116. Loader::Fuel(VENDOR_PATH . 'Fuel/classes');
  117. Loader::Kohana(VENDOR_PATH . 'Kohana/classes/kohana');
  118.  
  119. // Or map problem classes manually...
  120. Loader::map('Slim', VENDOR_PATH . 'Slim/Slim/Slim.php');
  121. Loader::map('Fuel\Core\Arr', VENDOR_PATH . 'Fuel/classes/arr.php');
  122.  
  123.  
  124. $o = new Phunction;
  125. ph();
  126.  
  127. $o = new Laravel\Database;
  128. $o = new Zend\Version;
  129. $o = new Slim;
  130. $o = new Slim_View;
  131. $o = new Kohana_Arr;
  132. $o = new Fuel\Core\Arr;
  133.  
  134.  
  135. print "Profit!";