PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/robmorgan/phinx/src/Phinx/Console/Command/AbstractCommand.php

https://gitlab.com/alexandresgv/siteentec
PHP | 341 lines | 167 code | 36 blank | 138 comment | 12 complexity | 3ccaab690f633689f93be6bce8e1ce76 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\Console
  28. */
  29. namespace Phinx\Console\Command;
  30. use Symfony\Component\Config\FileLocator;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Phinx\Config\Config;
  36. use Phinx\Config\ConfigInterface;
  37. use Phinx\Migration\Manager;
  38. use Phinx\Db\Adapter\AdapterInterface;
  39. /**
  40. * Abstract command, contains bootstrapping info
  41. *
  42. * @author Rob Morgan <robbym@gmail.com>
  43. */
  44. abstract class AbstractCommand extends Command
  45. {
  46. /**
  47. * The location of the default migration template.
  48. */
  49. const DEFAULT_MIGRATION_TEMPLATE = '/../../Migration/Migration.template.php.dist';
  50. /**
  51. * The location of the default seed template.
  52. */
  53. const DEFAULT_SEED_TEMPLATE = '/../../Seed/Seed.template.php.dist';
  54. /**
  55. * @var ConfigInterface
  56. */
  57. protected $config;
  58. /**
  59. * @var AdapterInterface
  60. */
  61. protected $adapter;
  62. /**
  63. * @var Manager
  64. */
  65. protected $manager;
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function configure()
  70. {
  71. $this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load');
  72. $this->addOption('--parser', '-p', InputOption::VALUE_REQUIRED, 'Parser used to read the config file. Defaults to YAML');
  73. }
  74. /**
  75. * Bootstrap Phinx.
  76. *
  77. * @param InputInterface $input
  78. * @param OutputInterface $output
  79. * @return void
  80. */
  81. public function bootstrap(InputInterface $input, OutputInterface $output)
  82. {
  83. if (!$this->getConfig()) {
  84. $this->loadConfig($input, $output);
  85. }
  86. $this->loadManager($output);
  87. // report the paths
  88. $output->writeln('<info>using migration path</info> ' . $this->getConfig()->getMigrationPath());
  89. try {
  90. $output->writeln('<info>using seed path</info> ' . $this->getConfig()->getSeedPath());
  91. } catch (\UnexpectedValueException $e) {
  92. // do nothing as seeds are optional
  93. }
  94. }
  95. /**
  96. * Sets the config.
  97. *
  98. * @param ConfigInterface $config
  99. * @return AbstractCommand
  100. */
  101. public function setConfig(ConfigInterface $config)
  102. {
  103. $this->config = $config;
  104. return $this;
  105. }
  106. /**
  107. * Gets the config.
  108. *
  109. * @return Config
  110. */
  111. public function getConfig()
  112. {
  113. return $this->config;
  114. }
  115. /**
  116. * Sets the database adapter.
  117. *
  118. * @param AdapterInterface $adapter
  119. * @return AbstractCommand
  120. */
  121. public function setAdapter(AdapterInterface $adapter)
  122. {
  123. $this->adapter = $adapter;
  124. return $this;
  125. }
  126. /**
  127. * Gets the database adapter.
  128. *
  129. * @return AdapterInterface
  130. */
  131. public function getAdapter()
  132. {
  133. return $this->adapter;
  134. }
  135. /**
  136. * Sets the migration manager.
  137. *
  138. * @param Manager $manager
  139. * @return AbstractCommand
  140. */
  141. public function setManager(Manager $manager)
  142. {
  143. $this->manager = $manager;
  144. return $this;
  145. }
  146. /**
  147. * Gets the migration manager.
  148. *
  149. * @return Manager
  150. */
  151. public function getManager()
  152. {
  153. return $this->manager;
  154. }
  155. /**
  156. * Returns config file path
  157. *
  158. * @param InputInterface $input
  159. * @return string
  160. */
  161. protected function locateConfigFile(InputInterface $input)
  162. {
  163. $configFile = $input->getOption('configuration');
  164. $useDefault = false;
  165. if (null === $configFile || false === $configFile) {
  166. $useDefault = true;
  167. }
  168. $cwd = getcwd();
  169. // locate the phinx config file (default: phinx.yml)
  170. // TODO - In future walk the tree in reverse (max 10 levels)
  171. $locator = new FileLocator(array(
  172. $cwd . DIRECTORY_SEPARATOR
  173. ));
  174. if (!$useDefault) {
  175. // Locate() throws an exception if the file does not exist
  176. return $locator->locate($configFile, $cwd, $first = true);
  177. }
  178. $possibleConfigFiles = array('phinx.php', 'phinx.json', 'phinx.yml');
  179. foreach ($possibleConfigFiles as $configFile) {
  180. try {
  181. return $locator->locate($configFile, $cwd, $first = true);
  182. } catch (\InvalidArgumentException $exception) {
  183. $lastException = $exception;
  184. }
  185. }
  186. throw $lastException;
  187. }
  188. /**
  189. * Parse the config file and load it into the config object
  190. *
  191. * @param InputInterface $input
  192. * @param OutputInterface $output
  193. * @throws \InvalidArgumentException
  194. * @return void
  195. */
  196. protected function loadConfig(InputInterface $input, OutputInterface $output)
  197. {
  198. $configFilePath = $this->locateConfigFile($input);
  199. $output->writeln('<info>using config file</info> .' . str_replace(getcwd(), '', realpath($configFilePath)));
  200. $parser = $input->getOption('parser');
  201. // If no parser is specified try to determine the correct one from the file extension. Defaults to YAML
  202. if (null === $parser) {
  203. $extension = pathinfo($configFilePath, PATHINFO_EXTENSION);
  204. switch (strtolower($extension)) {
  205. case 'json':
  206. $parser = 'json';
  207. break;
  208. case 'php':
  209. $parser = 'php';
  210. break;
  211. case 'yml':
  212. default:
  213. $parser = 'yaml';
  214. }
  215. }
  216. switch (strtolower($parser)) {
  217. case 'json':
  218. $config = Config::fromJson($configFilePath);
  219. break;
  220. case 'php':
  221. $config = Config::fromPhp($configFilePath);
  222. break;
  223. case 'yaml':
  224. $config = Config::fromYaml($configFilePath);
  225. break;
  226. default:
  227. throw new \InvalidArgumentException(sprintf('\'%s\' is not a valid parser.', $parser));
  228. }
  229. $output->writeln('<info>using config parser</info> ' . $parser);
  230. $this->setConfig($config);
  231. }
  232. /**
  233. * Load the migrations manager and inject the config
  234. *
  235. * @param OutputInterface $output
  236. * @return void
  237. */
  238. protected function loadManager(OutputInterface $output)
  239. {
  240. if (null === $this->getManager()) {
  241. $manager = new Manager($this->getConfig(), $output);
  242. $this->setManager($manager);
  243. }
  244. }
  245. /**
  246. * Verify that the migration directory exists and is writable.
  247. *
  248. * @throws InvalidArgumentException
  249. * @return void
  250. */
  251. protected function verifyMigrationDirectory($path)
  252. {
  253. if (!is_dir($path)) {
  254. throw new \InvalidArgumentException(sprintf(
  255. 'Migration directory "%s" does not exist',
  256. $path
  257. ));
  258. }
  259. if (!is_writable($path)) {
  260. throw new \InvalidArgumentException(sprintf(
  261. 'Migration directory "%s" is not writable',
  262. $path
  263. ));
  264. }
  265. }
  266. /**
  267. * Verify that the seed directory exists and is writable.
  268. *
  269. * @throws InvalidArgumentException
  270. * @return void
  271. */
  272. protected function verifySeedDirectory($path)
  273. {
  274. if (!is_dir($path)) {
  275. throw new \InvalidArgumentException(sprintf(
  276. 'Seed directory "%s" does not exist',
  277. $path
  278. ));
  279. }
  280. if (!is_writable($path)) {
  281. throw new \InvalidArgumentException(sprintf(
  282. 'Seed directory "%s" is not writable',
  283. $path
  284. ));
  285. }
  286. }
  287. /**
  288. * Returns the migration template filename.
  289. *
  290. * @return string
  291. */
  292. protected function getMigrationTemplateFilename()
  293. {
  294. return __DIR__ . self::DEFAULT_MIGRATION_TEMPLATE;
  295. }
  296. /**
  297. * Returns the seed template filename.
  298. *
  299. * @return string
  300. */
  301. protected function getSeedTemplateFilename()
  302. {
  303. return __DIR__ . self::DEFAULT_SEED_TEMPLATE;
  304. }
  305. }