Files
spa-api/laravel/loader.php

91 lines
1.8 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
*/
2011-10-05 18:32:48 -05:00
public static $paths = array(BASE_PATH, MODEL_PATH, LIBRARY_PATH, APP_PATH);
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
*/
2011-10-05 18:32:48 -05:00
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
*
2011-09-27 21:10:32 -05:00
* <code>
* // Load the file for the "User" class
* Loader::load('User');
*
* // Load the file for the "Repositories\User" class
* Loader::load('Repositories\\User');
* </code>
*
2011-08-02 22:19:13 -05:00
* @param string $class
* @return void
*/
2011-10-05 18:32:48 -05:00
public static function load($class)
2011-08-02 22:19:13 -05:00
{
2011-09-27 21:10:32 -05:00
// All Laravel core classes follow a namespace to directory convention.
// 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-27 21:10:32 -05:00
// Check to determine if an alias exists. If it does, we will define the
// alias and bail out. Aliases are defined for most used core classes.
2011-10-05 18:32:48 -05:00
if (array_key_exists($class, static::$aliases))
2011-09-27 21:10:32 -05:00
{
2011-10-05 18:32:48 -05:00
return class_alias(static::$aliases[$class], $class);
2011-09-27 21:10:32 -05:00
}
2011-08-03 22:10:07 -05:00
2011-10-05 18:32:48 -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.
*
* @param string $alias
* @param string $class
* @return void
*/
2011-10-05 18:32:48 -05:00
public static function alias($alias, $class)
2011-09-20 21:28:19 -05:00
{
2011-10-05 18:32:48 -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
*/
2011-10-05 18:32:48 -05:00
public static function path($path)
2011-09-20 21:28:19 -05:00
{
2011-10-05 18:32:48 -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
*/
2011-10-05 18:32:48 -05:00
public static function forget_alias($alias)
2011-09-20 21:28:19 -05:00
{
2011-10-05 18:32:48 -05:00
unset(static::$aliases[$alias]);
2011-09-20 21:28:19 -05:00
}
2011-08-02 22:19:13 -05:00
}