Skip to content

Commit 02a4b2a

Browse files
committed
update readme. add new class, some modify
1 parent 709a989 commit 02a4b2a

9 files changed

Lines changed: 104 additions & 50 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
Swoft Http server Component
44

5-
# Install
5+
## Install
66

77
- composer command
88

99
```bash
1010
composer require swoft/http-server
1111
```
1212

13-
# Document
13+
## Document
1414

1515
Please see [document site](https://doc.swoft.org)
1616

17-
# Unit testing
17+
## Unit testing
1818

1919
```bash
2020
phpunit
2121
```
2222

23-
# LICENSE
23+
## LICENSE
2424

2525
The Component is open-sourced software licensed under the [Apache license](LICENSE).

src/Bean/Parser/RequestMappingParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class RequestMappingParser extends AbstractParser
2626
* @param string $propertyName
2727
* @param string $methodName
2828
* @param null|mixed $propertyValue
29-
* @return mixed
3029
*/
3130
public function parser(
3231
string $className,
@@ -36,7 +35,8 @@ public function parser(
3635
$propertyValue = null
3736
) {
3837
$collector = ControllerCollector::getCollector();
39-
if (! isset($collector[$className])) {
38+
39+
if (!isset($collector[$className])) {
4040
return;
4141
}
4242

src/Http/HttpServer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ protected function registerRpcEvent()
7777
* onRequest event callback
7878
* Each request will create an coroutine
7979
*
80-
* @param Request $request
80+
* @param Request $request
8181
* @param Response $response
82+
* @throws \InvalidArgumentException
8283
*/
8384
public function onRequest(Request $request, Response $response)
8485
{

src/Middleware/AcceptMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
class AcceptMiddleware implements MiddlewareInterface
1717
{
18-
1918
use AcceptTrait;
2019

2120
/**
2221
* @param \Psr\Http\Message\ServerRequestInterface $request
2322
* @param \Psr\Http\Server\RequestHandlerInterface $handler
2423
* @return \Psr\Http\Message\ResponseInterface
24+
* @throws \InvalidArgumentException
2525
*/
2626
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
2727
{
@@ -30,4 +30,4 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3030
return $response;
3131
}
3232

33-
}
33+
}

src/Middleware/AcceptTrait.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,27 @@ trait AcceptTrait
2020
*/
2121
protected $acceptJson = 'application/json';
2222

23-
2423
/**
2524
* @param \Psr\Http\Message\ServerRequestInterface $request
2625
* @param \Psr\Http\Message\ResponseInterface $response
27-
* @return \Psr\Http\Message\ResponseInterface
26+
* @return \Psr\Http\Message\ResponseInterface|Response
2827
* @throws \InvalidArgumentException
2928
*/
3029
protected function handleAccept(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
3130
{
3231
// Only handle HTTP-Server Response
33-
if (! $response instanceof Response) {
32+
if (!$response instanceof Response) {
3433
return $response;
3534
}
3635

37-
// View
38-
$content = $response->getAttribute(AttributeEnum::RESPONSE_ATTRIBUTE);
39-
if ($content === null) {
36+
// View(has been handled by ViewMiddleware)
37+
$data = $response->getAttribute(AttributeEnum::RESPONSE_ATTRIBUTE);
38+
if ($data === null) {
4039
return $response;
4140
}
4241

4342
$accepts = $request->getHeader('accept');
44-
$currentAccept = current($accepts);
45-
$data = $response->getAttribute(AttributeEnum::RESPONSE_ATTRIBUTE);
43+
$currentAccept = \current($accepts);
4644

4745
if (empty($currentAccept)) {
4846
if ($response->isArrayable($data)) {
@@ -60,7 +58,7 @@ protected function handleAccept(ServerRequestInterface $request, ResponseInterfa
6058
return $response->json($data);
6159
}
6260

63-
if (! empty($data)) {
61+
if (!empty($data)) {
6462
return $response->raw((string)$data);
6563
}
6664

src/Middleware/HandlerAdapterMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class HandlerAdapterMiddleware implements MiddlewareInterface
2525
* @param \Psr\Http\Server\RequestHandlerInterface $handler
2626
*
2727
* @return \Psr\Http\Message\ResponseInterface
28+
* @throws \Swoft\Exception\InvalidArgumentException
2829
* @throws \Swoft\Http\Server\Exception\RouteNotFoundException
2930
* @throws \Swoft\Http\Server\Exception\MethodNotAllowedException
3031
* @throws \InvalidArgumentException
@@ -36,8 +37,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3637

3738
/* @var HandlerAdapter $handlerAdapter */
3839
$handlerAdapter = App::getBean('httpHandlerAdapter');
39-
$response = $handlerAdapter->doHandler($request, $httpHandler);
4040

41-
return $response;
41+
return $handlerAdapter->doHandler($request, $httpHandler);
4242
}
4343
}

src/Middleware/SwoftMiddleware.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
use Swoft\Http\Server\Exception\NotAcceptableException;
1111
use Swoft\Http\Message\Middleware\MiddlewareInterface;
1212

13-
1413
/**
1514
* @Bean()
1615
* Merge all swoft middleware to this one middleware for performance
17-
*
18-
* @Bean()
1916
*/
2017
class SwoftMiddleware implements MiddlewareInterface
2118
{
22-
2319
use AcceptTrait, RouterTrait;
2420

2521
/**
@@ -34,38 +30,26 @@ class SwoftMiddleware implements MiddlewareInterface
3430
*/
3531
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
3632
{
37-
/**
38-
* Fix Chrome ico request bug
39-
*/
33+
// Fix Chrome ico request bug
4034
if ($request->getUri()->getPath() === '/favicon.ico') {
4135
throw new NotAcceptableException('access favicon.ico');
4236
}
4337

44-
/**
45-
* Parser
46-
*/
38+
// Parser
4739
/* @var \Swoft\Http\Server\Parser\RequestParserInterface $requestParser */
4840
$requestParser = App::getBean('requestParser');
4941
$request = $requestParser->parse($request);
5042

51-
/**
52-
* Router
53-
*/
43+
// Router
5444
$request = $this->handleRouter($request);
5545

56-
/**
57-
* Delegate to next handler
58-
*/
46+
// Delegate to next handler
5947
$response = $handler->handle($request);
6048

61-
/**
62-
* Power by
63-
*/
49+
// Power by
6450
$response = $response->withAddedHeader('X-Powered-By', 'Swoft');
6551

66-
/**
67-
* Response handler, according to Accept
68-
*/
52+
// Response handler, according to Accept
6953
$response = $this->handleAccept($request, $response);
7054

7155
return $response;

src/Payload.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Swoft\Http\Server;
4+
5+
/**
6+
* Class Payload
7+
* - 使用它代替返回原始数据
8+
* - 可以设置 http status
9+
* @package Swoft\Http\Server
10+
*/
11+
class Payload
12+
{
13+
/**
14+
* @var int The http status for response
15+
*/
16+
private $status = 200;
17+
18+
/**
19+
* @var mixed The body data for response
20+
*/
21+
public $data;
22+
23+
/**
24+
* @param mixed $data
25+
* @param int $status
26+
* @return static
27+
*/
28+
public static function make($data = null, int $status = 0): self
29+
{
30+
if ($status) {
31+
$self = new static($data);
32+
33+
return $self->setStatus($status);
34+
}
35+
36+
return new static($data);
37+
}
38+
39+
/**
40+
* Payload constructor.
41+
* @param null $data
42+
*/
43+
public function __construct($data = null)
44+
{
45+
$this->data = $data;
46+
}
47+
48+
/**
49+
* @return int
50+
*/
51+
public function getStatus(): int
52+
{
53+
return $this->status;
54+
}
55+
56+
/**
57+
* @param int $status
58+
* @return Payload
59+
*/
60+
public function setStatus(int $status): self
61+
{
62+
$this->status = $status;
63+
64+
return $this;
65+
}
66+
}

src/Router/HandlerAdapter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@
1414
use Swoft\Http\Server\AttributeEnum;
1515
use Swoft\Http\Server\Exception\MethodNotAllowedException;
1616
use Swoft\Http\Server\Exception\RouteNotFoundException;
17+
use Swoft\Http\Server\Payload;
1718

1819
/**
1920
* http handler adapter
2021
*
2122
* @Bean("httpHandlerAdapter")
22-
* @uses HandlerAdapterMiddleware
23-
* @version 2017年11月23日
2423
* @author stelin <phpcrazy@126.com>
25-
* @copyright Copyright 2010-2016 swoft software
26-
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
2724
*/
2825
class HandlerAdapter implements HandlerAdapterInterface
2926
{
@@ -58,7 +55,7 @@ public function doHandler(ServerRequestInterface $request, array $routeInfo)
5855
"Method '%s' not allowed for access %s, Allow: %s",
5956
$request->getMethod(),
6057
$path,
61-
implode(',', $routeInfo[2])
58+
\implode(',', $routeInfo[2])
6259
));
6360
}
6461

@@ -75,9 +72,17 @@ public function doHandler(ServerRequestInterface $request, array $routeInfo)
7572

7673
// response
7774
if (!$response instanceof Response) {
78-
/* @var Response $contextResponse*/
79-
$contextResponse = RequestContext::getResponse();
80-
$response = $contextResponse->withAttribute(AttributeEnum::RESPONSE_ATTRIBUTE , $response);
75+
/* @var Response $newResponse*/
76+
$newResponse = RequestContext::getResponse();
77+
78+
// if is Payload
79+
if ($response instanceof Payload) {
80+
$response = $newResponse
81+
->withStatus($response->getStatus())
82+
->withAttribute(AttributeEnum::RESPONSE_ATTRIBUTE , $response->data);
83+
} else {
84+
$response = $newResponse->withAttribute(AttributeEnum::RESPONSE_ATTRIBUTE , $response);
85+
}
8186
}
8287

8388
return $response;

0 commit comments

Comments
 (0)