Files
spa-api/laravel/auth.php

93 lines
1.8 KiB
PHP
Raw Normal View History

2012-05-02 16:36:34 -05:00
<?php namespace Laravel; use Closure;
class Auth {
2011-06-08 23:45:08 -05:00
/**
2012-05-01 10:50:33 -05:00
* The currently active authentication drivers.
2011-06-08 23:45:08 -05:00
*
2012-05-01 10:50:33 -05:00
* @var array
2011-06-08 23:45:08 -05:00
*/
2012-05-01 10:50:33 -05:00
public static $drivers = array();
2011-06-08 23:45:08 -05:00
2011-09-21 21:46:16 -05:00
/**
2012-05-01 10:50:33 -05:00
* The third-party driver registrar.
2011-09-21 21:46:16 -05:00
*
2012-05-01 10:50:33 -05:00
* @var array
2011-09-21 21:46:16 -05:00
*/
2012-05-01 10:50:33 -05:00
public static $registrar = array();
2011-09-21 21:46:16 -05:00
2011-06-08 23:45:08 -05:00
/**
2012-05-01 10:50:33 -05:00
* Get an authentication driver instance.
2011-11-02 21:27:43 -05:00
*
2012-05-01 10:50:33 -05:00
* @param string $driver
* @return Driver
2011-06-08 23:45:08 -05:00
*/
2012-05-01 10:50:33 -05:00
public static function driver($driver = null)
2011-06-08 23:45:08 -05:00
{
2012-05-01 10:50:33 -05:00
if (is_null($driver)) $driver = Config::get('auth.driver');
2011-06-08 23:45:08 -05:00
2012-05-01 10:50:33 -05:00
if ( ! isset(static::$drivers[$driver]))
{
2012-05-01 10:50:33 -05:00
static::$drivers[$driver] = static::factory($driver);
}
2012-05-01 10:50:33 -05:00
return static::$drivers[$driver];
2011-06-08 23:45:08 -05:00
}
/**
2012-05-01 10:50:33 -05:00
* Create a new authentication driver instance.
*
2012-05-01 10:50:33 -05:00
* @param string $driver
* @return Driver
*/
2012-05-01 10:50:33 -05:00
protected static function factory($driver)
{
2012-05-01 10:50:33 -05:00
if (isset(static::$registrar[$driver]))
{
2012-05-02 16:52:55 -05:00
$resolver = static::$registrar[$driver];
return $resolver();
2011-10-10 21:34:15 -05:00
}
2012-05-01 10:50:33 -05:00
switch ($driver)
{
case 'fluent':
return new Auth\Drivers\Fluent(Config::get('auth.table'));
2012-05-01 10:50:33 -05:00
case 'eloquent':
return new Auth\Drivers\Eloquent(Config::get('auth.model'));
2011-06-08 23:45:08 -05:00
2012-05-01 10:50:33 -05:00
default:
throw new \Exception("Auth driver {$driver} is not supported.");
}
2011-06-08 23:45:08 -05:00
}
/**
2012-05-01 10:50:33 -05:00
* Register a third-party authentication driver.
*
2012-05-01 10:50:33 -05:00
* @param string $driver
* @param Closure $resolver
* @return void
*/
2012-05-01 10:50:33 -05:00
public static function extend($driver, Closure $resolver)
{
2012-05-01 10:50:33 -05:00
static::$registrar[$driver] = $resolver;
}
2011-06-08 23:45:08 -05:00
/**
2012-05-01 10:50:33 -05:00
* Magic Method for calling the methods on the default cache driver.
2011-06-08 23:45:08 -05:00
*
2012-05-01 10:50:33 -05:00
* <code>
* // Call the "user" method on the default auth driver
* $user = Auth::user();
*
* // Call the "check" method on the default auth driver
* Auth::check();
* </code>
2011-06-08 23:45:08 -05:00
*/
2012-05-01 10:50:33 -05:00
public static function __callStatic($method, $parameters)
2011-06-08 23:45:08 -05:00
{
2012-05-01 10:50:33 -05:00
return call_user_func_array(array(static::driver(), $method), $parameters);
2011-08-19 20:12:39 -05:00
}
2011-08-11 13:45:17 -05:00
2011-11-13 15:23:48 -06:00
}