tools.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Install Tools
  4. * 2010-06-05 ms
  5. */
  6. class ToolsShell extends Shell {
  7. var $uses = array();
  8. var $tasks = array('DbConfig');
  9. private $files = array();
  10. private $folder = null;
  11. private $models = array('Configuration', 'User', 'Role');
  12. private $Db;
  13. function startup() {
  14. Configure::write('debug', 2);
  15. foreach ($this->models as $m) {
  16. $imported = App::import('Model', $m);
  17. if ($imported === true) {
  18. $modelname = $m;
  19. break;
  20. }
  21. }
  22. if (empty($modelname)) {
  23. $this->error('At least one of the following DB-Tables are required:', implode(', ', $this->models));
  24. }
  25. $this->Db = ClassRegistry::init($modelname); //old: new Model();
  26. }
  27. function main() {
  28. $this->out('Install/Manage Tools');
  29. $this->out('');
  30. $this->out('Usage:');
  31. $this->out('- cake install Tool {params}');
  32. $this->out('- cake uninstall Tool {params}');
  33. $this->out('');
  34. $this->out('Tools:');
  35. $this->_getFiles();
  36. foreach ($this->files as $file) {
  37. $this->out('- '.Inflector::camelize(extractPathInfo('file', $file)));
  38. }
  39. $this->out('');
  40. $this->out('Params:');
  41. $this->out('-f => Force Reinstall (Drop + Create)');
  42. $this->out('-s => Status (TODO!)');
  43. }
  44. function install() {
  45. if (empty($this->args)) {
  46. return $this->main();
  47. }
  48. $args = $this->args;
  49. if (!empty($args[0]) && $args[0] == 'all') {
  50. $this->_getFiles();
  51. $args = $this->files;
  52. }
  53. if (!empty($this->params['f'])) {
  54. $this->args = $args;
  55. $this->uninstall();
  56. }
  57. foreach ($args as $arg) {
  58. if ($sql = $this->_getFile($arg)) {
  59. $sql = String::insert($sql, array('prefix'=>$this->Db->tablePrefix), array('before'=>'{', 'after'=>'}', 'clean'=>true));
  60. $this->Db->query($sql);
  61. $this->out('OK: '.$arg.' created');
  62. } else {
  63. $this->out($arg.' not found');
  64. }
  65. }
  66. $this->out('... done');
  67. }
  68. function uninstall() {
  69. if (empty($this->args)) {
  70. return $this->main();
  71. }
  72. $args = $this->args;
  73. if (!empty($args[0]) && $args[0] == 'all') {
  74. $this->_getFiles();
  75. $args = $this->files;
  76. }
  77. foreach ($args as $arg) {
  78. if ($sql = $this->_getFile($arg)) {
  79. $sqlParts = explode(NL, $sql, 2);
  80. if (mb_strpos($sqlParts[0], '-- ') !== 0) {
  81. $this->out('Error: '.$arg);
  82. continue;
  83. }
  84. $sql = trim(mb_substr($sqlParts[0], 3));
  85. if (!empty($sql)) {
  86. //$this->Db->execute('DROP TABLE IF EXISTS `'.$this->Db->tablePrefix.$sql.'`;');
  87. //$this->_ensureDatabaseConnection();
  88. $this->Db->query('DROP TABLE IF EXISTS `'.$this->Db->tablePrefix.$sql.'`;');
  89. $this->out('OK: '.$arg.' dropped');
  90. //die('drop: '.$sql);
  91. } else {
  92. $this->out('Error: '.$sql);
  93. }
  94. } else {
  95. $this->out($arg.' not found');
  96. }
  97. }
  98. $this->out('... done');
  99. }
  100. /*
  101. function _ensureDatabaseConnection() {
  102. if (empty($this->connection)) {
  103. $this->connection = $this->DbConfig->getConfig();
  104. }
  105. $this->Db =& ConnectionManager::getDataSource($this->connection);
  106. }
  107. */
  108. function _getFiles() {
  109. App::import('Core', 'Folder');
  110. //TODO: make it more generic (could be somewhere else too...)
  111. $this->folder = APP.'plugins'.DS.'tools'.DS.'config'.DS.'sql'.DS;
  112. $handle = new Folder($this->folder);
  113. $content = $handle->read(true, true);
  114. $this->files = $content[1];
  115. }
  116. function _getFile($file) {
  117. if (empty($this->files)) {
  118. $this->_getFiles();
  119. }
  120. $file = Inflector::underscore($file).'.sql';
  121. if (file_exists($this->folder.$file)) {
  122. return file_get_contents($this->folder.$file);
  123. }
  124. return false;
  125. }
  126. }
  127. ?>