ProxyAdapter.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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\Db\Adapter
  28. */
  29. namespace BBDDL\Db\Adapter;
  30. use BBDDL\Db\Table;
  31. use BBDDL\Db\Table\Column;
  32. use BBDDL\Db\Table\Index;
  33. use BBDDL\Db\Table\ForeignKey;
  34. //use BBDDL\Migration\IrreversibleMigrationException;
  35. /**
  36. * Phinx Proxy Adapter.
  37. *
  38. * Used for recording migration commands to automatically reverse them.
  39. *
  40. * @author Rob Morgan <robbym@gmail.com>
  41. */
  42. class ProxyAdapter extends AdapterWrapper
  43. {
  44. /**
  45. * @var array
  46. */
  47. protected $commands;
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getAdapterType()
  52. {
  53. return 'ProxyAdapter';
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function createTable(Table $table)
  59. {
  60. $this->recordCommand('createTable', array($table->getName()));
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function renameTable($tableName, $newTableName)
  66. {
  67. $this->recordCommand('renameTable', array($tableName, $newTableName));
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function dropTable($tableName)
  73. {
  74. $this->recordCommand('dropTable', array($tableName));
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function addColumn(Table $table, Column $column)
  80. {
  81. $this->recordCommand('addColumn', array($table, $column));
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function renameColumn($tableName, $columnName, $newColumnName)
  87. {
  88. $this->recordCommand('renameColumn', array($tableName, $columnName, $newColumnName));
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function changeColumn($tableName, $columnName, Column $newColumn)
  94. {
  95. $this->recordCommand('changeColumn', array($tableName, $columnName, $newColumn));
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function dropColumn($tableName, $columnName)
  101. {
  102. $this->recordCommand('dropColumn', array($tableName, $columnName));
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function addIndex(Table $table, Index $index)
  108. {
  109. $this->recordCommand('addIndex', array($table, $index));
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function dropIndex($tableName, $columns, $options = array())
  115. {
  116. $this->recordCommand('dropIndex', array($tableName, $columns, $options));
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function dropIndexByName($tableName, $indexName)
  122. {
  123. $this->recordCommand('dropIndexByName', array($tableName, $indexName));
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function addForeignKey(Table $table, ForeignKey $foreignKey)
  129. {
  130. $this->recordCommand('addForeignKey', array($table, $foreignKey));
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function dropForeignKey($tableName, $columns, $constraint = null)
  136. {
  137. $this->recordCommand('dropForeignKey', array($columns, $constraint));
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function createDatabase($name, $options = array())
  143. {
  144. $this->recordCommand('createDatabase', array($name, $options));
  145. }
  146. /**
  147. * Record a command for execution later.
  148. *
  149. * @param string $name Command Name
  150. * @param array $arguments Command Arguments
  151. * @return void
  152. */
  153. public function recordCommand($name, $arguments)
  154. {
  155. $this->commands[] = array(
  156. 'name' => $name,
  157. 'arguments' => $arguments
  158. );
  159. }
  160. /**
  161. * Sets an array of recorded commands.
  162. *
  163. * @param array $commands Commands
  164. * @return ProxyAdapter
  165. */
  166. public function setCommands($commands)
  167. {
  168. $this->commands = $commands;
  169. return $this;
  170. }
  171. /**
  172. * Gets an array of the recorded commands.
  173. *
  174. * @return array
  175. */
  176. public function getCommands()
  177. {
  178. return $this->commands;
  179. }
  180. /**
  181. * Gets an array of the recorded commands in reverse.
  182. *
  183. * @throws IrreversibleMigrationException if a command cannot be reversed.
  184. * @return array
  185. */
  186. /*public function getInvertedCommands()
  187. {
  188. if (null === $this->getCommands()) {
  189. return array();
  190. }
  191. $invCommands = array();
  192. $supportedCommands = array(
  193. 'createTable', 'renameTable', 'addColumn',
  194. 'renameColumn', 'addIndex', 'addForeignKey'
  195. );
  196. foreach (array_reverse($this->getCommands()) as $command) {
  197. if (!in_array($command['name'], $supportedCommands)) {
  198. throw new IrreversibleMigrationException(sprintf(
  199. 'Cannot reverse a "%s" command',
  200. $command['name']
  201. ));
  202. }
  203. $invertMethod = 'invert' . ucfirst($command['name']);
  204. $invertedCommand = $this->$invertMethod($command['arguments']);
  205. $invCommands[] = array(
  206. 'name' => $invertedCommand['name'],
  207. 'arguments' => $invertedCommand['arguments']
  208. );
  209. }
  210. return $invCommands;
  211. }*/
  212. /**
  213. * Execute the recorded commands.
  214. *
  215. * @return void
  216. */
  217. public function executeCommands()
  218. {
  219. $commands = $this->getCommands();
  220. foreach ($commands as $command) {
  221. call_user_func_array(array($this->getAdapter(), $command['name']), $command['arguments']);
  222. }
  223. }
  224. /**
  225. * Execute the recorded commands in reverse.
  226. *
  227. * @return void
  228. */
  229. /*public function executeInvertedCommands()
  230. {
  231. $commands = $this->getInvertedCommands();
  232. foreach ($commands as $command) {
  233. call_user_func_array(array($this->getAdapter(), $command['name']), $command['arguments']);
  234. }
  235. }*/
  236. /**
  237. * Returns the reverse of a createTable command.
  238. *
  239. * @param array $args Method Arguments
  240. * @return array
  241. */
  242. public function invertCreateTable($args)
  243. {
  244. return array('name' => 'dropTable', 'arguments' => array($args[0]));
  245. }
  246. /**
  247. * Returns the reverse of a renameTable command.
  248. *
  249. * @param array $args Method Arguments
  250. * @return array
  251. */
  252. public function invertRenameTable($args)
  253. {
  254. return array('name' => 'renameTable', 'arguments' => array($args[1], $args[0]));
  255. }
  256. /**
  257. * Returns the reverse of a addColumn command.
  258. *
  259. * @param array $args Method Arguments
  260. * @return array
  261. */
  262. public function invertAddColumn($args)
  263. {
  264. return array('name' => 'dropColumn', 'arguments' => array($args[0]->getName(), $args[1]->getName()));
  265. }
  266. /**
  267. * Returns the reverse of a renameColumn command.
  268. *
  269. * @param array $args Method Arguments
  270. * @return array
  271. */
  272. public function invertRenameColumn($args)
  273. {
  274. return array('name' => 'renameColumn', 'arguments' => array($args[0], $args[2], $args[1]));
  275. }
  276. /**
  277. * Returns the reverse of a addIndex command.
  278. *
  279. * @param array $args Method Arguments
  280. * @return array
  281. */
  282. public function invertAddIndex($args)
  283. {
  284. return array('name' => 'dropIndex', 'arguments' => array($args[0]->getName(), $args[1]->getColumns()));
  285. }
  286. /**
  287. * Returns the reverse of a addForeignKey command.
  288. *
  289. * @param array $args Method Arguments
  290. * @return array
  291. */
  292. public function invertAddForeignKey($args)
  293. {
  294. return array('name' => 'dropForeignKey', 'arguments' => array($args[0]->getName(), $args[1]->getColumns()));
  295. }
  296. }