Files
spa-api/laravel/loader.php

94 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 {
/**
* The paths to be searched by the loader.
*
* @var array
*/
2011-08-31 00:07:45 -05:00
private $paths;
2011-08-02 22:19:13 -05:00
/**
* All of the class aliases.
*
* @var array
*/
2011-08-31 00:07:45 -05:00
private $aliases;
2011-08-02 22:19:13 -05:00
/**
* Bootstrap the auto-loader.
*
* @return void
*/
2011-08-26 21:42:04 -05:00
public function __construct($aliases, $paths)
2011-08-02 22:19:13 -05:00
{
2011-08-26 21:42:04 -05:00
$this->paths = $paths;
$this->aliases = $aliases;
2011-08-02 22:19:13 -05:00
}
/**
* Load a class file for a given class name.
*
* This function is registered on the SPL auto-loader stack by the front controller during each request.
2011-08-03 22:10:07 -05:00
* All Laravel class names follow a namespace to directory convention.
2011-08-02 22:19:13 -05:00
*
* @param string $class
* @return void
*/
2011-08-26 21:42:04 -05:00
public function load($class)
2011-08-02 22:19:13 -05:00
{
$file = strtolower(str_replace('\\', '/', $class));
2011-08-26 21:42:04 -05:00
if (array_key_exists($class, $this->aliases))
{
2011-08-26 21:42:04 -05:00
return class_alias($this->aliases[$class], $class);
}
2011-08-03 22:10:07 -05:00
2011-08-26 21:42:04 -05:00
foreach ($this->paths as $directory)
2011-08-02 22:19:13 -05:00
{
if (file_exists($path = $directory.$file.EXT))
{
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-08-19 21:39:38 -05:00
* Register a path with the auto-loader.
*
* After registering the path, it will be checked similarly to the models and libraries directories.
2011-08-02 22:19:13 -05:00
*
* @param string $path
* @return void
*/
2011-08-26 21:42:04 -05:00
public function register_path($path)
2011-08-02 22:19:13 -05:00
{
2011-08-26 21:42:04 -05:00
$this->paths[] = rtrim($path, '/').'/';
2011-08-02 22:19:13 -05:00
}
2011-08-19 21:39:38 -05:00
/**
* Register an alias with the auto-loader.
*
* @param array $alias
* @return void
*/
2011-08-26 21:42:04 -05:00
public function register_alias($alias)
2011-08-19 21:39:38 -05:00
{
2011-08-26 21:42:04 -05:00
$this->aliases = array_merge($this->aliases, $alias);
2011-08-19 21:39:38 -05:00
}
/**
* Remove an alias from the auto-loader's list of aliases.
*
* @param string $alias
* @return void
*/
2011-08-26 21:42:04 -05:00
public function forget_alias($alias)
2011-08-19 21:39:38 -05:00
{
2011-08-26 21:42:04 -05:00
unset($this->aliases[$alias]);
2011-08-19 21:39:38 -05:00
}
2011-08-02 22:19:13 -05:00
}