Home > Articles > Kohana 3 and Waterspout integration

Kohana 3 and Waterspout integration

February 18th, 2011

Inspired by [1] and [2] an idea came to integrate Kohana with Waterspout, to make add Kohana more powerful in the sence of websockets.

Actually there are two ways how to do that, depending on which of them should be main framework and which should be dependable. The first oprtion is convinient if you are going to use Kohana+Waterspout only as a WebSocket server. The second option is more convinient as you can use as separately Kohana and Waterspout for some controllers and for some controllers together.

Option I (Kohana is a main framework)

Actually we are going to convert Kohana into WebSocket server.

Implementation could be similar to Kohana + Zend Framework Web Services.

  • We just include waterspout server into Kohana’s index.php
  • Rename waterspout Controller class to something line ControllerWS and a variable $request to $requestws
  • Add one more class to extension chain of Kohana Controller by extending  ControllerWS.
  • Execute as a daemon kohana’s index.php, like php –q kohana_path/index.php

Option II (Waterspout and Kohana work in parralel)

Here we are going to allow calls of Kohana controllers from Waterspout controllers.

For this we need:

  • Make a copy of index.php from kohana root and name it for example waterspout.php, inside we will use also a copy of bootstrap.php, lets call it bootstrapwaterspout.php

Just change in the code last line of index.php file

// Bootstrap the application
require APPPATH.'bootstrapwaterspout'.EXT;
  • Make a copy of bootstrap.php from kohana application folder and name it for example bootstrapwaterspout.php, place it in the same direcory where origional bootstrap was.
  • Change bootstrapwaterspout.php in a way that just comment out last block
  • In waterspout we also need some changes. As Waterspout and Kohana have some classes and variables with the same name, we should rename waterspout class Controller to for exapme ControllerWS, just do refactor in eclipse or find-replace. You Should also replace variable $request to $requestWS in the whole waterspout framework (not too much, some kind of ~10 replacements)
  • In waterspout, I do not why we are starting to receive warnings, but line 229 should be also replaced in file /libs/ to the folowing code
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/


//echo Request::instance()
//->execute()
//->send_headers()
//->response;
  • We are done, just place this include in the place you would like to request kohana controllers or views, in our case

from

$this->remote_ip  =  reset(explode(':', $remote_ip));

to

preg_match('/(?[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3})[:]/',$remote_ip,$preg_result);
$this->remote_ip  =  $preg_result['ip'];
  • We are done, just place this include in the place you would like to request kohana controllers or views (or even better into waterspout server.php), in our case
require_once realpath(__DIR__.'/../../waterspout.php');

and feel free to request controllers or views via request factory like this

$body =  Request::factory('yourcontroller/youraction')->execute()->response;

References

[1] – https://github.com/wp-kohana/wp-kohana
[2] – http://kohana-modules.com/modules/kerkness/wordpress-for-kohana

Share and Enjoy:
  • Twitter
  • Google Bookmarks
  • del.icio.us
  • Facebook
  • Print this article!
  • Digg
  • Sphinn
  • Mixx
  • E-mail this story to a friend!
  • LinkedIn
Author: Anton Vedeshin Categories: Articles Tags:
  1. Christophe Jung
    July 13th, 2011 at 10:38 | #1

    Hye,

    I want to try this class, but i can’t dezip de file. Can you send me apr email?

    Thank’s a lot

    Reply

  1. No trackbacks yet.

Bad Behavior has blocked 144 access attempts in the last 7 days.