PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/migrations/src/Shell/Task/MigrationSnapshotTask.php

https://gitlab.com/alexandresgv/siteentec
PHP | 277 lines | 159 code | 33 blank | 85 comment | 18 complexity | 3ed70efbe6575e124c0b3d986a900a77 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Migrations\Shell\Task;
  15. use Cake\Core\Configure;
  16. use Cake\Core\Plugin;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Event\Event;
  19. use Cake\Event\EventManager;
  20. use Cake\Filesystem\Folder;
  21. use Cake\ORM\TableRegistry;
  22. /**
  23. * Task class for generating migration snapshot files.
  24. */
  25. class MigrationSnapshotTask extends SimpleMigrationTask
  26. {
  27. /**
  28. * Tables to skip
  29. *
  30. * @var array
  31. */
  32. public $skipTables = ['phinxlog'];
  33. /**
  34. * Regex of Table name to skip
  35. *
  36. * @var string
  37. */
  38. public $skipTablesRegex = '_phinxlog';
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function bake($name)
  43. {
  44. $collection = $this->getCollection($this->connection);
  45. EventManager::instance()->on('Bake.initialize', function (Event $event) use ($collection) {
  46. $event->subject->loadHelper('Migrations.Migration', [
  47. 'collection' => $collection
  48. ]);
  49. });
  50. return parent::bake($name);
  51. }
  52. /**
  53. * After the file has been successfully created, we mark the newly
  54. * created snapshot as applied
  55. *
  56. * {@inheritDoc}
  57. */
  58. public function createFile($path, $contents)
  59. {
  60. $createFile = parent::createFile($path, $contents);
  61. if ($createFile) {
  62. $this->markSnapshotApplied($path);
  63. }
  64. return $createFile;
  65. }
  66. /**
  67. * Will mark a snapshot created, the snapshot being identified by its
  68. * full file path.
  69. *
  70. * @param string $path Path to the newly created snapshot
  71. * @return void
  72. */
  73. protected function markSnapshotApplied($path)
  74. {
  75. $fileName = pathinfo($path, PATHINFO_FILENAME);
  76. list($version, ) = explode('_', $fileName, 2);
  77. $dispatchCommand = 'migrations mark_migrated -t ' . $version . ' -o';
  78. if (!empty($this->params['connection'])) {
  79. $dispatchCommand .= ' -c ' . $this->params['connection'];
  80. }
  81. if (!empty($this->params['plugin'])) {
  82. $dispatchCommand .= ' -p ' . $this->params['plugin'];
  83. }
  84. $this->_io->out('Marking the snapshot ' . $fileName . ' as migrated...');
  85. $this->dispatchShell($dispatchCommand);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function template()
  91. {
  92. return 'Migrations.config/snapshot';
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function templateData()
  98. {
  99. $namespace = Configure::read('App.namespace');
  100. $pluginPath = '';
  101. if ($this->plugin) {
  102. $namespace = $this->_pluginNamespace($this->plugin);
  103. $pluginPath = $this->plugin . '.';
  104. }
  105. $collection = $this->getCollection($this->connection);
  106. $tables = $collection->listTables();
  107. if ($this->params['require-table'] === true || $this->plugin) {
  108. $tableNamesInModel = $this->getTableNames($this->plugin);
  109. foreach ($tableNamesInModel as $num => $table) {
  110. if (!in_array($tables[$num], $tables)) {
  111. unset($tableNamesInModel[$num]);
  112. }
  113. }
  114. $tables = $tableNamesInModel;
  115. } else {
  116. foreach ($tables as $num => $table) {
  117. if ((in_array($table, $this->skipTables)) || (strpos($table, $this->skipTablesRegex) !== false)) {
  118. unset($tables[$num]);
  119. continue;
  120. }
  121. }
  122. }
  123. $autoId = true;
  124. if (isset($this->params['disable-autoid'])) {
  125. $autoId = !$this->params['disable-autoid'];
  126. }
  127. return [
  128. 'plugin' => $this->plugin,
  129. 'pluginPath' => $pluginPath,
  130. 'namespace' => $namespace,
  131. 'collection' => $collection,
  132. 'tables' => $tables,
  133. 'action' => 'create_table',
  134. 'name' => $this->BakeTemplate->viewVars['name'],
  135. 'autoId' => $autoId
  136. ];
  137. }
  138. /**
  139. * Get a collection from a database
  140. *
  141. * @param string $connection Database connection name.
  142. * @return \Cake\Database\Schema\Collection
  143. */
  144. public function getCollection($connection)
  145. {
  146. $connection = ConnectionManager::get($connection);
  147. return $connection->schemaCollection();
  148. }
  149. /**
  150. * To check if a Table Model is to be added in the migration file
  151. *
  152. * @param string $tableName Table name in underscore case.
  153. * @param string|null $pluginName Plugin name if exists.
  154. * @deprecated Will be removed in the next version
  155. * @return bool True if the model is to be added.
  156. */
  157. public function tableToAdd($tableName, $pluginName = null)
  158. {
  159. return true;
  160. }
  161. /**
  162. * Gets list Tables Names
  163. *
  164. * @param string|null $pluginName Plugin name if exists.
  165. * @return array
  166. */
  167. public function getTableNames($pluginName = null)
  168. {
  169. if ($pluginName !== null && !Plugin::loaded($pluginName)) {
  170. return [];
  171. }
  172. $list = [];
  173. $tables = $this->findTables($pluginName);
  174. foreach ($tables as $num => $table) {
  175. $list = array_merge($list, $this->fetchTableName($table, $pluginName));
  176. }
  177. return array_unique($list);
  178. }
  179. /**
  180. * Find Table Class
  181. *
  182. * @param string $pluginName Plugin name if exists.
  183. * @return array
  184. */
  185. public function findTables($pluginName = null)
  186. {
  187. $path = 'Model' . DS . 'Table' . DS;
  188. if ($pluginName) {
  189. $path = Plugin::path($pluginName) . 'src' . DS . $path;
  190. } else {
  191. $path = APP . $path;
  192. }
  193. if (!is_dir($path)) {
  194. return false;
  195. }
  196. $tableDir = new Folder($path);
  197. $tableDir = $tableDir->find('.*\.php');
  198. return $tableDir;
  199. }
  200. /**
  201. * fetch TableName From Table Object
  202. *
  203. * @param string $className Name of Table Class.
  204. * @param string|null $pluginName Plugin name if exists.
  205. * @return array
  206. */
  207. public function fetchTableName($className, $pluginName = null)
  208. {
  209. $tables = [];
  210. $className = str_replace('Table.php', '', $className);
  211. if ($pluginName !== null) {
  212. $className = $pluginName . '.' . $className;
  213. }
  214. $table = TableRegistry::get($className);
  215. foreach ($table->associations()->keys() as $key) {
  216. if ($table->associations()->get($key)->type() === 'belongsToMany') {
  217. $tables[] = $table->associations()->get($key)->_junctionTableName();
  218. }
  219. }
  220. $tables[] = $table->table();
  221. return $tables;
  222. }
  223. /**
  224. * Gets the option parser instance and configures it.
  225. *
  226. * @return \Cake\Console\ConsoleOptionParser
  227. */
  228. public function getOptionParser()
  229. {
  230. $parser = parent::getOptionParser();
  231. $parser->description(
  232. 'Bake migration snapshot class.'
  233. )->addOption('require-table', [
  234. 'boolean' => true,
  235. 'default' => false,
  236. 'help' => 'If require-table is set to true, check also that the table class exists.'
  237. ])->addOption('disable-autoid', [
  238. 'boolean' => true,
  239. 'default' => false,
  240. 'help' => 'Disable phinx behavior of automatically adding an id field.'
  241. ]);
  242. return $parser;
  243. }
  244. }