Skip to content

Commit c13221f

Browse files
committed
first commit
0 parents  commit c13221f

File tree

11 files changed

+351
-0
lines changed

11 files changed

+351
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.phpunit
2+
phpunit.xml
3+
vendor/
4+
composer.lock
5+
composer.phar
6+
.php_cs.cache
7+
/build

.php_cs.dist

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
$header = <<<'EOF'
3+
This file is part of the swoole-http-message-bridge
4+
5+
(c) Indra Gunawan <hello@indra.my.id>
6+
7+
For the full copyright and license information, please view the LICENSE
8+
file that was distributed with this source code.
9+
EOF;
10+
11+
return PhpCsFixer\Config::create()
12+
->setRiskyAllowed(true)
13+
->setRules([
14+
'@Symfony' => true,
15+
'@Symfony:risky' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'combine_consecutive_unsets' => true,
18+
// one should use PHPUnit methods to set up expected exception instead of annotations
19+
'general_phpdoc_annotation_remove' => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'],
20+
'header_comment' => ['header' => $header],
21+
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
22+
'no_useless_else' => true,
23+
'no_useless_return' => true,
24+
'ordered_imports' => true,
25+
'php_unit_strict' => true,
26+
'phpdoc_add_missing_param_annotation' => true,
27+
'psr4' => true,
28+
'strict_comparison' => true,
29+
'strict_param' => true,
30+
'lowercase_constants' => true,
31+
'lowercase_keywords' => true,
32+
'visibility_required' => true,
33+
])
34+
->setFinder(PhpCsFixer\Finder::create()->in(__DIR__))
35+
;

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Indra Gunawan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Swoole Http Message Bridge
2+
3+
Provides integration Swoole Http Request / Response to Symfony Request / Response.

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "indragunawan/swoole-http-message-bridge",
3+
"description": "Swoole Http Message Bridge",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Indra Gunawan",
9+
"email": "hello@indra.my.id"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"Indragunawan\\SwooleHttpMessageBridge\\": "src/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"Indragunawan\\SwooleHttpMessageBridge\\Tests\\": "tests/"
20+
}
21+
},
22+
"require": {
23+
"php": "^7.0",
24+
"symfony/http-foundation": "^2.7 || ^3.0 || ^4.0"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^6.5"
28+
}
29+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="Indragunawan Swoole HTTP Message Bridge Test Suite">
16+
<directory>./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<filter>
21+
<whitelist>
22+
<directory>./</directory>
23+
<exclude>
24+
<directory>./tests</directory>
25+
<directory>./vendor</directory>
26+
</exclude>
27+
</whitelist>
28+
</filter>
29+
</phpunit>

src/Request.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the swoole-http-message-bridge
5+
*
6+
* (c) Indra Gunawan <hello@indra.my.id>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Indragunawan\SwooleHttpMessageBridge;
13+
14+
use Swoole\Http\Request as SwooleRequest;
15+
use Symfony\Component\HttpFoundation\ParameterBag;
16+
use Symfony\Component\HttpFoundation\Request as SfRequest;
17+
18+
/**
19+
* @author Indra Gunawan <hello@indra.my.id>
20+
*/
21+
class Request
22+
{
23+
/**
24+
* Creates new Symfony request with values from SwooleRequest.
25+
*
26+
* @param SwooleRequest $swooleRequest
27+
*
28+
* @return SfRequest
29+
*/
30+
public static function createFromSwooleRequest(SwooleRequest $swooleRequest): SfRequest
31+
{
32+
$headers = array_combine(array_map(function ($key) {
33+
return 'HTTP_'.str_replace('-', '_', $key);
34+
}, array_keys($swooleRequest->header)), array_values($swooleRequest->header));
35+
36+
$server = array_change_key_case(array_merge($swooleRequest->server, $headers), CASE_UPPER);
37+
38+
if ($trustedProxies = $server['TRUSTED_PROXIES'] ?? false) {
39+
SfRequest::setTrustedProxies(explode(',', $trustedProxies), self::HEADER_X_FORWARDED_ALL ^ self::HEADER_X_FORWARDED_HOST);
40+
}
41+
42+
if ($trustedHosts = $server['TRUSTED_HOSTS'] ?? false) {
43+
SfRequest::setTrustedHosts(explode(',', $trustedHosts));
44+
}
45+
46+
$sfRequest = new SfRequest(
47+
$swooleRequest->get ?? [],
48+
$swooleRequest->post ?? [],
49+
[],
50+
$swooleRequest->cookie ?? [],
51+
$swooleRequest->files ?? [],
52+
$server,
53+
$swooleRequest->rawContent()
54+
);
55+
56+
if (0 === strpos($sfRequest->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
57+
&& in_array(strtoupper($sfRequest->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'], true)
58+
) {
59+
parse_str($sfRequest->getContent(), $data);
60+
$sfRequest->request = new ParameterBag($data);
61+
}
62+
63+
return $sfRequest;
64+
}
65+
}

src/Response.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the swoole-http-message-bridge
5+
*
6+
* (c) Indra Gunawan <hello@indra.my.id>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Indragunawan\SwooleHttpMessageBridge;
13+
14+
use Swoole\Http\Response as SwooleResponse;
15+
use Symfony\Component\HttpFoundation\Response as SfResponse;
16+
17+
/**
18+
* @author Indra Gunawan <hello@indra.my.id>
19+
*/
20+
class Response
21+
{
22+
/**
23+
* Writes SwooleResponse with values from SfResponse.
24+
*
25+
* @param SwooleResponse $swooleResponse
26+
* @param SfResponse $sfResponse
27+
*/
28+
public static function writeSwooleResponse(SwooleResponse $swooleResponse, SfResponse $sfResponse)
29+
{
30+
// write headers
31+
self::writeHeaders($swooleResponse, $sfResponse);
32+
33+
// write content
34+
$swooleResponse->write($sfResponse->getContent());
35+
}
36+
37+
/**
38+
* Writes SwooleResponse headers with values from SfResponse headers.
39+
*
40+
* @param SwooleResponse $swooleResponse
41+
* @param SfResponse $sfResponse
42+
*/
43+
protected static function writeHeaders(SwooleResponse $swooleResponse, SfResponse $sfResponse)
44+
{
45+
// headers have already been sent by the developer
46+
if (headers_sent()) {
47+
return;
48+
}
49+
50+
// headers
51+
foreach ($sfResponse->headers->allPreserveCaseWithoutCookies() as $name => $values) {
52+
foreach ($values as $value) {
53+
$swooleResponse->header($name, $value);
54+
}
55+
}
56+
57+
// status
58+
$swooleResponse->status($sfResponse->getStatusCode());
59+
60+
// cookies
61+
foreach ($sfResponse->headers->getCookies() as $cookie) {
62+
$swooleResponse->cookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
63+
}
64+
}
65+
}

tests/RequestTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the swoole-http-message-bridge
5+
*
6+
* (c) Indra Gunawan <hello@indra.my.id>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Indragunawan\SwooleHttpMessageBridge\Tests;
13+
14+
use Indragunawan\SwooleHttpMessageBridge\Request;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @author Indra Gunawan <hello@indra.my.id>
19+
*/
20+
class RequestTest extends TestCase
21+
{
22+
public function testCreateRequest()
23+
{
24+
$swooleRequest = $this->createMock(\Swoole\Http\Request::class);
25+
$swooleRequest->header = [
26+
'user-agent' => 'Swolle',
27+
];
28+
29+
$swooleRequest->server = [
30+
'request_method' => 'GET',
31+
];
32+
33+
$swooleRequest->get = [
34+
'k' => 'v',
35+
];
36+
37+
$request = Request::createFromSwooleRequest($swooleRequest);
38+
39+
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\Request::class, $request);
40+
$this->assertSame(1, $request->headers->count());
41+
$this->assertSame(2, $request->server->count());
42+
$this->assertSame('GET', $request->getMethod());
43+
$this->assertArrayHasKey('HTTP_USER_AGENT', $request->server->all());
44+
}
45+
46+
public function testCreateRequestWithContent()
47+
{
48+
$swooleRequest = $this->createMock(\Swoole\Http\Request::class);
49+
$swooleRequest->header = [
50+
'user-agent' => 'Swoole',
51+
'content-type' => 'application/x-www-form-urlencoded',
52+
];
53+
54+
$swooleRequest->server = [
55+
'request_method' => 'PUT',
56+
];
57+
58+
$swooleRequest->method('rawContent')
59+
->willReturn('a=av&b=bv');
60+
61+
$request = Request::createFromSwooleRequest($swooleRequest);
62+
63+
$this->assertSame(2, $request->request->count());
64+
}
65+
}

0 commit comments

Comments
 (0)