PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php

https://gitlab.com/alexandresgv/siteentec
PHP | 343 lines | 152 code | 39 blank | 152 comment | 18 complexity | b35e1db939b923e2f3b87874534882fc MD5 | raw file
  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\Manager
  28. */
  29. namespace Phinx\Migration\Manager;
  30. use Phinx\Db\Adapter\AdapterFactory;
  31. use Phinx\Db\Adapter\AdapterInterface;
  32. use Phinx\Migration\MigrationInterface;
  33. use Phinx\Seed\SeedInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class Environment
  36. {
  37. /**
  38. * @var string
  39. */
  40. protected $name;
  41. /**
  42. * @var array
  43. */
  44. protected $options;
  45. /**
  46. * @var OutputInterface
  47. */
  48. protected $output;
  49. /**
  50. * @var int
  51. */
  52. protected $currentVersion;
  53. /**
  54. * @var string
  55. */
  56. protected $schemaTableName = 'phinxlog';
  57. /**
  58. * @var AdapterInterface
  59. */
  60. protected $adapter;
  61. /**
  62. * Class Constructor.
  63. *
  64. * @param string $name Environment Name
  65. * @param array $options Options
  66. * @return Environment
  67. */
  68. public function __construct($name, $options)
  69. {
  70. $this->name = $name;
  71. $this->options = $options;
  72. }
  73. /**
  74. * Executes the specified migration on this environment.
  75. *
  76. * @param MigrationInterface $migration Migration
  77. * @param string $direction Direction
  78. * @return void
  79. */
  80. public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP)
  81. {
  82. $startTime = time();
  83. $direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
  84. $migration->setAdapter($this->getAdapter());
  85. // begin the transaction if the adapter supports it
  86. if ($this->getAdapter()->hasTransactions()) {
  87. $this->getAdapter()->beginTransaction();
  88. }
  89. // Run the migration
  90. if (method_exists($migration, MigrationInterface::CHANGE)) {
  91. if ($direction === MigrationInterface::DOWN) {
  92. // Create an instance of the ProxyAdapter so we can record all
  93. // of the migration commands for reverse playback
  94. $proxyAdapter = AdapterFactory::instance()
  95. ->getWrapper('proxy', $this->getAdapter());
  96. $migration->setAdapter($proxyAdapter);
  97. /** @noinspection PhpUndefinedMethodInspection */
  98. $migration->change();
  99. $proxyAdapter->executeInvertedCommands();
  100. $migration->setAdapter($this->getAdapter());
  101. } else {
  102. /** @noinspection PhpUndefinedMethodInspection */
  103. $migration->change();
  104. }
  105. } else {
  106. $migration->{$direction}();
  107. }
  108. // commit the transaction if the adapter supports it
  109. if ($this->getAdapter()->hasTransactions()) {
  110. $this->getAdapter()->commitTransaction();
  111. }
  112. // Record it in the database
  113. $this->getAdapter()->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time()));
  114. }
  115. /**
  116. * Executes the specified seeder on this environment.
  117. *
  118. * @param MigrationInterface $migration Migration
  119. * @param string $direction Direction
  120. * @return void
  121. */
  122. public function executeSeed(SeedInterface $seed)
  123. {
  124. $startTime = time();
  125. $seed->setAdapter($this->getAdapter());
  126. // begin the transaction if the adapter supports it
  127. if ($this->getAdapter()->hasTransactions()) {
  128. $this->getAdapter()->beginTransaction();
  129. }
  130. // Run the seeder
  131. if (method_exists($seed, SeedInterface::RUN)) {
  132. $seed->run();
  133. }
  134. // commit the transaction if the adapter supports it
  135. if ($this->getAdapter()->hasTransactions()) {
  136. $this->getAdapter()->commitTransaction();
  137. }
  138. }
  139. /**
  140. * Sets the environment's name.
  141. *
  142. * @param string $name Environment Name
  143. * @return Environment
  144. */
  145. public function setName($name)
  146. {
  147. $this->name = $name;
  148. return $this;
  149. }
  150. /**
  151. * Gets the environment name.
  152. *
  153. * @return string
  154. */
  155. public function getName()
  156. {
  157. return $this->name;
  158. }
  159. /**
  160. * Sets the environment's options.
  161. *
  162. * @param array $options Environment Options
  163. * @return Environment
  164. */
  165. public function setOptions($options)
  166. {
  167. $this->options = $options;
  168. return $this;
  169. }
  170. /**
  171. * Gets the environment's options.
  172. *
  173. * @return array
  174. */
  175. public function getOptions()
  176. {
  177. return $this->options;
  178. }
  179. /**
  180. * Sets the console output.
  181. *
  182. * @param OutputInterface $output Output
  183. * @return Environment
  184. */
  185. public function setOutput(OutputInterface $output)
  186. {
  187. $this->output = $output;
  188. return $this;
  189. }
  190. /**
  191. * Gets the console output.
  192. *
  193. * @return OutputInterface
  194. */
  195. public function getOutput()
  196. {
  197. return $this->output;
  198. }
  199. /**
  200. * Gets all migrated version numbers.
  201. *
  202. * @return array
  203. */
  204. public function getVersions()
  205. {
  206. return $this->getAdapter()->getVersions();
  207. }
  208. /**
  209. * Sets the current version of the environment.
  210. *
  211. * @param int $version Environment Version
  212. * @return Environment
  213. */
  214. public function setCurrentVersion($version)
  215. {
  216. $this->currentVersion = $version;
  217. return $this;
  218. }
  219. /**
  220. * Gets the current version of the environment.
  221. *
  222. * @return int
  223. */
  224. public function getCurrentVersion()
  225. {
  226. // We don't cache this code as the current version is pretty volatile.
  227. // TODO - that means they're no point in a setter then?
  228. // maybe we should cache and call a reset() method everytime a migration is run
  229. $versions = $this->getVersions();
  230. $version = 0;
  231. if (!empty($versions)) {
  232. $version = end($versions);
  233. }
  234. $this->setCurrentVersion($version);
  235. return $this->currentVersion;
  236. }
  237. /**
  238. * Sets the database adapter.
  239. *
  240. * @param AdapterInterface $adapter Database Adapter
  241. * @return Environment
  242. */
  243. public function setAdapter(AdapterInterface $adapter)
  244. {
  245. $this->adapter = $adapter;
  246. return $this;
  247. }
  248. /**
  249. * Gets the database adapter.
  250. *
  251. * @return AdapterInterface
  252. */
  253. public function getAdapter()
  254. {
  255. if (isset($this->adapter)) {
  256. return $this->adapter;
  257. }
  258. if (isset($this->options['connection'])) {
  259. if (!($this->options['connection'] instanceof \PDO)) {
  260. throw new \RuntimeException('The specified connection is not a PDO instance');
  261. }
  262. $this->options['adapter'] = $this->options['connection']->getAttribute(\PDO::ATTR_DRIVER_NAME);
  263. }
  264. if (!isset($this->options['adapter'])) {
  265. throw new \RuntimeException('No adapter was specified for environment: ' . $this->getName());
  266. }
  267. $adapter = AdapterFactory::instance()
  268. ->getAdapter($this->options['adapter'], $this->options);
  269. if (isset($this->options['wrapper'])) {
  270. $adapter = AdapterFactory::instance()
  271. ->getWrapper($this->options['wrapper'], $adapter);
  272. }
  273. if ($this->getOutput()) {
  274. $adapter->setOutput($this->getOutput());
  275. }
  276. // Use the TablePrefixAdapter if table prefix/suffixes are in use
  277. if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
  278. $adapter = AdapterFactory::instance()
  279. ->getWrapper('prefix', $adapter);
  280. }
  281. $this->setAdapter($adapter);
  282. return $adapter;
  283. }
  284. /**
  285. * Sets the schema table name.
  286. *
  287. * @param string $schemaTableName Schema Table Name
  288. * @return Environment
  289. */
  290. public function setSchemaTableName($schemaTableName)
  291. {
  292. $this->schemaTableName = $schemaTableName;
  293. return $this;
  294. }
  295. /**
  296. * Gets the schema table name.
  297. *
  298. * @return string
  299. */
  300. public function getSchemaTableName()
  301. {
  302. return $this->schemaTableName;
  303. }
  304. }