Files
spa-api/laravel/autoloader.php

213 lines
5.4 KiB
PHP
Raw Normal View History

2012-01-28 14:55:08 -06:00
<?php namespace Laravel; defined('DS') 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-23 16:07:08 -06:00
* The directories that use the PSR-0 naming convention.
2011-11-22 18:03:37 -06:00
*
* @var array
*/
2012-01-23 16:07:08 -06:00
public static $psr = array();
2011-11-22 18:03:37 -06:00
/**
2012-01-23 16:07:08 -06:00
* The mappings for namespaces to directories.
2011-10-31 22:15:47 -05:00
*
* @var array
*/
2012-01-23 16:07:08 -06:00
public static $namespaces = array();
2011-10-31 22:15:47 -05:00
/**
2012-01-23 16:07:08 -06:00
* All of the class aliases registered with the auto-loader.
*
* @var array
*/
2012-01-23 16:07:08 -06:00
public static $aliases = 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
elseif (($slash = strpos($class, '\\')) !== false)
2011-10-31 22:15:47 -05:00
{
$namespace = substr($class, 0, $slash);
2012-01-23 16:07:08 -06:00
// If the class namespace is mapped to a directory, we will load the class
// using the PSR-0 standards from that directory; however, we will trim
// off the beginning of the namespace to account for files in the root
// of the mapped directory.
if (isset(static::$namespaces[$namespace]))
{
$directory = static::$namespaces[$namespace];
return static::load_psr(substr($class, $slash + 1), $directory);
}
2011-11-09 22:57:06 -06:00
2012-01-23 16:07:08 -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.
if (Bundle::exists($namespace) and ! Bundle::started($namespace))
2011-10-31 22:15:47 -05:00
{
Bundle::start(strtolower($namespace));
2012-01-16 13:59:24 -06:00
static::load($class);
2011-10-31 22:15:47 -05:00
}
}
2012-01-23 16:07:08 -06:00
// If the class is not maped and is not part of a bundle or a mapped
// namespace, we'll make a last ditch effort to load the class via
// the PSR-0 from one of the registered directories.
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
* @param string $directory
2012-01-16 13:59:24 -06:00
* @return void
2011-11-22 18:00:17 -06:00
*/
protected static function load_psr($class, $directory = null)
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
2012-01-23 16:07:08 -06:00
// which the class resides, so we'll convert the namespace
// slashes to directory slashes.
2012-01-16 13:59:24 -06:00
$file = str_replace(array('\\', '_'), '/', $class);
if (is_null($directory))
{
$directories = static::$psr;
}
else
{
$directories = array($directory);
}
2012-01-16 13:59:24 -06:00
// 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.
2012-01-23 16:07:08 -06:00
//
// We will check for both lowercase and CamelCase files as
// Laravel uses a lowercase version of PSR-0, while true
2012-01-25 15:31:04 -06:00
// PSR-0 uses CamelCase for all file names.
$lower = strtolower($file);
foreach ($directories as $directory)
2011-11-22 18:00:17 -06:00
{
if (file_exists($path = $directory.$lower.EXT))
2012-01-16 13:59:24 -06:00
{
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
2012-01-28 14:55:08 -06:00
* Autoloader::map(array('User' => path('app').'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
{
$directories = static::format($directory);
2012-01-16 13:59:24 -06:00
static::$psr = array_unique(array_merge(static::$psr, $directories));
2011-11-22 18:00:17 -06:00
}
/**
* Map namespaces to directories.
*
* @param string $namespace
* @param string $path
*/
public static function namespaces($mappings)
{
$directories = static::format(array_values($mappings));
$mappings = array_combine(array_keys($mappings), $directories);
static::$namespaces = array_merge(static::$namespaces, $mappings);
}
/**
* Format an array of directories with the proper trailing slashes.
*
* @param array $directories
* @return array
*/
protected static function format($directories)
{
return array_map(function($directory)
{
return rtrim($directory, DS).DS;
2012-01-23 16:07:08 -06:00
}, (array) $directories);
}
2011-10-31 22:15:47 -05:00
}