PageRenderTime 113ms CodeModel.GetById 32ms RepoModel.GetById 2ms app.codeStats 0ms

/fuel/packages/oil/classes/console.php

https://bitbucket.org/codeyash/bootstrap
PHP | 237 lines | 183 code | 29 blank | 25 comment | 20 complexity | 7bc8756a0dcec3694c22c3a288cdfa30 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  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\Console Class
  15. *
  16. * @package Fuel
  17. * @subpackage Oil
  18. * @category Core
  19. * @author Phil Sturgeon
  20. */
  21. class Console
  22. {
  23. public function __construct()
  24. {
  25. error_reporting(E_ALL | E_STRICT);
  26. ini_set("error_log", NULL);
  27. ini_set("log_errors", 1);
  28. ini_set("html_errors", 0);
  29. ini_set("display_errors", 0);
  30. while (ob_get_level ())
  31. {
  32. ob_end_clean();
  33. }
  34. ob_implicit_flush(true);
  35. // And, go!
  36. self::main();
  37. }
  38. private function main()
  39. {
  40. \Cli::write(sprintf(
  41. 'Fuel %s - PHP %s (%s) (%s) [%s]',
  42. \Fuel::VERSION,
  43. phpversion(),
  44. php_sapi_name(),
  45. self::build_date(),
  46. PHP_OS
  47. ));
  48. // Loop until they break it
  49. while (TRUE)
  50. {
  51. if (\Cli::$readline_support)
  52. {
  53. readline_completion_function(array(__CLASS__, 'tab_complete'));
  54. }
  55. if ( ! $__line = rtrim(trim(trim(\Cli::input('>>> ')), PHP_EOL), ';'))
  56. {
  57. continue;
  58. }
  59. if ($__line == 'quit')
  60. {
  61. break;
  62. }
  63. // Add this line to history
  64. //$this->history[] = array_slice($this->history, 0, -99) + array($line);
  65. if (\Cli::$readline_support)
  66. {
  67. readline_add_history($__line);
  68. }
  69. if (self::is_immediate($__line))
  70. {
  71. $__line = "return ($__line)";
  72. }
  73. ob_start();
  74. // Unset the previous line and execute the new one
  75. $random_ret = \Str::random();
  76. try
  77. {
  78. $ret = eval("unset(\$__line); $__line;");
  79. }
  80. catch(\Exception $e)
  81. {
  82. $ret = $random_ret;
  83. $__line = $e->getMessage();
  84. }
  85. // Error was returned
  86. if ($ret === $random_ret)
  87. {
  88. \Cli::error('Parse Error - ' . $__line);
  89. \Cli::beep();
  90. }
  91. if (ob_get_length() == 0)
  92. {
  93. if (is_bool($ret))
  94. {
  95. echo $ret ? 'true' : 'false';
  96. }
  97. elseif (is_string($ret))
  98. {
  99. echo addcslashes($ret, "\0..\37\177..\377");
  100. }
  101. elseif ( ! is_null($ret))
  102. {
  103. var_export($ret);
  104. }
  105. }
  106. unset($ret);
  107. $out = ob_get_contents();
  108. ob_end_clean();
  109. if ((strlen($out) > 0) && (substr($out, -1) != PHP_EOL))
  110. {
  111. $out .= PHP_EOL;
  112. }
  113. echo $out;
  114. unset($out);
  115. }
  116. }
  117. private static function is_immediate($line)
  118. {
  119. $skip = array(
  120. 'class', 'declare', 'die', 'echo', 'exit', 'for',
  121. 'foreach', 'function', 'global', 'if', 'include',
  122. 'include_once', 'print', 'require', 'require_once',
  123. 'return', 'static', 'switch', 'unset', 'while'
  124. );
  125. $okeq = array('===', '!==', '==', '!=', '<=', '>=');
  126. $code = '';
  127. $sq = false;
  128. $dq = false;
  129. for ($i = 0; $i < strlen($line); $i++)
  130. {
  131. $c = $line{$i};
  132. if ($c == "'")
  133. {
  134. $sq = !$sq;
  135. }
  136. elseif ($c == '"')
  137. {
  138. $dq = !$dq;
  139. }
  140. elseif ( ($sq) || ($dq) && $c == "\\")
  141. {
  142. ++$i;
  143. }
  144. else
  145. {
  146. $code .= $c;
  147. }
  148. }
  149. $code = str_replace($okeq, '', $code);
  150. if (strcspn($code, ';{=') != strlen($code))
  151. {
  152. return false;
  153. }
  154. $kw = preg_split("[^a-z0-9_]i", $code);
  155. foreach ($kw as $i)
  156. {
  157. if (in_array($i, $skip))
  158. {
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. public static function tab_complete($line, $pos, $cursor)
  165. {
  166. $const = array_keys(get_defined_constants());
  167. $var = array_keys($GLOBALS);
  168. $func = get_defined_functions();
  169. foreach ($func["user"] as $i)
  170. {
  171. $func["internal"][] = $i;
  172. }
  173. $func = $func["internal"];
  174. return array_merge($const, $var, $func);
  175. }
  176. private static function build_date()
  177. {
  178. ob_start();
  179. phpinfo(INFO_GENERAL);
  180. $x = ob_get_contents();
  181. ob_end_clean();
  182. $x = strip_tags($x);
  183. $x = explode("\n", $x); // PHP_EOL doesn't work on Windows
  184. $s = array('Build Date => ', 'Build Date ');
  185. foreach ($x as $i)
  186. {
  187. foreach ($s as $j)
  188. {
  189. if (substr($i, 0, strlen($j)) == $j)
  190. {
  191. return trim(substr($i, strlen($j)));
  192. }
  193. }
  194. }
  195. return '???';
  196. }
  197. }
  198. /* End of file oil/classes/console.php */