ConnectionRegistry.php 2.9 KB

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