PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Phpmig/Console/Command/InitCommand.php

http://github.com/davedevelopment/phpmig
PHP | 143 lines | 76 code | 25 blank | 42 comment | 6 complexity | 0075976383ff9d649b2712f5afeda312 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Phpmig
  4. * @subpackage Phpmig\Console
  5. */
  6. namespace Phpmig\Console\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. /**
  10. * This file is part of phpmig
  11. *
  12. * Copyright (c) 2011 Dave Marshall <dave.marshall@atstsolutuions.co.uk>
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. /**
  18. * Init command
  19. *
  20. * @author Dave Marshall <david.marshall@bskyb.com>
  21. */
  22. class InitCommand extends AbstractCommand
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function configure()
  28. {
  29. $this->setName('init')
  30. ->setDescription('Initialise this directory for use with phpmig')
  31. ->setHelp(<<<EOT
  32. The <info>init</info> command creates a skeleton bootstrap file and a migrations directory
  33. <info>phpmig init</info>
  34. EOT
  35. );
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function execute(InputInterface $input, OutputInterface $output)
  41. {
  42. $cwd = getcwd();
  43. $bootstrap = $cwd . DIRECTORY_SEPARATOR . 'phpmig.php';
  44. $relative = 'migrations';
  45. $migrations = $cwd . DIRECTORY_SEPARATOR . $relative;
  46. $this->initMigrationsDir($migrations, $output);
  47. $this->initBootstrap($bootstrap, $relative, $output);
  48. return 0;
  49. }
  50. /**
  51. * Create migrations dir
  52. *
  53. * @param $path
  54. * @return void
  55. */
  56. protected function initMigrationsDir($migrations, OutputInterface $output)
  57. {
  58. if (file_exists($migrations) && is_dir($migrations)) {
  59. $output->writeln(
  60. '<info>--</info> ' .
  61. str_replace(getcwd(), '.', $migrations) . ' already exists -' .
  62. ' <comment>Place your migration files in here</comment>'
  63. );
  64. return;
  65. }
  66. if (false === mkdir($migrations)) {
  67. throw new \RuntimeException(sprintf('Could not create directory "%s"', $migrations));
  68. }
  69. $output->writeln(
  70. '<info>+d</info> ' .
  71. str_replace(getcwd(), '.', $migrations) .
  72. ' <comment>Place your migration files in here</comment>'
  73. );
  74. }
  75. /**
  76. * Create bootstrap
  77. *
  78. * @param string $bootstrap where to put bootstrap file
  79. * @param string $migrations path to migrations dir relative to bootstrap
  80. * @return void
  81. */
  82. protected function initBootstrap($bootstrap, $migrations, OutputInterface $output)
  83. {
  84. if (file_exists($bootstrap)) {
  85. $output->writeln(
  86. '<info>--</info> ' .
  87. str_replace(getcwd(), '.', $bootstrap) . ' already exists -' .
  88. ' <comment>Create services in here</comment>'
  89. );
  90. return;
  91. }
  92. if (!is_writable(dirname($bootstrap))) {
  93. throw new \RuntimeException(sprintf('The file "%s" is not writeable', $bootstrap));
  94. }
  95. $contents = <<<PHP
  96. <?php
  97. use \Phpmig\Adapter;
  98. \$container = new ArrayObject();
  99. // replace this with a better Phpmig\Adapter\AdapterInterface
  100. \$container['phpmig.adapter'] = new Adapter\File\Flat(__DIR__ . DIRECTORY_SEPARATOR . '$migrations/.migrations.log');
  101. \$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';
  102. // You can also provide an array of migration files
  103. // \$container['phpmig.migrations'] = array_merge(
  104. // glob('migrations_1/*.php'),
  105. // glob('migrations_2/*.php')
  106. // );
  107. return \$container;
  108. PHP;
  109. if (false === file_put_contents($bootstrap, $contents)) {
  110. throw new \RuntimeException('The file "%s" could not be written to', $bootstrap);
  111. }
  112. $output->writeln(
  113. '<info>+f</info> ' .
  114. str_replace(getcwd(), '.', $bootstrap) .
  115. ' <comment>Create services in here</comment>'
  116. );
  117. }
  118. }