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