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: /**
27: * Returns password hasher object out of a hasher name or a configuration array
28: *
29: * @param string|array $passwordHasher Name of the password hasher or an array with
30: * at least the key `className` set to the name of the class to use
31: * @return \Cake\Auth\AbstractPasswordHasher Password hasher instance
32: * @throws \RuntimeException If password hasher class not found or
33: * it does not extend Cake\Auth\AbstractPasswordHasher
34: */
35: public static function build($passwordHasher)
36: {
37: $config = [];
38: if (is_string($passwordHasher)) {
39: $class = $passwordHasher;
40: } else {
41: $class = $passwordHasher['className'];
42: $config = $passwordHasher;
43: unset($config['className']);
44: }
45:
46: $className = App::className($class, 'Auth', 'PasswordHasher');
47: if ($className === false) {
48: throw new RuntimeException(sprintf('Password hasher class "%s" was not found.', $class));
49: }
50:
51: $hasher = new $className($config);
52: if (!($hasher instanceof AbstractPasswordHasher)) {
53: throw new RuntimeException('Password hasher must extend AbstractPasswordHasher class.');
54: }
55:
56: return $hasher;
57: }
58: }
59: