1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Auth;
16:
17: use Cake\Core\App;
18: use RuntimeException;
19:
20: /**
21: * Builds password hashing objects
22: */
23: class PasswordHasherFactory
24: {
25: /**
26: * Returns password hasher object out of a hasher name or a configuration array
27: *
28: * @param string|array $passwordHasher Name of the password hasher or an array with
29: * at least the key `className` set to the name of the class to use
30: * @return \Cake\Auth\AbstractPasswordHasher Password hasher instance
31: * @throws \RuntimeException If password hasher class not found or
32: * it does not extend Cake\Auth\AbstractPasswordHasher
33: */
34: public static function build($passwordHasher)
35: {
36: $config = [];
37: if (is_string($passwordHasher)) {
38: $class = $passwordHasher;
39: } else {
40: $class = $passwordHasher['className'];
41: $config = $passwordHasher;
42: unset($config['className']);
43: }
44:
45: $className = App::className($class, 'Auth', 'PasswordHasher');
46: if ($className === false) {
47: throw new RuntimeException(sprintf('Password hasher class "%s" was not found.', $class));
48: }
49:
50: $hasher = new $className($config);
51: if (!($hasher instanceof AbstractPasswordHasher)) {
52: throw new RuntimeException('Password hasher must extend AbstractPasswordHasher class.');
53: }
54:
55: return $hasher;
56: }
57: }
58: