command.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Oil;
  13. /**
  14. * Oil\Cli Class
  15. *
  16. * @package Fuel
  17. * @subpackage Oil
  18. * @category Core
  19. */
  20. class Command
  21. {
  22. public static function init($args)
  23. {
  24. \Config::load('oil', true);
  25. // Remove flag options from the main argument list
  26. $args = self::_clear_args($args);
  27. try
  28. {
  29. if ( ! isset($args[1]))
  30. {
  31. if (\Cli::option('v', \Cli::option('version')))
  32. {
  33. \Cli::write('Fuel: '.\Fuel::VERSION.' running in "'.\Fuel::$env.'" mode');
  34. return;
  35. }
  36. static::help();
  37. return;
  38. }
  39. switch ($args[1])
  40. {
  41. case 'g':
  42. case 'generate':
  43. $action = isset($args[2]) ? $args[2]: 'help';
  44. $subfolder = 'orm';
  45. if (is_int(strpos($action, '/')))
  46. {
  47. list($action, $subfolder)=explode('/', $action);
  48. }
  49. switch ($action)
  50. {
  51. case 'config':
  52. case 'controller':
  53. case 'model':
  54. case 'migration':
  55. case 'task':
  56. call_user_func('Oil\Generate::'.$action, array_slice($args, 3));
  57. break;
  58. case 'views':
  59. call_user_func('Oil\Generate::views', array_slice($args, 3), $subfolder);
  60. break;
  61. case 'admin':
  62. call_user_func('Oil\Generate_Admin::forge', array_slice($args, 3), $subfolder);
  63. break;
  64. case 'scaffold':
  65. call_user_func('Oil\Generate_Scaffold::forge', array_slice($args, 3), $subfolder);
  66. break;
  67. default:
  68. Generate::help();
  69. }
  70. break;
  71. case 'c':
  72. case 'console':
  73. new Console;
  74. case 'p':
  75. case 'package':
  76. $action = isset($args[2]) ? $args[2]: 'help';
  77. switch ($action)
  78. {
  79. case 'install':
  80. case 'uninstall':
  81. call_user_func_array('Oil\Package::'.$action, array_slice($args, 3));
  82. break;
  83. default:
  84. Package::help();
  85. }
  86. break;
  87. case 'r':
  88. case 'refine':
  89. // Developers of third-party tasks may not be displaying PHP errors. Report any error and quit
  90. set_error_handler(function($errno, $errstr, $errfile, $errline) {
  91. if (!error_reporting()) return; // If the error was supressed with an @ then we ignore it!
  92. \Cli::error("Error: {$errstr} in $errfile on $errline");
  93. \Cli::beep();
  94. exit(1);
  95. });
  96. $task = isset($args[2]) ? $args[2] : null;
  97. call_user_func('Oil\Refine::run', $task, array_slice($args, 3));
  98. break;
  99. case 'cell':
  100. case 'cells':
  101. $action = isset($args[2]) ? $args[2]: 'help';
  102. switch ($action)
  103. {
  104. case 'list':
  105. call_user_func('Oil\Cell::all');
  106. break;
  107. case 'search':
  108. case 'install':
  109. case 'upgrade':
  110. case 'uninstall':
  111. call_user_func_array('Oil\Cell::'.$action, array_slice($args, 3));
  112. break;
  113. case 'info':
  114. case 'details':
  115. call_user_func_array('Oil\Cell::info', array_slice($args, 3));
  116. break;
  117. default:
  118. Cell::help();
  119. }
  120. break;
  121. case 't':
  122. case 'test':
  123. // Suppressing this because if the file does not exist... well thats a bad thing and we can't really check
  124. // I know that supressing errors is bad, but if you're going to complain: shut up. - Phil
  125. $phpunit_autoload_path = \Config::get('oil.phpunit.autoload_path', 'PHPUnit/Autoload.php' );
  126. @include_once($phpunit_autoload_path);
  127. // Attempt to load PHUnit. If it fails, we are done.
  128. if ( ! class_exists('PHPUnit_Framework_TestCase'))
  129. {
  130. throw new Exception('PHPUnit does not appear to be installed.'.PHP_EOL.PHP_EOL."\tPlease visit http://phpunit.de and install.");
  131. }
  132. // Check for a custom phpunit config, but default to the one from core
  133. if (file_exists(APPPATH.'phpunit.xml'))
  134. {
  135. $phpunit_config = APPPATH.'phpunit.xml';
  136. }
  137. else
  138. {
  139. $phpunit_config = COREPATH.'phpunit.xml';
  140. }
  141. // CD to the root of Fuel and call up phpunit with the path to our config
  142. $phpunit_command = \Config::get('oil.phpunit.binary_path', 'phpunit');
  143. $command = 'cd '.DOCROOT.'; '.$phpunit_command.' -c "'.$phpunit_config.'"';
  144. // Respect the group options
  145. \Cli::option('group') and $command .= ' --group '.\Cli::option('group');
  146. \Cli::option('exclude-group') and $command .= ' --exclude-group '.\Cli::option('exclude-group');
  147. // Respect the coverage-html option
  148. \Cli::option('coverage-html') and $command .= ' --coverage-html '.\Cli::option('coverage-html');
  149. \Cli::option('coverage-clover') and $command .= ' --coverage-clover '.\Cli::option('coverage-clover');
  150. \Cli::option('coverage-text') and $command .= ' --coverage-text='.\Cli::option('coverage-text');
  151. \Cli::option('coverage-php') and $command .= ' --coverage-php '.\Cli::option('coverage-php');
  152. \Cli::write('Tests Running...This may take a few moments.', 'green');
  153. $return_code = 0;
  154. foreach(explode(';', $command) as $c)
  155. {
  156. passthru($c, $return_code_task);
  157. // Return failure if any subtask fails
  158. $return_code |= $return_code_task;
  159. }
  160. exit($return_code);
  161. break;
  162. default:
  163. static::help();
  164. }
  165. }
  166. catch (Exception $e)
  167. {
  168. \Cli::error('Error: '.$e->getMessage());
  169. \Cli::beep();
  170. \Cli::option('speak') and `say --voice="Trinoids" "{$e->getMessage()}"`;
  171. exit(1);
  172. }
  173. }
  174. public static function help()
  175. {
  176. echo <<<HELP
  177. Usage:
  178. php oil [cell|console|generate|package|refine|help|test]
  179. Runtime options:
  180. -f, [--force] # Overwrite files that already exist
  181. -s, [--skip] # Skip files that already exist
  182. -q, [--quiet] # Supress status output
  183. -t, [--speak] # Speak errors in a robot voice
  184. Description:
  185. The 'oil' command can be used in several ways to facilitate quick development, help with
  186. testing your application and for running Tasks.
  187. Environment:
  188. If you want to specify a specific environment oil has to run in, overload the environment
  189. variable on the commandline: FUEL_ENV=staging php oil <commands>
  190. Documentation:
  191. http://docs.fuelphp.com/packages/oil/intro.html
  192. HELP;
  193. }
  194. private static function _clear_args($actions = array())
  195. {
  196. foreach ($actions as $key => $action)
  197. {
  198. if (substr($action, 0, 1) === '-')
  199. {
  200. unset($actions[$key]);
  201. }
  202. }
  203. return $actions;
  204. }
  205. }
  206. /* End of file oil/classes/command.php */