auth.php 1.82 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel; use Closure;
2

3
class Auth {
4 5

	/**
6
	 * The currently active authentication drivers.
7
	 *
8
	 * @var array
9
	 */
10
	public static $drivers = array();
11 12

	/**
13
	 * The third-party driver registrar.
Taylor Otwell committed
14
	 *
15
	 * @var array
Taylor Otwell committed
16
	 */
17
	public static $registrar = array();
Taylor Otwell committed
18 19

	/**
20
	 * Get an authentication driver instance.
Taylor Otwell committed
21
	 *
22 23
	 * @param  string  $driver
	 * @return Driver
24
	 */
25
	public static function driver($driver = null)
26
	{
27
		if (is_null($driver)) $driver = Config::get('auth.driver');
28

29
		if ( ! isset(static::$drivers[$driver]))
30
		{
31
			static::$drivers[$driver] = static::factory($driver);
32 33
		}

34
		return static::$drivers[$driver];
35 36 37
	}

	/**
38
	 * Create a new authentication driver instance.
39
	 *
40 41
	 * @param  string  $driver
	 * @return Driver
42
	 */
43
	protected static function factory($driver)
44
	{
45
		if (isset(static::$registrar[$driver]))
46
		{
47 48 49
			$resolver = static::$registrar[$driver];

			return $resolver();
Taylor Otwell committed
50
		}
51

52 53 54 55
		switch ($driver)
		{
			case 'fluent':
				return new Auth\Drivers\Fluent(Config::get('auth.table'));
56

57 58
			case 'eloquent':
				return new Auth\Drivers\Eloquent(Config::get('auth.model'));
59

60 61 62
			default:
				throw new \Exception("Auth driver {$driver} is not supported.");
		}
63 64 65
	}

	/**
66
	 * Register a third-party authentication driver.
67
	 *
68 69
	 * @param  string   $driver
	 * @param  Closure  $resolver
70 71
	 * @return void
	 */
72
	public static function extend($driver, Closure $resolver)
73
	{
74
		static::$registrar[$driver] = $resolver;
75 76 77
	}

	/**
78
	 * Magic Method for calling the methods on the default cache driver.
79
	 *
80 81 82 83 84 85 86
	 * <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>
87
	 */
88
	public static function __callStatic($method, $parameters)
89
	{
90
		return call_user_func_array(array(static::driver(), $method), $parameters);
Taylor Otwell committed
91
	}
92

93
}