Skip to content
Browse files

An example console application based on http://github.com/cloudhead/h…

  • Loading branch information...
1 parent 9bdbce5 commit 1fd565ce6c989c1e1faf8571e2af6da7d88b8b32 @jlogsdon committed
Showing with 72 additions and 0 deletions.
  1. +72 −0 http-console.php
View
72 http-console.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * An example application using php-cli-tools and Buzz
+ */
+
+define('BUZZ_PATH', realpath('../Buzz'));
+define('TOOL_PATH', realpath('./'));
+define('SCRIPT_NAME', array_shift($argv));
+
+require_once BUZZ_PATH . '/lib/Buzz/ClassLoader.php';
+Buzz\ClassLoader::register();
+
+require_once TOOL_PATH . '/lib/cli/cli.php';
+\cli\register_autoload();
+
+class HttpConsole {
+ protected $_host;
+ protected $_prompt;
+
+ public function __construct($host) {
+ $this->_host = 'http://' . $host;
+ $this->_prompt = '%K' . $this->_host . '%n/%K>%n ';
+ }
+
+ public function handleRequest($type, $path) {
+ $request = new Buzz\Message\Request($type, $path, $this->_host);
+ $response = new Buzz\Message\Response;
+
+ $client = new Buzz\Client\FileGetContents();
+ $client->send($request, $response);
+
+ // Display headers
+ foreach ($response->getHeaders() as $i => $header) {
+ if ($i == 0) {
+ \cli\line('%G{:header}%n', compact('header'));
+ continue;
+ }
+
+ list($key, $value) = explode(': ', $header, 2);
+ \cli\line('%W{:key}%n: {:value}', compact('key', 'value'));
+ }
+ \cli\line("\n");
+ print $response->getContent() . "\n";
+
+ switch ($type) {
+ }
+ }
+
+ public function run() {
+ while (true) {
+ $cmd = \cli\prompt($this->_prompt, false, null);
+
+ if (preg_match('/^(HEAD|GET|POST|PUT|DELETE) (\S+)$/', $cmd, $matches)) {
+ $this->handleRequest($matches[1], $matches[2]);
+ continue;
+ }
+
+ if ($cmd == '\q') {
+ break;
+ }
+ }
+ }
+}
+
+try {
+ $console = new HttpConsole(array_shift($argv) ?: '127.0.0.1:80');
+ $console->run();
+} catch (\Exception $e) {
+ \cli\err("\n\n%R" . $e->getMessage() . "%n\n");
+}
+
+?>

0 comments on commit 1fd565c

Please sign in to comment.
Something went wrong with that request. Please try again.