PasswordBrokerManager.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Illuminate\Auth\Passwords;
  3. use Illuminate\Support\Str;
  4. use InvalidArgumentException;
  5. use Illuminate\Contracts\Auth\PasswordBrokerFactory as FactoryContract;
  6. /**
  7. * @mixin \Illuminate\Contracts\Auth\PasswordBroker
  8. */
  9. class PasswordBrokerManager implements FactoryContract
  10. {
  11. /**
  12. * The application instance.
  13. *
  14. * @var \Illuminate\Foundation\Application
  15. */
  16. protected $app;
  17. /**
  18. * The array of created "drivers".
  19. *
  20. * @var array
  21. */
  22. protected $brokers = [];
  23. /**
  24. * Create a new PasswordBroker manager instance.
  25. *
  26. * @param \Illuminate\Foundation\Application $app
  27. * @return void
  28. */
  29. public function __construct($app)
  30. {
  31. $this->app = $app;
  32. }
  33. /**
  34. * Attempt to get the broker from the local cache.
  35. *
  36. * @param string|null $name
  37. * @return \Illuminate\Contracts\Auth\PasswordBroker
  38. */
  39. public function broker($name = null)
  40. {
  41. $name = $name ?: $this->getDefaultDriver();
  42. return isset($this->brokers[$name])
  43. ? $this->brokers[$name]
  44. : $this->brokers[$name] = $this->resolve($name);
  45. }
  46. /**
  47. * Resolve the given broker.
  48. *
  49. * @param string $name
  50. * @return \Illuminate\Contracts\Auth\PasswordBroker
  51. *
  52. * @throws \InvalidArgumentException
  53. */
  54. protected function resolve($name)
  55. {
  56. $config = $this->getConfig($name);
  57. if (is_null($config)) {
  58. throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
  59. }
  60. // The password broker uses a token repository to validate tokens and send user
  61. // password e-mails, as well as validating that password reset process as an
  62. // aggregate service of sorts providing a convenient interface for resets.
  63. return new PasswordBroker(
  64. $this->createTokenRepository($config),
  65. $this->app['auth']->createUserProvider($config['provider'] ?? null)
  66. );
  67. }
  68. /**
  69. * Create a token repository instance based on the given configuration.
  70. *
  71. * @param array $config
  72. * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
  73. */
  74. protected function createTokenRepository(array $config)
  75. {
  76. $key = $this->app['config']['app.key'];
  77. if (Str::startsWith($key, 'base64:')) {
  78. $key = base64_decode(substr($key, 7));
  79. }
  80. $connection = $config['connection'] ?? null;
  81. return new DatabaseTokenRepository(
  82. $this->app['db']->connection($connection),
  83. $this->app['hash'],
  84. $config['table'],
  85. $key,
  86. $config['expire']
  87. );
  88. }
  89. /**
  90. * Get the password broker configuration.
  91. *
  92. * @param string $name
  93. * @return array
  94. */
  95. protected function getConfig($name)
  96. {
  97. return $this->app['config']["auth.passwords.{$name}"];
  98. }
  99. /**
  100. * Get the default password broker name.
  101. *
  102. * @return string
  103. */
  104. public function getDefaultDriver()
  105. {
  106. return $this->app['config']['auth.defaults.passwords'];
  107. }
  108. /**
  109. * Set the default password broker name.
  110. *
  111. * @param string $name
  112. * @return void
  113. */
  114. public function setDefaultDriver($name)
  115. {
  116. $this->app['config']['auth.defaults.passwords'] = $name;
  117. }
  118. /**
  119. * Dynamically call the default driver instance.
  120. *
  121. * @param string $method
  122. * @param array $parameters
  123. * @return mixed
  124. */
  125. public function __call($method, $parameters)
  126. {
  127. return $this->broker()->{$method}(...$parameters);
  128. }
  129. }