Posted By

mscribellito on 06/10/11


Tagged


Versions (?)

PHP CLI


 / Published in: PHP
 

  1. <?php
  2.  
  3. /*
  4.  * CLI
  5.  * @package CLI
  6.  */
  7. final class CLI {
  8.  
  9. private static $instance = NULL;
  10. public $out = NULL;
  11. public $err = NULL;
  12. public $in = NULL;
  13. private $argc = 0;
  14. private $argv = array();
  15. private $args = array();
  16.  
  17. private function __construct() {
  18.  
  19. }
  20.  
  21. public function __destruct() {
  22.  
  23. }
  24.  
  25. public function __clone() {
  26.  
  27. }
  28.  
  29. public static function getInstance() {
  30.  
  31. if (!isset(self::$instance)) {
  32.  
  33. self::$instance = new self();
  34. self::$instance->initialize();
  35. }
  36.  
  37. return self::$instance;
  38.  
  39. }
  40.  
  41. private function initialize() {
  42.  
  43. $this->out = new CLIOutputStream('php://stdout');
  44. $this->err = new CLIOutputStream('php://stderr');
  45. $this->in = new CLIInputStream('php://stdin');
  46.  
  47. $this->argc = $_SERVER['argc'];
  48. $this->argv = $_SERVER['argv'];
  49.  
  50. $args = array(0 => $this->argv[0]);
  51.  
  52. for ($i = 1; $i < $this->argc; $i++) {
  53.  
  54. if (!isset($this->argv[$i])) {
  55. break;
  56. }
  57.  
  58. $opt = $this->argv[$i];
  59.  
  60. if (substr($opt, 0, 2) !== '--') {
  61. $args[$i] = $opt;
  62. continue;
  63. }
  64.  
  65. $opt = substr($opt, 2);
  66.  
  67. if (strpos($opt, '=')) {
  68. list ($opt, $value) = explode('=', $opt, 2);
  69. } else {
  70. $value = NULL;
  71. }
  72.  
  73. $args[$opt] = $value;
  74. }
  75.  
  76. unset($this->argc, $this->argv);
  77.  
  78. $this->args = $args;
  79.  
  80. }
  81.  
  82. public function quit($status = 0) {
  83.  
  84. exit($status);
  85.  
  86. }
  87.  
  88. public function wait($seconds = 0) {
  89.  
  90. sleep($seconds);
  91.  
  92. }
  93.  
  94. public function arg($opt) {
  95.  
  96. if (array_key_exists($opt, $this->args)) {
  97. return $this->args[$opt];
  98. }
  99.  
  100. }
  101.  
  102. public function args() {
  103.  
  104. return $this->args;
  105.  
  106. }
  107.  
  108. public function __toString() {
  109.  
  110. }
  111.  
  112. }
  113.  
  114. /*
  115.  * CLIOutputStream
  116.  * @package CLI
  117.  */
  118. final class CLIOutputStream {
  119.  
  120. private $output;
  121.  
  122. const LF = PHP_EOL;
  123.  
  124. public function __construct($stream) {
  125.  
  126. $this->output = fopen($stream, 'w');
  127.  
  128. }
  129.  
  130. public function __destruct() {
  131.  
  132. $this->close();
  133.  
  134. }
  135.  
  136. public function close() {
  137.  
  138. fclose($this->output);
  139.  
  140. }
  141.  
  142. public function write($text) {
  143.  
  144. fputs($this->output, $text);
  145.  
  146. }
  147.  
  148. public function writeln($text, $numlines = 1) {
  149.  
  150. fputs($this->output, $text . str_repeat(self::LF, $numlines));
  151.  
  152. }
  153.  
  154. }
  155.  
  156. /*
  157.  * CLIInputStream
  158.  * @package CLI
  159.  */
  160. final class CLIInputStream {
  161.  
  162. private $input;
  163.  
  164. public function __construct($stream) {
  165.  
  166. $this->input = fopen($stream, 'r');
  167.  
  168. }
  169.  
  170. public function __destruct() {
  171.  
  172. $this->close();
  173.  
  174. }
  175.  
  176. public function close() {
  177.  
  178. fclose($this->input);
  179.  
  180. }
  181.  
  182. public function next() {
  183.  
  184. return chop(fgets($this->input));
  185.  
  186. }
  187.  
  188. }
  189.  
  190. class Scanner {
  191.  
  192. private $scanner = NULL;
  193.  
  194. public function __construct(CLIInputStream $input) {
  195.  
  196. $this->scanner = $input;
  197.  
  198. }
  199.  
  200. public function next() {
  201.  
  202. $token = preg_split('/\s+/', $this->scanner->next());
  203.  
  204. return (string) $token[0];
  205.  
  206. }
  207.  
  208. public function nextBoolean() {
  209.  
  210. $boolean = strtolower($this->next());
  211.  
  212. if (preg_match('/(true|yes|on|1)/', $boolean)) {
  213. return TRUE;
  214. } else if (preg_match('/false|no|off|0/', $boolean)) {
  215. return FALSE;
  216. }
  217.  
  218. return NULL;
  219.  
  220. }
  221.  
  222. public function nextDouble() {
  223.  
  224. $double = $this->next();
  225.  
  226. if (preg_match('/^-?[0-9]\.[0-9]*$/', $double)) {
  227. return (double) $double;
  228. }
  229.  
  230. return NULL;
  231.  
  232. }
  233.  
  234. public function nextFloat() {
  235.  
  236. $float = $this->next();
  237.  
  238. if (preg_match('/^-?[0-9]\.[0-9]*$/', $float)) {
  239. return (float) $float;
  240. }
  241.  
  242. return NULL;
  243.  
  244. }
  245.  
  246. public function nextInt() {
  247.  
  248. $int = $this->next();
  249.  
  250. if (preg_match('/^-?[0-9]$/', $int)) {
  251. return (int) $int;
  252. }
  253.  
  254. return NULL;
  255.  
  256. }
  257.  
  258. public function nextLine() {
  259.  
  260. return (string) $this->scanner->next();
  261.  
  262. }
  263.  
  264. }
  265.  
  266. // EOF

Report this snippet  

You need to login to post a comment.