auth.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php namespace Laravel; use Closure;
  2. class Auth {
  3. /**
  4. * The currently active authentication drivers.
  5. *
  6. * @var array
  7. */
  8. public static $drivers = array();
  9. /**
  10. * The third-party driver registrar.
  11. *
  12. * @var array
  13. */
  14. public static $registrar = array();
  15. /**
  16. * Get an authentication driver instance.
  17. *
  18. * @param string $driver
  19. * @return Driver
  20. */
  21. public static function driver($driver = null)
  22. {
  23. if (is_null($driver)) $driver = Config::get('auth.driver');
  24. if ( ! isset(static::$drivers[$driver]))
  25. {
  26. static::$drivers[$driver] = static::factory($driver);
  27. }
  28. return static::$drivers[$driver];
  29. }
  30. /**
  31. * Create a new authentication driver instance.
  32. *
  33. * @param string $driver
  34. * @return Driver
  35. */
  36. protected static function factory($driver)
  37. {
  38. if (isset(static::$registrar[$driver]))
  39. {
  40. $resolver = static::$registrar[$driver];
  41. return $resolver();
  42. }
  43. switch ($driver)
  44. {
  45. case 'fluent':
  46. return new Auth\Drivers\Fluent(Config::get('auth.table'));
  47. case 'eloquent':
  48. return new Auth\Drivers\Eloquent(Config::get('auth.model'));
  49. default:
  50. throw new \Exception("Auth driver {$driver} is not supported.");
  51. }
  52. }
  53. /**
  54. * Register a third-party authentication driver.
  55. *
  56. * @param string $driver
  57. * @param Closure $resolver
  58. * @return void
  59. */
  60. public static function extend($driver, Closure $resolver)
  61. {
  62. static::$registrar[$driver] = $resolver;
  63. }
  64. /**
  65. * Magic Method for calling the methods on the default cache driver.
  66. *
  67. * <code>
  68. * // Call the "user" method on the default auth driver
  69. * $user = Auth::user();
  70. *
  71. * // Call the "check" method on the default auth driver
  72. * Auth::check();
  73. * </code>
  74. */
  75. public static function __callStatic($method, $parameters)
  76. {
  77. return call_user_func_array(array(static::driver(), $method), $parameters);
  78. }
  79. }