/src/madeam/Console.php

https://github.com/GunioRobot/madeam · PHP · 206 lines · 127 code · 29 blank · 50 comment · 23 complexity · 5e0f9c089f2eccd8cf91fb9ae2d7b8ff MD5 · raw file

  1. <?php
  2. namespace madeam;
  3. /**
  4. * Madeam PHP Framework <http://madeam.com>
  5. * Copyright (c) 2009, Joshua Davey
  6. * 202-212 Adeliade St. W, Toronto, Ontario, Canada
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) 2009, Joshua Davey
  12. * @link http://www.madeam.com
  13. * @package madeam
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. * @author Joshua Davey
  16. */
  17. class Console {
  18. /**
  19. * undocumented
  20. *
  21. * @author Joshua Davey
  22. */
  23. public function __construct($params = array()) {
  24. array_shift($params); // remove script path
  25. $action = array_shift($params);
  26. // parse POSIX params
  27. // -name Posts => array('name' => 'Posts')
  28. // -scaffold -name Posts => array('scaffold' => true, 'name' => 'Posts')
  29. foreach ($params as $key => $param) {
  30. if (preg_match('/--.*/', $param)) {
  31. if (!isset($params[$key+1]) || preg_match('/--.*/', $params[$key+1])) {
  32. $params[substr($param, 2)] = true;
  33. unset($params[$key]);
  34. } else {
  35. $params[substr($param, 2)] = $params[$key+1];
  36. unset($params[$key]);
  37. unset($params[$key+1]);
  38. }
  39. }
  40. }
  41. // reflection
  42. $reflection = new \ReflectionObject($this);
  43. // check methods for callbacks
  44. $method = $reflection->getMethod($action);
  45. //
  46. $defaults = array();
  47. $parameters = $method->getParameters();
  48. foreach ($parameters as $parameter) {
  49. // set parameters of callback (parameters in methods act as meta data for callbacks)
  50. if ($parameter->isDefaultValueAvailable()) {
  51. $defaults[$parameter->getName()] = $parameter->getDefaultValue();
  52. } else {
  53. $defaults[$parameter->getName()] = null;
  54. }
  55. }
  56. $args = array();
  57. foreach ($defaults as $param => $value) {
  58. if (isset($params[$param])) {
  59. $args[] = $params[$param];
  60. } else {
  61. $args[] = $value;
  62. }
  63. }
  64. call_user_func_array(array($this, $action), $args);
  65. }
  66. /**
  67. * undocumented
  68. *
  69. * @author Joshua Davey
  70. */
  71. public function initialize2() {
  72. array_shift($_SERVER['argv']);
  73. $args = $_SERVER['argv'];
  74. $scriptName = false;
  75. $commandName = false;
  76. // get list of available consoles
  77. $scriptNames = array('create', 'delete', 'test', 'migrate');
  78. while(true) {
  79. // reset console
  80. $scriptName = false;
  81. // check to see if the console exists in the args
  82. if (isset($args[0])) {
  83. $scriptName = array_shift($args);
  84. }
  85. // if the command requires to be in the application's root path then check it.
  86. // If we aren't in the applicatin's root path then tell the user and exit
  87. if ($scriptName != 'make') {
  88. if (!file_exists(realpath('application/vendor/Madeam.php'))) {
  89. console\CLI::outError('Please point Madeam Console to the root directory of your application.');
  90. exit();
  91. }
  92. }
  93. try {
  94. $scriptClassName = 'Console_Script_' . ucfirst($scriptName);
  95. $script = new $scriptClassName;
  96. break;
  97. } catch (exception\AutoloadFail $e) {
  98. if ($scriptName === false) {
  99. // ask them for the script of the console they'd like to use
  100. console\CLI::outMenu('Scripts', $scriptNames);
  101. $scriptName = console\CLI::getCommand('script');
  102. }
  103. // by entering a console name at this point it means they've tried entering one that doesn't exist.
  104. // prompt them with an saying please try again.
  105. if ($scriptName !== false) {
  106. console\CLI::outError("Sorry the script you entered does not exist.");
  107. }
  108. }
  109. }
  110. // get list of commands for this script
  111. $commandNames = array();
  112. $classReflection = new \ReflectionClass($script);
  113. foreach ($classReflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $mehodReflection) {
  114. $commandNames[] = $mehodReflection->getName();
  115. }
  116. do {
  117. // by entering a console name at this point it means they've tried entering one that doesn't exist.
  118. // prompt them with an saying please try again.
  119. if ($commandName !== false) {
  120. console\CLI::outError("Sorry the command you entered does not exist.");
  121. }
  122. // reset command
  123. $commandName = false;
  124. // check to see if the command exists in the args
  125. if (isset($args[0])) {
  126. $commandName = array_shift($args);
  127. }
  128. if ($commandName == null) {
  129. console\CLI::outMenu('Commands', $commandNames);
  130. $commandName = console\CLI::getCommand('command');
  131. }
  132. } while(! in_array($commandName, $commandNames));
  133. try {
  134. return $script->$commandName($this->parseArguments($args));
  135. } catch (Exception $e) {
  136. Exception::handle($e);
  137. }
  138. // unset arguments -- they are only for first time use
  139. $args = array();
  140. }
  141. /**
  142. * undocumented
  143. *
  144. * @author Joshua Davey
  145. */
  146. protected function parseArguments($commandNames) {
  147. $params = array();
  148. foreach ($commandNames as $commandName) {
  149. $nodes = explode('=', $commandName);
  150. $name = $nodes[0];
  151. $value = $nodes[1];
  152. $params[$name] = $value;
  153. }
  154. return $params;
  155. }
  156. protected function createFile($file_name, $file_path, $file_content) {
  157. if (substr($file_path, - 1) !== DS) {
  158. $file_path = $file_path . DS;
  159. }
  160. $file = $file_path . $file_name;
  161. if (file_exists($file)) {
  162. if (console\CLI::getYN('The file ' . $file_name . ' already exists. Overwrite?') === false) {
  163. return false;
  164. }
  165. }
  166. if (file_put_contents($file, $file_content)) {
  167. console\CLI::outCreate('file ' . $file);
  168. return true;
  169. }
  170. }
  171. protected function createDir($dir) {
  172. if (!file_exists($dir)) {
  173. mkdir($dir, 0777, true);
  174. console\CLI::outCreate('directory ' . $dir);
  175. }
  176. }
  177. }