PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/shells/ruck.php

https://github.com/iainmullan/cakephp-bits
PHP | 152 lines | 85 code | 25 blank | 42 comment | 8 complexity | 4e58bdbcd507c6d94b552dfd934b3849 MD5 | raw file
  1. <?php
  2. /**
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  4. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  5. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  6. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  7. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  8. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  9. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  10. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11. *
  12. * A Console task wrapping around Ruckusing commands, for convenience. The name of the task is shortened to 'ruck' for
  13. * command line brevity!
  14. *
  15. * Examples:
  16. * > cake ruck setup
  17. * > cake ruck version
  18. * > cake ruck generate
  19. * > cake ruck migrate <version number>
  20. *
  21. * There is also a special task called 'config' which copies DB config details
  22. * from your cake app into the ruckusing DB config
  23. *
  24. * > cake ruck config
  25. *
  26. * Copyright (c) Iain Mullan 2010 www.ebotunes.com
  27. * @see http://code.google.com/p/ruckusing/
  28. * @author Iain Mullan
  29. * @created 13th January 2010
  30. *
  31. */
  32. class RuckShell extends Shell {
  33. var $backupConfig = false;
  34. public function main() {
  35. }
  36. /**
  37. * Task methods
  38. */
  39. function setup() {
  40. $this->_main('db', 'setup');
  41. }
  42. function version() {
  43. $this->_main('db', 'version');
  44. }
  45. function add() {
  46. $this->generate();
  47. }
  48. function generate() {
  49. if (empty($this->args)) {
  50. $this->out("\nUsage: cake ruck generate <migration name>\n");
  51. exit();
  52. }
  53. $name = $this->args[0];
  54. $command = 'php generate.php '.$name;
  55. $this->_exec($command);
  56. }
  57. function migrate() {
  58. if (empty($this->args)) {
  59. $args = '';
  60. } else {
  61. $args = 'VERSION='.$this->args[0];
  62. }
  63. $this->_main('db', 'migrate', $args);
  64. }
  65. /**
  66. * Invoke this task with no arguments to output the current Ruck Db config.
  67. * Alternatively give the name of a CakeDB config, to copy those details to the Ruck db config.
  68. */
  69. function config() {
  70. if (empty($this->args)) {
  71. $this->_readConfig();
  72. } else {
  73. $config = $this->args[0];
  74. $this->_writeConfig($config);
  75. }
  76. }
  77. private function _writeConfig($config) {
  78. // Copy the Cake app's db config details to the ruckusing db config
  79. echo 'Copying $'.$config.' db config';
  80. // filename APP/vendors/ruckusing/config/database.inc.php
  81. chdir(APP."vendors/ruckusing/config");
  82. // backup the old version first
  83. if ($this->backupConfig) {
  84. rename('database.inc.php', 'database.inc.php.'.time());
  85. }
  86. // get the cake details
  87. App::Import('ConnectionManager');
  88. $ds = ConnectionManager::getDataSource($config);
  89. $dsc = $ds->config;
  90. if ($dsc['driver'] == 'mysqli') {
  91. $dsc['driver'] = 'mysql';
  92. }
  93. // generate the contents of the whole file
  94. $contents = "<?php
  95. \$ruckusing_db_config = array(
  96. 'development' => array(
  97. 'type' => '{$dsc['driver']}',
  98. 'host' => '{$dsc['host']}',
  99. 'port' => {$dsc['port']},
  100. 'database' => '{$dsc['database']}',
  101. 'user' => '{$dsc['login']}',
  102. 'password' => '{$dsc['password']}'
  103. )
  104. );
  105. ?>\n";
  106. $file = fopen('database.inc.php', 'a+');
  107. fwrite($file, $contents);
  108. fclose($file);
  109. }
  110. private function _readConfig() {
  111. chdir(APP."vendors/ruckusing/config");
  112. $file = file('database.inc.php');
  113. print_r($file);
  114. }
  115. private function _exec($command) {
  116. chdir(APP."vendors/ruckusing");
  117. $output = '';
  118. exec($command, $output);
  119. $this->out($output);
  120. }
  121. private function _main($namespace, $task, $args = '') {
  122. $args = $namespace.':'.$task.' '.$args;
  123. $command = 'php main.php '.$args;
  124. $this->_exec($command);
  125. }
  126. }
  127. ?>