AbstractCommand.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TopThink [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2015 http://www.topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: zhangyajun <448901948@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace think\migration\command;
  10. use InvalidArgumentException;
  11. use think\console\Command;
  12. use think\console\Input;
  13. use think\console\input\Option as InputOption;
  14. use think\console\Output;
  15. use Phinx\Config;
  16. use Phinx\Migration\Manager;
  17. use Phinx\Db\Adapter\AdapterInterface;
  18. abstract class AbstractCommand extends Command
  19. {
  20. /**
  21. * The location of the default migration template.
  22. */
  23. const DEFAULT_MIGRATION_TEMPLATE = '/../../phinx/src/Phinx/Migration/Migration.template.php.dist';
  24. /**
  25. * The location of the default seed template.
  26. */
  27. const DEFAULT_SEED_TEMPLATE = '/../../phinx/src/Phinx/Seed/Seed.template.php.dist';
  28. /**
  29. * @var Config
  30. */
  31. protected $config;
  32. /**
  33. * @var AdapterInterface
  34. */
  35. protected $adapter;
  36. /**
  37. * @var Manager
  38. */
  39. protected $manager;
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function configure()
  44. {
  45. $this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load');
  46. }
  47. /**
  48. * Bootstrap Phinx.
  49. *
  50. * @param Input $input
  51. * @param Output $output
  52. * @return void
  53. */
  54. public function bootstrap(Input $input, Output $output)
  55. {
  56. if (!$this->getConfig()) {
  57. $this->loadConfig($input, $output);
  58. }
  59. $this->loadManager($output);
  60. // report the paths
  61. $output->writeln('<info>using migration path</info> ' . $this->getConfig()->getMigrationPath());
  62. try {
  63. $output->writeln('<info>using seed path</info> ' . $this->getConfig()->getSeedPath());
  64. } catch (\UnexpectedValueException $e) {
  65. // do nothing as seeds are optional
  66. }
  67. }
  68. /**
  69. * Sets the config.
  70. *
  71. * @param Config $config
  72. * @return AbstractCommand
  73. */
  74. public function setConfig(Config $config)
  75. {
  76. $this->config = $config;
  77. return $this;
  78. }
  79. /**
  80. * Gets the config.
  81. *
  82. * @return Config
  83. */
  84. public function getConfig()
  85. {
  86. return $this->config;
  87. }
  88. /**
  89. * Sets the database adapter.
  90. *
  91. * @param AdapterInterface $adapter
  92. * @return AbstractCommand
  93. */
  94. public function setAdapter(AdapterInterface $adapter)
  95. {
  96. $this->adapter = $adapter;
  97. return $this;
  98. }
  99. /**
  100. * Gets the database adapter.
  101. *
  102. * @return AdapterInterface
  103. */
  104. public function getAdapter()
  105. {
  106. return $this->adapter;
  107. }
  108. /**
  109. * Sets the migration manager.
  110. *
  111. * @param Manager $manager
  112. * @return AbstractCommand
  113. */
  114. public function setManager(Manager $manager)
  115. {
  116. $this->manager = $manager;
  117. return $this;
  118. }
  119. /**
  120. * Gets the migration manager.
  121. *
  122. * @return Manager
  123. */
  124. public function getManager()
  125. {
  126. return $this->manager;
  127. }
  128. /**
  129. * Parse the config file and load it into the config object
  130. *
  131. * @param Input $input
  132. * @param Output $output
  133. * @throws \InvalidArgumentException
  134. * @return void
  135. */
  136. protected function loadConfig(Input $input, Output $output)
  137. {
  138. $configFile = $input->getOption('configuration');
  139. if (null === $configFile || false === $configFile) {
  140. $configFile = APP_PATH . 'database' . EXT;
  141. }
  142. if (!is_file($configFile)) {
  143. throw new InvalidArgumentException();
  144. }
  145. $output->writeln('<info>using config file</info> .' . str_replace(getcwd(), '', realpath($configFile)));
  146. $config = new Config($configFile);
  147. $this->setConfig($config);
  148. }
  149. /**
  150. * Load the migrations manager and inject the config
  151. *
  152. * @param Output $output
  153. * @return void
  154. */
  155. protected function loadManager(Output $output)
  156. {
  157. if (null === $this->getManager()) {
  158. $manager = new Manager($this->getConfig(), $output);
  159. $this->setManager($manager);
  160. }
  161. }
  162. /**
  163. * Verify that the migration directory exists and is writable.
  164. *
  165. * @param $path
  166. */
  167. protected function verifyMigrationDirectory($path)
  168. {
  169. if (!is_dir($path)) {
  170. throw new InvalidArgumentException(sprintf(
  171. 'Migration directory "%s" does not exist',
  172. $path
  173. ));
  174. }
  175. if (!is_writable($path)) {
  176. throw new InvalidArgumentException(sprintf(
  177. 'Migration directory "%s" is not writable',
  178. $path
  179. ));
  180. }
  181. }
  182. /**
  183. * Verify that the seed directory exists and is writable.
  184. *
  185. * @param $path
  186. */
  187. protected function verifySeedDirectory($path)
  188. {
  189. if (!is_dir($path)) {
  190. throw new InvalidArgumentException(sprintf(
  191. 'Seed directory "%s" does not exist',
  192. $path
  193. ));
  194. }
  195. if (!is_writable($path)) {
  196. throw new InvalidArgumentException(sprintf(
  197. 'Seed directory "%s" is not writable',
  198. $path
  199. ));
  200. }
  201. }
  202. /**
  203. * Returns the migration template filename.
  204. *
  205. * @return string
  206. */
  207. protected function getMigrationTemplateFilename()
  208. {
  209. return __DIR__ . self::DEFAULT_MIGRATION_TEMPLATE;
  210. }
  211. /**
  212. * Returns the seed template filename.
  213. *
  214. * @return string
  215. */
  216. protected function getSeedTemplateFilename()
  217. {
  218. return __DIR__ . self::DEFAULT_SEED_TEMPLATE;
  219. }
  220. }