Skip to content

Commit 689f012

Browse files
committed
Bug fixed, Slim headers array must be reduced before using it with reactPHP
1 parent d77d59b commit 689f012

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

readme.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Introduction
66

77
This library is created in order to use reactPHP as a HTTP server for Slim framework. It will launch a Slim\App process when a request is made, and at the same time it will transfer data from reactPHP native objects into Slim objects. With this, we will be able to create a basic react server for a Slim framework application.
88

9-
Data, cookies and file uploads transmission between react and Slim objects. You can access through slim objects native functions to uploaded files and cookies.
9+
Data, cookies and file uploads transmission between react and Slim objects. You can access through slim native functions to uploaded files, data and cookies.
1010

1111
##Installation
1212
You can install the component in the following ways:
@@ -17,6 +17,8 @@ You can install the component in the following ways:
1717
##Usage
1818
After the composer autoload requirement a Slim\App should be instanced and prepared as usual. Slim\App can be bootstrapped and all dependencies can be injected as you like, after that, a reactphp-slim server should be instanced and call the run method in it, using slim\App as parameter. The reactphp-slim server will act as intermediary and will launch the slim application through the ``process`` method when requested, this method avoids the usual request and response bootstrap made by Slim.
1919

20+
When uploading files, move_uploaded_files() probably won't work, use native object methods to move the file.
21+
2022
```php
2123
require '../vendor/autoload.php';
2224

@@ -31,7 +33,12 @@ $app->any('/hello/{name}', function (
3133
\Slim\Http\Response $response) {
3234

3335
$name = $request->getAttribute('name');
36+
3437
$response->getBody()->write("Hello, $name");
38+
$response->getBody()->write(print_r($request->getParsedBody(), true));
39+
$response->getBody()->write(print_r($request->getCookieParams(), true));
40+
$response->getBody()->write(print_r($request->getHeaders(), true));
41+
// $response->getBody()->write(print_r($request->getUploadedFiles(), true));
3542

3643
return $response;
3744
});
@@ -43,6 +50,10 @@ $server->withHost('192.168.67.1')->withPort(1337)->run($app);
4350

4451
\mbarquin\reactSlim\Server object is the class which is going to configure and launch a ReactPHP server. It has two main methods
4552

53+
54+
**withHost($string)**
55+
Sets the IP to be listened to
56+
4657
**withPort($int)**
4758
Sets the port the server will be listening to, by default it will be set to 1337.
4859

src/Response/SlimResponse.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ class SlimResponse implements ResponseInterface
3636
*/
3737
static function setReactResponse(\React\Http\Response $reactResp, \Slim\Http\Response $slimResponse, $endRequest = false)
3838
{
39-
$reactResp->writeHead(
40-
$slimResponse->getStatusCode(), $slimResponse->getHeaders()
41-
);
39+
$headers = static::reduceHeaders($slimResponse->getHeaders());
40+
$reactResp->writeHead($slimResponse->getStatusCode(), $headers);
4241

4342
$reactResp->write($slimResponse->getBody());
4443

@@ -47,6 +46,27 @@ static function setReactResponse(\React\Http\Response $reactResp, \Slim\Http\Res
4746
}
4847
}
4948

49+
/**
50+
* Reduces slim headers array to be used on reactPHP
51+
*
52+
* @param array $headersArray Headers array given by slim
53+
*
54+
* @return array Ready 4 reactPHP array
55+
*/
56+
static public function reduceHeaders($headersArray)
57+
{
58+
$auxArray = [];
59+
foreach ($headersArray as $name => $value) {
60+
$myContent = '';
61+
foreach($value as $text) {
62+
$myContent .= $text;
63+
}
64+
$auxArray[$name] = $myContent;
65+
}
66+
67+
return $auxArray;
68+
}
69+
5070
/**
5171
* Returns a new Slim response object instance
5272
*

src/Server.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ private function getCallbacks(\Slim\App $app)
120120
if ($continue === false) {
121121
$filesArr = SlimRequest::getSlimFilesArray($server->partials[$boundary]);
122122

123-
$lastRequest = $slRequest->withUploadedFiles($filesArr)->withParsedBody($server->partials[$boundary]['fields']);
123+
$lastRequest = $slRequest
124+
->withUploadedFiles($filesArr)
125+
->withParsedBody($server->partials[$boundary]['fields']);
126+
124127
$slResponse = $app->process($lastRequest, $slResponse);
125128
SlimResponse::setReactResponse($response, $slResponse, true);
126129
}

0 commit comments

Comments
 (0)