PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/Migrations/AbstractMigration.php

https://github.com/christianjul/FLOW3-Composer
PHP | 272 lines | 87 code | 29 blank | 156 comment | 7 complexity | 40db725b9ade32fc8d71daba82188523 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Core\Migrations;
  3. /* *
  4. * This script belongs to the FLOW3 package "FLOW3". *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Core\Migrations\Tools;
  13. /**
  14. * The base class for code migrations.
  15. */
  16. abstract class AbstractMigration {
  17. const MAXIMUM_LINE_LENGTH = 79;
  18. /**
  19. * @var Manager
  20. */
  21. protected $migrationsManager;
  22. /**
  23. * @var string
  24. */
  25. protected $packageKey;
  26. /**
  27. * @var array
  28. */
  29. protected $packageData;
  30. /**
  31. * @var array
  32. */
  33. protected $operations = array('searchAndReplace' => array(), 'searchAndReplaceRegex' => array());
  34. /**
  35. * @var array
  36. */
  37. protected $notes = array();
  38. /**
  39. * @var array
  40. */
  41. protected $warnings = array();
  42. /**
  43. * @param \TYPO3\FLOW3\Core\Migrations\Manager $manager
  44. * @param string $packageKey
  45. */
  46. public function __construct(\TYPO3\FLOW3\Core\Migrations\Manager $manager, $packageKey) {
  47. $this->migrationsManager = $manager;
  48. $this->packageKey = $packageKey;
  49. }
  50. /**
  51. * Returns the package key this migration comes from.
  52. *
  53. * @return string
  54. */
  55. public function getPackageKey() {
  56. return $this->packageKey;
  57. }
  58. /**
  59. * Returns the identifier of this migration, e.g. 'FLOW3-201201261636'.
  60. *
  61. * @return string
  62. */
  63. public function getIdentifier() {
  64. return $this->packageKey . '-' . substr(get_class($this), -12);
  65. }
  66. /**
  67. * Anything that needs to be done in the migration when migrating
  68. * into the "up" direction needs to go into this method.
  69. *
  70. * It will be called by the Manager upon migration.
  71. *
  72. * @return void
  73. * @api
  74. */
  75. abstract public function up();
  76. /**
  77. * @param array $packageData
  78. * @return void
  79. */
  80. public function execute(array $packageData) {
  81. $this->packageData = $packageData;
  82. $this->applySearchAndReplaceOperations();
  83. }
  84. /**
  85. * Will show all notes and warnings accumulated.
  86. *
  87. * @return void
  88. */
  89. public function outputNotesAndWarnings() {
  90. foreach (array('notes', 'warnings') as $type) {
  91. if ($this->$type === array()) {
  92. continue;
  93. }
  94. $this->outputLine();
  95. $this->outputLine(' ' . str_repeat('-', self::MAXIMUM_LINE_LENGTH));
  96. $this->outputLine(' ' . ucfirst($type));
  97. $this->outputLine(' ' . str_repeat('-', self::MAXIMUM_LINE_LENGTH));
  98. foreach ($this->$type as $note) {
  99. $this->outputLine(' * ' . $this->wrapAndPrefix($note));
  100. }
  101. $this->outputLine(' ' . str_repeat('-', self::MAXIMUM_LINE_LENGTH));
  102. }
  103. }
  104. /**
  105. * This can be used to show a note to the developer.
  106. *
  107. * If changes cannot be automated or something needs to be
  108. * adjusted manually for other reasons, leave a note for the
  109. * developer. The notes will be shown together after migrations
  110. * have been run.
  111. *
  112. * @param string $note
  113. * @return void
  114. * @see showWarning
  115. * @api
  116. */
  117. protected function showNote($note) {
  118. $this->notes[] = $note;
  119. }
  120. /**
  121. /**
  122. * This can be used to show a warning to the developer.
  123. *
  124. * Similar to showNote, but the output is given a stronger
  125. * emphasis. The warnings will be shown together after migrations
  126. * have been run.
  127. *
  128. * @param string $warning
  129. * @return void
  130. * @see showNote
  131. * @api
  132. */
  133. protected function showWarning($warning) {
  134. $this->warnings[] = $warning;
  135. }
  136. /**
  137. * Does a simple search and replace on all (textual) files. The filter array can be
  138. * used to give file extensions to limit the operation to.
  139. *
  140. * @param string $search
  141. * @param string $replacement
  142. * @param array $filter
  143. * @return void
  144. * @api
  145. */
  146. protected function searchAndReplace($search, $replacement, array $filter = array('php', 'yaml', 'html')) {
  147. $this->operations['searchAndReplace'][] = array($search, $replacement, $filter);
  148. }
  149. /**
  150. * Does a regex search and replace on all (textual) files. The filter array can be
  151. * used to give file extensions to limit the operation to.
  152. *
  153. * The patterns are used as is, no quoting is done.
  154. *
  155. * @param string $search
  156. * @param string $replacement
  157. * @param array $filter
  158. * @return void
  159. * @api
  160. */
  161. protected function searchAndReplaceRegex($search, $replacement, array $filter = array('php', 'yaml', 'html')) {
  162. $this->operations['searchAndReplaceRegex'][] = array($search, $replacement, $filter);
  163. }
  164. /**
  165. * Rename a class from $oldName to $newName.
  166. *
  167. * This expects fully qualified class names, so proper refactoring
  168. * can be done.
  169. *
  170. * @param string $oldName
  171. * @param string $newName
  172. * @return void
  173. * @throws \LogicException
  174. */
  175. protected function renameClass($oldName, $newName) {
  176. throw new \LogicException('renameClass is not yet implemented, sorry!', 1335525001);
  177. }
  178. /**
  179. * Rename a class method.
  180. *
  181. * This expects a fully qualified class name, so proper refactoring
  182. * can be done.
  183. *
  184. * @param string $className the class that contains the method to be renamed
  185. * @param string $oldMethodName the method to be renamed
  186. * @param string $newMethodName the new method name
  187. * @param boolean $withInheritance if true, also rename method on child classes
  188. * @return void
  189. * @throws \LogicException
  190. */
  191. protected function renameMethod($className, $oldMethodName, $newMethodName, $withInheritance = TRUE) {
  192. throw new \LogicException('renameClass is not yet implemented, sorry!', 1335525001);
  193. }
  194. /**
  195. * Applies all registered searchAndReplace and searchAndReplaceRegex operations.
  196. *
  197. * @return void
  198. */
  199. protected function applySearchAndReplaceOperations() {
  200. $allPathsAndFilenames = \TYPO3\FLOW3\Utility\Files::readDirectoryRecursively($this->packageData['path'], NULL, TRUE);
  201. foreach ($this->operations['searchAndReplace'] as $operation) {
  202. foreach ($allPathsAndFilenames as $pathAndFilename) {
  203. $pathInfo = pathinfo($pathAndFilename);
  204. if (!isset($pathInfo['filename'])) continue;
  205. if (strpos($pathAndFilename, 'Migrations/Code') !== FALSE) continue;
  206. if ($operation[2] !== array()) {
  207. if (!isset($pathInfo['extension']) || !in_array($pathInfo['extension'], $operation[2], TRUE)) {
  208. continue;
  209. }
  210. }
  211. Tools::searchAndReplace($operation[0], $operation[1], $pathAndFilename);
  212. }
  213. }
  214. }
  215. /**
  216. * The given text is word-wrapped and each line after the first one is
  217. * prefixed with $prefix.
  218. *
  219. * @param string $text
  220. * @param string $prefix
  221. * @return string
  222. */
  223. protected function wrapAndPrefix($text, $prefix = ' ') {
  224. $text = explode(chr(10), wordwrap($text, self::MAXIMUM_LINE_LENGTH, chr(10), TRUE));
  225. return implode(PHP_EOL . $prefix, $text);
  226. }
  227. /**
  228. * Outputs specified text to the console window and appends a line break.
  229. *
  230. * You can specify arguments that will be passed to the text via sprintf
  231. *
  232. * @param string $text Text to output
  233. * @param array $arguments Optional arguments to use for sprintf
  234. * @return void
  235. */
  236. protected function outputLine($text = '', array $arguments = array()) {
  237. if ($arguments !== array()) {
  238. $text = vsprintf($text, $arguments);
  239. }
  240. echo $text . PHP_EOL;
  241. }
  242. }
  243. ?>