Files
spa-api/laravel/loader.php

82 lines
1.7 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-08-02 22:19:13 -05:00
class Loader {
/**
2011-09-08 17:49:16 -05:00
* The paths that will be searched by the loader.
2011-08-02 22:19:13 -05:00
*
* @var array
*/
public static $paths = array();
2011-08-02 22:19:13 -05:00
/**
2011-09-08 17:49:16 -05:00
* The class aliases defined for the application.
2011-08-02 22:19:13 -05:00
*
* @var array
*/
public static $aliases = array();
2011-08-02 22:19:13 -05:00
/**
2011-09-08 17:49:16 -05:00
* Load the file for a given class.
2011-08-02 22:19:13 -05:00
*
* @param string $class
* @return void
*/
public static function load($class)
2011-08-02 22:19:13 -05:00
{
2011-09-20 21:28:19 -05:00
// All Laravel core classes follow a namespace to directory convention. So, we will
// replace all of the namespace slashes with directory slashes.
$file = strtolower(str_replace('\\', '/', $class));
2011-08-02 22:19:13 -05:00
2011-09-20 21:28:19 -05:00
// First, we'll check to determine if an alias exists. If it does, we will define the
// alias and bail out. Aliases are defined for most developer used core classes.
if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class);
2011-08-03 22:10:07 -05:00
foreach (static::$paths as $path)
2011-08-02 22:19:13 -05:00
{
2011-09-08 17:49:16 -05:00
if (file_exists($path = $path.$file.EXT))
2011-08-02 22:19:13 -05:00
{
require_once $path;
2011-08-02 22:19:13 -05:00
return;
2011-08-03 22:10:07 -05:00
}
}
2011-08-02 22:19:13 -05:00
}
2011-09-20 21:28:19 -05:00
/**
* Register a class alias with the auto-loader.
*
* Note: Aliases are lazy-loaded, so the aliased class will not be included until it is needed.
*
* @param string $alias
* @param string $class
* @return void
*/
public static function alias($alias, $class)
2011-09-20 21:28:19 -05:00
{
static::$aliases[$alias] = $class;
2011-09-20 21:28:19 -05:00
}
/**
* Register a path with the auto-loader.
*
* @param string $path
* @return void
*/
public static function path($path)
2011-09-20 21:28:19 -05:00
{
static::$paths[] = rtrim($path, '/').'/';
2011-09-20 21:28:19 -05:00
}
/**
* Remove an alias from the auto-loader's alias registrations.
*
* @param string $alias
* @return void
*/
public static function forget_alias($alias)
2011-09-20 21:28:19 -05:00
{
unset(static::$aliases[$alias]);
2011-09-20 21:28:19 -05:00
}
2011-08-02 22:19:13 -05:00
}