AdapterFactory.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Phinx
  4. *
  5. * (The MIT license)
  6. * Copyright (c) 2015 Rob Morgan
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated * documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. *
  26. * @package Phinx
  27. * @subpackage Phinx\Migration
  28. */
  29. namespace BBDDL\Db\Adapter;
  30. use BBDDL\Db\Adapter\AdapterInterface;
  31. /**
  32. * Adapter factory and registry.
  33. *
  34. * Used for registering adapters and creating instances of adapters.
  35. *
  36. * @author Woody Gilk <woody.gilk@gmail.com>
  37. */
  38. class AdapterFactory
  39. {
  40. /**
  41. * @var AdapterFactory
  42. */
  43. protected static $instance;
  44. /**
  45. * Get the factory singleton instance.
  46. *
  47. * @return AdapterFactory
  48. */
  49. public static function instance()
  50. {
  51. if (!static::$instance) {
  52. static::$instance = new static();
  53. }
  54. return static::$instance;
  55. }
  56. /**
  57. * Class map of database adapters, indexed by PDO::ATTR_DRIVER_NAME.
  58. *
  59. * @var array
  60. */
  61. protected $adapters = array(
  62. 'mysql' => 'Phinx\Db\Adapter\MysqlAdapter',
  63. 'pgsql' => 'Phinx\Db\Adapter\PostgresAdapter',
  64. 'sqlite' => 'Phinx\Db\Adapter\SQLiteAdapter',
  65. 'sqlsrv' => 'Phinx\Db\Adapter\SqlServerAdapter',
  66. );
  67. /**
  68. * Class map of adapters wrappers, indexed by name.
  69. *
  70. * @var array
  71. */
  72. protected $wrappers = array(
  73. 'prefix' => 'Phinx\Db\Adapter\TablePrefixAdapter',
  74. 'proxy' => 'Phinx\Db\Adapter\ProxyAdapter',
  75. );
  76. /**
  77. * Add or replace an adapter with a fully qualified class name.
  78. *
  79. * @throws \RuntimeException
  80. * @param string $name
  81. * @param string $class
  82. * @return $this
  83. */
  84. public function registerAdapter($name, $class)
  85. {
  86. if (!is_subclass_of($class, 'Phinx\Db\Adapter\AdapterInterface')) {
  87. throw new \RuntimeException(sprintf(
  88. 'Adapter class "%s" must implement Phinx\\Db\\Adapter\\AdapterInterface',
  89. $class
  90. ));
  91. }
  92. $this->adapters[$name] = $class;
  93. return $this;
  94. }
  95. /**
  96. * Get an adapter class by name.
  97. *
  98. * @throws \RuntimeException
  99. * @param string $name
  100. * @return string
  101. */
  102. protected function getClass($name)
  103. {
  104. if (empty($this->adapters[$name])) {
  105. throw new \RuntimeException(sprintf(
  106. 'Adapter "%s" has not been registered',
  107. $name
  108. ));
  109. }
  110. return $this->adapters[$name];
  111. }
  112. /**
  113. * Get an adapter instance by name.
  114. *
  115. * @param string $name
  116. * @param array $options
  117. * @return AdapterInterface
  118. */
  119. public function getAdapter($name, array $options)
  120. {
  121. $class = $this->getClass($name);
  122. return new $class($options);
  123. }
  124. /**
  125. * Add or replace a wrapper with a fully qualified class name.
  126. *
  127. * @throws \RuntimeException
  128. * @param string $name
  129. * @param string $class
  130. * @return $this
  131. */
  132. public function registerWrapper($name, $class)
  133. {
  134. if (!is_subclass_of($class, 'Phinx\Db\Adapter\WrapperInterface')) {
  135. throw new \RuntimeException(sprintf(
  136. 'Wrapper class "%s" must be implement Phinx\\Db\\Adapter\\WrapperInterface',
  137. $class
  138. ));
  139. }
  140. $this->wrappers[$name] = $class;
  141. return $this;
  142. }
  143. /**
  144. * Get a wrapper class by name.
  145. *
  146. * @throws \RuntimeException
  147. * @param string $name
  148. * @return string
  149. */
  150. protected function getWrapperClass($name)
  151. {
  152. if (empty($this->wrappers[$name])) {
  153. throw new \RuntimeException(sprintf(
  154. 'Wrapper "%s" has not been registered',
  155. $name
  156. ));
  157. }
  158. return $this->wrappers[$name];
  159. }
  160. /**
  161. * Get a wrapper instance by name.
  162. *
  163. * @param string $name
  164. * @param AdapterInterface $adapter
  165. * @return AdapterInterface
  166. */
  167. public function getWrapper($name, AdapterInterface $adapter)
  168. {
  169. $class = $this->getWrapperClass($name);
  170. return new $class($adapter);
  171. }
  172. }