ConnectionRegistry.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v3.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Database;
  18. use Cake\Core\App;
  19. use Cake\Database\Connection;
  20. use Cake\Database\Exception\MissingDriverException;
  21. use Cake\Error;
  22. use Cake\Utility\ObjectRegistry;
  23. /**
  24. * A registry object for connection instances.
  25. *
  26. * @see Cake\Database\ConnectionManager
  27. */
  28. class ConnectionRegistry extends ObjectRegistry {
  29. /**
  30. * Resolve a driver classname.
  31. *
  32. * Part of the template method for Cake\Utility\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. if (is_object($class)) {
  39. return $class;
  40. }
  41. return App::classname($class, 'Database/Driver');
  42. }
  43. /**
  44. * Throws an exception when a driver is missing
  45. *
  46. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  47. *
  48. * @param string $class The classname that is missing.
  49. * @param string $plugin The plugin the driver is missing in.
  50. * @throws Cake\Database\Exception\MissingDriverException
  51. */
  52. protected function _throwMissingClassError($class, $plugin) {
  53. throw new MissingDriverException([
  54. 'class' => $class,
  55. 'plugin' => $plugin,
  56. ]);
  57. }
  58. /**
  59. * Create the connection object with the correct driver.
  60. *
  61. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  62. *
  63. * @param string|Driver $class The classname or object to make.
  64. * @param array $settings An array of settings to use for the driver.
  65. * @return Connection A connection with the correct driver.
  66. */
  67. protected function _create($class, $settings) {
  68. if (is_object($class)) {
  69. $instance = $class;
  70. }
  71. unset($settings['className']);
  72. if (!isset($instance)) {
  73. $instance = new $class($settings);
  74. }
  75. $settings['datasource'] = $instance;
  76. return new Connection($settings);
  77. }
  78. /**
  79. * Remove a single adapter from the registry.
  80. *
  81. * @param string $name The adapter name.
  82. * @return void
  83. */
  84. public function unload($name) {
  85. unset($this->_loaded[$name]);
  86. }
  87. }