Commits

Thomas Weinert committed e9df078

Code Cleanup, removed unused properties and variables
Refactored response dom handling into a abstract superclass

Comments (0)

Files changed (7)

example/Http/ajax/ajax.php

 $route = new Http\Route();
 $route->match(
   '/time',
-  function ($request, $parameters) {
+  function (Http\Request $request) {
     $response = $request->createResponse();
     $response->content = new Http\Response\Content\Xml();
     $dom = $response->content->document;
 );
 $route->match(
   '/',
-  function ($request) {
+  function (Http\Request $request) {
     $response = $request->createResponse();
     $response->content = new Http\Response\Content\File(
       __DIR__.'/index.html', 'text/html', 'utf-8'
 $loop = Carica\Io\Event\Loop\Factory::get();
 $loop->setInterval(
   function() use ($loop) {
-    var_dump($loop->count());
+    echo "Current event count: ", $loop->count(), "\n";
     gc_collect_cycles();
   },
   5000

example/Http/hello-world.php

 include(__DIR__.'/../../src/Carica/Io/Loader.php');
 Carica\Io\Loader::register();
 
+use Carica\Io\Network\Http;
+
 $server = new Carica\Io\Network\Server();
 $server->events()->on(
   'connection',
   function ($stream) {
-    $request = new Carica\Io\Network\Http\Connection($stream);
+    $request = new Http\Connection($stream);
     $request->events()->on(
       'request',
-      function ($request) {
+      function (Http\Request $request) {
         echo $request->method.' '.$request->url."\n";
         $request->connection()->write(
           "HTTP/1.1 200 OK\r\n".

example/Http/route.php

 $route = new Carica\Io\Network\Http\Route();
 $route->match(
   '/hello/{name}',
-  function ($request, $parameters) {
+  function (Http\Request $request, $parameters) {
     $response = $request->createResponse();
     $response->content = new Http\Response\Content\String(
       "Hello ".$parameters['name']."!\n"
 );
 $route->match(
   '/agent',
-  function ($request, $parameters) {
+  function (Http\Request $request) {
     $response = $request->createResponse();
     $response->content = new Http\Response\Content\String(
       $request->headers['User-Agent']
 );
 $route->match(
   '/xml',
-  function ($request, $parameters) {
+  function (Http\Request $request) {
     $response = $request->createResponse();
     $response->content = new Http\Response\Content\Xml();
     $dom = $response->content->document;

example/MySQL/parallel.php

 
 $mysqlOne = new Io\Deferred\MySQL(new mysqli('localhost'));
 $mysqlTwo = new Io\Deferred\MySQL(new mysqli('localhost'));
-
 $time = microtime(TRUE);
-
+$debug = function($result) use ($time) {
+  var_dump(iterator_to_array($result));
+  var_dump(microtime(TRUE) - $time);
+};
 $queries = Io\Deferred::When(
   $mysqlOne("SELECT 'Query 1', SLEEP(5)")
-    ->done(
-      function($result) use ($time) {
-        var_dump(iterator_to_array($result));
-        var_dump(microtime(TRUE) - $time);
-      }
-    ),
+    ->done($debug),
   $mysqlTwo("SELECT 'Query 2', SLEEP(1)")
-    ->done(
-      function($result) use ($time) {
-        var_dump(iterator_to_array($result));
-        var_dump(microtime(TRUE) - $time);
-      }
-    )
+    ->done($debug)
 );
-
 Io\Event\Loop\Factory::run($queries);
 
 
       'read-data',
       function($data) use ($client, &$clients) {
         if (empty($client->name) &&
-            preg_match("(\S+)", $data, $matches) &&
+            preg_match('(\S+)', $data, $matches) &&
             !isset($clients[$matches[0]])) {
           $client->name = $matches[0];
           $clients[$client->name] = $client;
   function () use (&$clients) {
     echo "Send time to ".count($clients)." clients\n";
     foreach ($clients as $index => $client) {
+      /**
+       * @var Io\Network\Connection $client
+       */
       if ($client->isActive()) {
         $client->write(date(DATE_ATOM)."\n");
       } else {

src/Carica/Io/Network/Http/Response/Content/Dom.php

+<?php
+
+namespace Carica\Io\Network\Http\Response\Content {
+
+  use Carica\Io;
+  use Carica\Io\Network;
+  use Carica\Io\Network\Http\Response;
+
+  /**
+   * An response thats filled using a DOM document
+   *
+   * @property \DOMDocument $document
+   */
+  abstract class Dom extends Response\Content {
+
+    private $_document = NULL;
+
+    public function __construct($type = 'application/xml; charset=utf-8') {
+      parent::__construct($type);
+      $this->_document = new \DOMDocument('1.0', 'utf-8');
+    }
+
+    public function __get($name) {
+      switch ($name) {
+      case 'document' :
+        return $this->{'_'.$name};
+      }
+      return parent::__get($name);
+    }
+  }
+}