Files
spa-api/laravel/loader.php

112 lines
2.3 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-09-20 21:28:19 -05:00
protected $paths;
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-09-20 21:28:19 -05:00
protected $aliases;
2011-08-02 22:19:13 -05:00
/**
2011-09-08 17:49:16 -05:00
* Create a new class loader instance.
2011-08-02 22:19:13 -05:00
*
2011-09-08 17:49:16 -05:00
* @param array $paths
* @param array $aliases
2011-08-02 22:19:13 -05:00
* @return void
*/
2011-09-08 17:49:16 -05:00
public function __construct($paths, $aliases)
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
}
/**
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
*/
2011-08-26 21:42:04 -05:00
public 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, $this->aliases)) return class_alias($this->aliases[$class], $class);
2011-08-03 22:10:07 -05:00
2011-09-08 17:49:16 -05:00
foreach ($this->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.
*
* <code>
* // Register an alias for the "SwiftMailer\Transport" class
* Loader::alias('Transport', 'SwiftMailer\\Transport');
* </code>
*
* @param string $alias
* @param string $class
* @return void
*/
public function alias($alias, $class)
{
$this->aliases[$alias] = $class;
}
/**
* Register a path with the auto-loader.
*
* The registered path will be searched when auto-loading classes.
*
* <code>
* // Register a path to be searched by the auto-loader
* Loader::path('path/to/files');
* </code>
*
* @param string $path
* @return void
*/
public function path($path)
{
$this->paths[] = rtrim($path, '/').'/';
}
/**
* Remove an alias from the auto-loader's alias registrations.
*
* <code>
* // Remove the "Transport" alias from the registered aliases
* Loader::forget_alias('Transport');
* </code>
*
* @param string $alias
* @return void
*/
public function forget_alias($alias)
{
unset($this->aliases[$alias]);
}
2011-08-02 22:19:13 -05:00
}