PasswordHasherFactory.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Builds password hashing objects
  4. *
  5. * Backported from 3.x
  6. */
  7. class PasswordHasherFactory {
  8. /**
  9. * Returns password hasher object out of a hasher name or a configuration array
  10. *
  11. * @param string|array $passwordHasher Name of the password hasher or an array with
  12. * at least the key `className` set to the name of the class to use
  13. * @return \Cake\Auth\AbstractPasswordHasher Password hasher instance
  14. * @throws \RuntimeException If password hasher class not found or
  15. * it does not extend Cake\Auth\AbstractPasswordHasher
  16. */
  17. public static function build($passwordHasher) {
  18. $config = [];
  19. if (is_string($passwordHasher)) {
  20. $class = $passwordHasher;
  21. } else {
  22. $class = $passwordHasher['className'];
  23. $config = $passwordHasher;
  24. unset($config['className']);
  25. }
  26. list($plugin, $class) = pluginSplit($class, true);
  27. $className = $class . 'PasswordHasher';
  28. App::uses($className, $plugin . 'Controller/Component/Auth');
  29. if (!class_exists($className)) {
  30. throw new CakeException(sprintf('Password hasher class "%s" was not found.', $class));
  31. }
  32. if (!is_subclass_of($className, 'AbstractPasswordHasher')) {
  33. throw new CakeException('Password hasher must extend AbstractPasswordHasher class.');
  34. }
  35. return new $className($config);
  36. }
  37. }