auth.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php namespace Laravel;
  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. return static::$registrar[$driver]();
  41. }
  42. switch ($driver)
  43. {
  44. case 'fluent':
  45. return new Auth\Drivers\Fluent(Config::get('auth.table'));
  46. case 'eloquent':
  47. return new Auth\Drivers\Eloquent(Config::get('auth.model'));
  48. default:
  49. throw new \Exception("Auth driver {$driver} is not supported.");
  50. }
  51. }
  52. /**
  53. * Register a third-party authentication driver.
  54. *
  55. * @param string $driver
  56. * @param Closure $resolver
  57. * @return void
  58. */
  59. public static function extend($driver, Closure $resolver)
  60. {
  61. static::$registrar[$driver] = $resolver;
  62. }
  63. /**
  64. * Magic Method for calling the methods on the default cache driver.
  65. *
  66. * <code>
  67. * // Call the "user" method on the default auth driver
  68. * $user = Auth::user();
  69. *
  70. * // Call the "check" method on the default auth driver
  71. * Auth::check();
  72. * </code>
  73. */
  74. public static function __callStatic($method, $parameters)
  75. {
  76. return call_user_func_array(array(static::driver(), $method), $parameters);
  77. }
  78. }