Files
spa-api/laravel/autoloader.php

152 lines
3.8 KiB
PHP
Raw Normal View History

2012-01-16 13:59:24 -06:00
<?php namespace Laravel; defined('APP_PATH') or die('No direct script access.');
2011-10-31 22:15:47 -05:00
class Autoloader {
/**
2011-11-22 18:00:17 -06:00
* The mappings from class names to file paths.
2011-10-31 22:15:47 -05:00
*
* @var array
*/
2011-11-22 18:00:17 -06:00
public static $mappings = array();
2011-10-31 22:15:47 -05:00
/**
2012-01-16 13:59:24 -06:00
* All of the class aliases registered with the auto-loader.
2011-11-22 18:03:37 -06:00
*
* @var array
*/
2012-01-16 13:59:24 -06:00
public static $aliases = array();
2011-11-22 18:03:37 -06:00
/**
2012-01-16 13:59:24 -06:00
* The directories that use the PSR-0 naming convention.
2011-10-31 22:15:47 -05:00
*
* @var array
*/
2012-01-16 13:59:24 -06:00
public static $psr = array();
2011-10-31 22:15:47 -05:00
/**
* Load the file corresponding to a given class.
*
2012-01-16 13:59:24 -06:00
* This method is registerd in the bootstrap file as an SPL auto-loader.
2011-11-01 19:41:35 -05:00
*
2011-10-31 22:15:47 -05:00
* @param string $class
* @return void
*/
2011-10-31 23:58:45 -05:00
public static function load($class)
2011-10-31 22:15:47 -05:00
{
2012-01-16 13:59:24 -06:00
// First, we will check to see if the class has been aliased. If it has,
// we will register the alias, which may cause the auto-loader to be
// called again for the "real" class name.
if (isset(static::$aliases[$class]))
2011-10-31 22:15:47 -05:00
{
2012-01-16 13:59:24 -06:00
class_alias(static::$aliases[$class], $class);
2011-10-31 22:15:47 -05:00
}
2012-01-16 13:59:24 -06:00
// All classes in Laravel are staticly mapped. There is no crazy search
// routine that digs through directories. It's just a simple array of
// class to file path maps for ultra-fast file loading.
elseif (isset(static::$mappings[$class]))
2011-10-31 23:46:45 -05:00
{
2012-01-16 13:59:24 -06:00
require static::$mappings[$class];
2011-11-22 18:00:17 -06:00
}
2011-10-31 22:15:47 -05:00
2012-01-16 13:59:24 -06:00
// If the class is namespaced to an existing bundle and the bundle has
// not been started, we will start the bundle and attempt to load the
// class file again. If that fails, an error will be thrown by PHP.
//
// This allows bundle classes to be loaded by the auto-loader before
// their class mappings have actually been registered; however, it
// is up to the bundle developer to namespace their classes to
// match the name of their bundle.
elseif (($slash = strpos($class, '\\')) !== false)
2011-10-31 22:15:47 -05:00
{
2012-01-16 13:59:24 -06:00
$bundle = substr($class, 0, $slash);
2011-11-09 22:57:06 -06:00
2012-01-16 13:59:24 -06:00
// It's very important that we make sure the bundle has not been
// started here. If we don't, we'll end up in an infinite loop
// attempting to load a bundle's class.
if (Bundle::exists($bundle) and ! Bundle::started($bundle))
2011-10-31 22:15:47 -05:00
{
2012-01-16 13:59:24 -06:00
Bundle::start($bundle);
static::load($class);
2011-10-31 22:15:47 -05:00
}
}
2012-01-16 13:59:24 -06:00
static::load_psr($class);
2011-11-22 18:00:17 -06:00
}
2011-11-22 18:00:17 -06:00
/**
2012-01-16 13:59:24 -06:00
* Attempt to resolve a class using the PSR-0 standard.
2011-11-22 18:00:17 -06:00
*
* @param string $class
2012-01-16 13:59:24 -06:00
* @return void
2011-11-22 18:00:17 -06:00
*/
2012-01-16 13:59:24 -06:00
protected static function load_psr($class)
2011-11-22 18:00:17 -06:00
{
2012-01-16 13:59:24 -06:00
// The PSR-0 standard indicates that class namespace slashes or
// underscores should be used to indicate the directory tree in
// which the class resides.
$file = str_replace(array('\\', '_'), '/', $class);
// Once we have formatted the class name, we will simply spin
// through the registered PSR-0 directories and attempt to
// locate and load the class into the script.
foreach (static::$psr as $directory)
2011-11-22 18:00:17 -06:00
{
2012-01-16 13:59:24 -06:00
if (file_exists($path = $directory.strtolower($file).EXT))
{
return require $path;
}
elseif (file_exists($path = $directory.$file.EXT))
{
return require $path;
}
2011-11-22 18:00:17 -06:00
}
}
/**
2012-01-16 13:59:24 -06:00
* Register an array of class to path mappings.
2011-11-22 18:00:17 -06:00
*
* <code>
2012-01-16 13:59:24 -06:00
* // Register a class mapping with the Autoloader
* Autoloader::map(array('User' => APP_PATH.'models/user.php'));
2011-11-22 18:00:17 -06:00
* </code>
*
2012-01-16 13:59:24 -06:00
* @param array $mappings
* @return void
2011-11-22 18:00:17 -06:00
*/
2012-01-16 13:59:24 -06:00
public static function map($mappings)
2011-11-22 18:00:17 -06:00
{
2012-01-16 13:59:24 -06:00
static::$mappings = array_merge(static::$mappings, $mappings);
2011-11-22 18:00:17 -06:00
}
/**
2012-01-16 13:59:24 -06:00
* Register a class alias with the auto-loader.
2011-11-22 18:00:17 -06:00
*
2012-01-16 13:59:24 -06:00
* @param string $class
* @param string $alias
2011-11-22 18:00:17 -06:00
* @return void
*/
2012-01-16 13:59:24 -06:00
public static function alias($class, $alias)
2011-11-22 18:00:17 -06:00
{
2012-01-16 13:59:24 -06:00
static::$aliases[$alias] = $class;
2011-10-31 22:15:47 -05:00
}
2011-11-22 18:00:17 -06:00
/**
2012-01-16 13:59:24 -06:00
* Register directories to be searched as a PSR-0 library.
2011-11-22 18:00:17 -06:00
*
2012-01-16 13:59:24 -06:00
* @param string|array $directory
2011-11-22 18:00:17 -06:00
* @return void
*/
2012-01-16 13:59:24 -06:00
public static function psr($directory)
2011-11-22 18:00:17 -06:00
{
2012-01-16 13:59:24 -06:00
$directories = array_map(function($directory)
{
return rtrim($directory, '/').'/';
}, (array) $directory);
static::$psr = array_unique(array_merge(static::$psr, $directories));
2011-11-22 18:00:17 -06:00
}
2011-10-31 22:15:47 -05:00
}