Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phpixie inside reactphp #1

Closed
softwarevamp opened this issue Aug 9, 2015 · 6 comments
Closed

phpixie inside reactphp #1

softwarevamp opened this issue Aug 9, 2015 · 6 comments

Comments

@softwarevamp
Copy link

could you please write some code sample running phpixie inside reactphp

@dracony
Copy link
Member

dracony commented Aug 9, 2015

Sure, but I am on vacation atm, so I dont have much time for that. Ill post
something tomorrow =)
On Aug 9, 2015 4:15 AM, "softwarevamp" [email protected] wrote:

could you please write some code sample running phpixie inside reactphp


Reply to this email directly or view it on GitHub
#1.

@dracony
Copy link
Member

dracony commented Aug 9, 2015

Actually I just gave it a go, the important part is converting ReactPHP requests to PSR-7: reactphp/http#28

As soon as they have PSR-7 support it should be a one liner, but for now it will take some tinkering to convert them manually. The flow is pretty much this:

require_once(__DIR__.'/../vendor/autoload.php');
$pixie = new Project\Framework();
$pixie->registerDebugHandlers();

$app = function ($request, $response) use($pixie) {
   $psr7ServerRequest  = convertRequestToPsr7($request);
   $psr7Response = $pixie->processHttpServerRequest($psr7ServerRequest);
   convertPsr7Response($response);
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run();

I could not find anything that converts React requests to psr7 requests but there must be something available. if not, you can use this to build one (just pass relevant parameters extracted from react request):

$http = $pixie->builder->components()->http();
$psr7ServerRequest = $http->messages()->serverRequest(...);

https://github.com/PHPixie/HTTP/blob/master/src/PHPixie/HTTP/Messages.php#L27

@softwarevamp
Copy link
Author

Do you have some guides converting to reactphp response?

发送自我的 BlackBerry 10 智能手机。
发件人: dracony
已发送: 2015年8月10日星期一 上午2:15
收件人: PHPixie/HTTP
答复: PHPixie/HTTP
抄送: softwarevamp
主题: Re: [HTTP] phpixie inside reactphp (#1)

Actually I just gave it a go, the important part is converting ReactPHP requests to PSR-7: reactphp/http#28

As soon as they have PSR-7 support it should be a one liner, but for now it will take some tinkering to convert them manually. The flow is pretty much this:

require_once(__DIR__.'/../vendor/autoload.php');
$pixie = new Project\Framework();
$pixie->registerDebugHandlers();

$app = function ($request, $response) use($pixie) {
   $psr7ServerRequest  = convertRequestToPsr7($request);
   $psr7Response = $pixie->processHttpServerRequest($psr7ServerRequest);
   convertPsr7Response($response);
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run();

I could not find anything that converts React requests to psr7 requests but there must be something available. if not, you can use this to build one (just pass relevant parameters extracted from react request):

$http = $pixie->builder->components()->http();
$psr7ServerRequest = $http->messages()->serverRequest(...);

https://github.com/PHPixie/HTTP/blob/master/src/PHPixie/HTTP/Messages.php#L27


Reply to this email directly or view it on GitHub:
#1 (comment)

@dracony
Copy link
Member

dracony commented Aug 12, 2015

<?php

require_once('vendor/autoload.php');

$host = 'localhost';
$port = 1337;

$framework = new Project\Framework();
$framework->registerDebugHandlers(false, false);

$app = function ($request, $response) use ($framework, $host, $port) {
    $http = $framework->builder()->components()->http();
    $uri = $http->messages()->uri('http://'.$host.':'.$port.$request->getPath());

    $serverRequest = $http->messages()->serverRequest(
        $request->getHttpVersion(),
        $request->getHeaders(),
        '',
        $request->getMethod(),
        $uri,
        array(),
        $request->getQuery(),
        array(),
        array(),
        array()
    );

    $frameworkResponse = $framework->processHttpServerRequest($serverRequest);
    $response->writeHead(
        $frameworkResponse->getStatusCode(),
        $frameworkResponse->getHeaders()
    );
    $response->end(
        $frameworkResponse->getBody()
    );
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);

$http->on('request', $app);

$socket->listen($port);
$loop->run();

Here you go =)

@softwarevamp
Copy link
Author

Real guru!

发送自我的 BlackBerry 10 智能手机。
发件人: dracony
已发送: 2015年8月12日星期三 下午7:39
收件人: PHPixie/HTTP
答复: PHPixie/HTTP
抄送: softwarevamp
主题: Re: [HTTP] phpixie inside reactphp (#1)

<?php

require_once('vendor/autoload.php');

$host = 'localhost';
$port = 1337;

$framework = new Project\Framework();
$framework->registerDebugHandlers(false, false);

$app = function ($request, $response) use ($framework, $host, $port) {
    $http = $framework->builder()->components()->http();
    $uri = $http->messages()->uri('http://'.$host.':'.$port.$request->getPath());

    $serverRequest = $http->messages()->serverRequest(
        $request->getHttpVersion(),
        $request->getHeaders(),
        '',
        $request->getMethod(),
        $uri,
        array(),
        $request->getQuery(),
        array(),
        array(),
        array()
    );

    $frameworkResponse = $framework->processHttpServerRequest($serverRequest);
    $response->writeHead(
        $frameworkResponse->getStatusCode(),
        $frameworkResponse->getHeaders()
    );
    $response->end(
        $frameworkResponse->getBody()
    );
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);

$http->on('request', $app);

$socket->listen($port);
$loop->run();

Here you go =)


Reply to this email directly or view it on GitHub:
#1 (comment)

@dracony
Copy link
Member

dracony commented Aug 13, 2015

Thanks =)))))))))))

Although do keep in mind ReactPHP has a few limitations, like not parsing $_POST data by default, so I did not include it in this example. If you'll need anything specific just ask =)

@dracony dracony closed this as completed Oct 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants