PageRenderTime 91ms CodeModel.GetById 28ms RepoModel.GetById 3ms app.codeStats 0ms

/classes/command.php

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