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\Datasource;
16:
17: use Cake\Core\App;
18: use Cake\Core\ObjectRegistry;
19: use Cake\Datasource\Exception\MissingDatasourceException;
20:
21: /**
22: * A registry object for connection instances.
23: *
24: * @see \Cake\Datasource\ConnectionManager
25: */
26: class ConnectionRegistry extends ObjectRegistry
27: {
28:
29: /**
30: * Resolve a datasource classname.
31: *
32: * Part of the template method for Cake\Core\ObjectRegistry::load()
33: *
34: * @param string $class Partial classname to resolve.
35: * @return string|false Either the correct classname or false.
36: */
37: protected function _resolveClassName($class)
38: {
39: if (is_object($class)) {
40: return $class;
41: }
42:
43: return App::className($class, 'Datasource');
44: }
45:
46: /**
47: * Throws an exception when a datasource is missing
48: *
49: * Part of the template method for Cake\Core\ObjectRegistry::load()
50: *
51: * @param string $class The classname that is missing.
52: * @param string $plugin The plugin the datasource is missing in.
53: * @return void
54: * @throws \Cake\Datasource\Exception\MissingDatasourceException
55: */
56: protected function _throwMissingClassError($class, $plugin)
57: {
58: throw new MissingDatasourceException([
59: 'class' => $class,
60: 'plugin' => $plugin,
61: ]);
62: }
63:
64: /**
65: * Create the connection object with the correct settings.
66: *
67: * Part of the template method for Cake\Core\ObjectRegistry::load()
68: *
69: * If a callable is passed as first argument, The returned value of this
70: * function will be the result of the callable.
71: *
72: * @param string|object|callable $class The classname or object to make.
73: * @param string $alias The alias of the object.
74: * @param array $settings An array of settings to use for the datasource.
75: * @return object A connection with the correct settings.
76: */
77: protected function _create($class, $alias, $settings)
78: {
79: if (is_callable($class)) {
80: return $class($alias);
81: }
82:
83: if (is_object($class)) {
84: return $class;
85: }
86:
87: unset($settings['className']);
88:
89: return new $class($settings);
90: }
91:
92: /**
93: * Remove a single adapter from the registry.
94: *
95: * @param string $name The adapter name.
96: * @return void
97: */
98: public function unload($name)
99: {
100: unset($this->_loaded[$name]);
101: }
102: }
103: