Skip to content

Commit

Permalink
Merge pull request #8 from inhere/master
Browse files Browse the repository at this point in the history
update readme. add new class, some modify
  • Loading branch information
inhere authored Apr 4, 2018
2 parents 709a989 + 02a4b2a commit 67a7ae0
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 50 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

Swoft Http server Component

# Install
## Install

- composer command

```bash
composer require swoft/http-server
```

# Document
## Document

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

# Unit testing
## Unit testing

```bash
phpunit
```

# LICENSE
## LICENSE

The Component is open-sourced software licensed under the [Apache license](LICENSE).
4 changes: 2 additions & 2 deletions src/Bean/Parser/RequestMappingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class RequestMappingParser extends AbstractParser
* @param string $propertyName
* @param string $methodName
* @param null|mixed $propertyValue
* @return mixed
*/
public function parser(
string $className,
Expand All @@ -36,7 +35,8 @@ public function parser(
$propertyValue = null
) {
$collector = ControllerCollector::getCollector();
if (! isset($collector[$className])) {

if (!isset($collector[$className])) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ protected function registerRpcEvent()
* onRequest event callback
* Each request will create an coroutine
*
* @param Request $request
* @param Request $request
* @param Response $response
* @throws \InvalidArgumentException
*/
public function onRequest(Request $request, Response $response)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/AcceptMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
class AcceptMiddleware implements MiddlewareInterface
{

use AcceptTrait;

/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Server\RequestHandlerInterface $handler
* @return \Psr\Http\Message\ResponseInterface
* @throws \InvalidArgumentException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Expand All @@ -30,4 +30,4 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $response;
}

}
}
16 changes: 7 additions & 9 deletions src/Middleware/AcceptTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,27 @@ trait AcceptTrait
*/
protected $acceptJson = 'application/json';


/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @return \Psr\Http\Message\ResponseInterface
* @return \Psr\Http\Message\ResponseInterface|Response
* @throws \InvalidArgumentException
*/
protected function handleAccept(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
// Only handle HTTP-Server Response
if (! $response instanceof Response) {
if (!$response instanceof Response) {
return $response;
}

// View
$content = $response->getAttribute(AttributeEnum::RESPONSE_ATTRIBUTE);
if ($content === null) {
// View(has been handled by ViewMiddleware)
$data = $response->getAttribute(AttributeEnum::RESPONSE_ATTRIBUTE);
if ($data === null) {
return $response;
}

$accepts = $request->getHeader('accept');
$currentAccept = current($accepts);
$data = $response->getAttribute(AttributeEnum::RESPONSE_ATTRIBUTE);
$currentAccept = \current($accepts);

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

if (! empty($data)) {
if (!empty($data)) {
return $response->raw((string)$data);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/HandlerAdapterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HandlerAdapterMiddleware implements MiddlewareInterface
* @param \Psr\Http\Server\RequestHandlerInterface $handler
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \Swoft\Exception\InvalidArgumentException
* @throws \Swoft\Http\Server\Exception\RouteNotFoundException
* @throws \Swoft\Http\Server\Exception\MethodNotAllowedException
* @throws \InvalidArgumentException
Expand All @@ -36,8 +37,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

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

return $response;
return $handlerAdapter->doHandler($request, $httpHandler);
}
}
28 changes: 6 additions & 22 deletions src/Middleware/SwoftMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
use Swoft\Http\Server\Exception\NotAcceptableException;
use Swoft\Http\Message\Middleware\MiddlewareInterface;


/**
* @Bean()
* Merge all swoft middleware to this one middleware for performance
*
* @Bean()
*/
class SwoftMiddleware implements MiddlewareInterface
{

use AcceptTrait, RouterTrait;

/**
Expand All @@ -34,38 +30,26 @@ class SwoftMiddleware implements MiddlewareInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
/**
* Fix Chrome ico request bug
*/
// Fix Chrome ico request bug
if ($request->getUri()->getPath() === '/favicon.ico') {
throw new NotAcceptableException('access favicon.ico');
}

/**
* Parser
*/
// Parser
/* @var \Swoft\Http\Server\Parser\RequestParserInterface $requestParser */
$requestParser = App::getBean('requestParser');
$request = $requestParser->parse($request);

/**
* Router
*/
// Router
$request = $this->handleRouter($request);

/**
* Delegate to next handler
*/
// Delegate to next handler
$response = $handler->handle($request);

/**
* Power by
*/
// Power by
$response = $response->withAddedHeader('X-Powered-By', 'Swoft');

/**
* Response handler, according to Accept
*/
// Response handler, according to Accept
$response = $this->handleAccept($request, $response);

return $response;
Expand Down
66 changes: 66 additions & 0 deletions src/Payload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Swoft\Http\Server;

/**
* Class Payload
* - 使用它代替返回原始数据
* - 可以设置 http status
* @package Swoft\Http\Server
*/
class Payload
{
/**
* @var int The http status for response
*/
private $status = 200;

/**
* @var mixed The body data for response
*/
public $data;

/**
* @param mixed $data
* @param int $status
* @return static
*/
public static function make($data = null, int $status = 0): self
{
if ($status) {
$self = new static($data);

return $self->setStatus($status);
}

return new static($data);
}

/**
* Payload constructor.
* @param null $data
*/
public function __construct($data = null)
{
$this->data = $data;
}

/**
* @return int
*/
public function getStatus(): int
{
return $this->status;
}

/**
* @param int $status
* @return Payload
*/
public function setStatus(int $status): self
{
$this->status = $status;

return $this;
}
}
21 changes: 13 additions & 8 deletions src/Router/HandlerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
use Swoft\Http\Server\AttributeEnum;
use Swoft\Http\Server\Exception\MethodNotAllowedException;
use Swoft\Http\Server\Exception\RouteNotFoundException;
use Swoft\Http\Server\Payload;

/**
* http handler adapter
*
* @Bean("httpHandlerAdapter")
* @uses HandlerAdapterMiddleware
* @version 2017年11月23日
* @author stelin <[email protected]>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
class HandlerAdapter implements HandlerAdapterInterface
{
Expand Down Expand Up @@ -58,7 +55,7 @@ public function doHandler(ServerRequestInterface $request, array $routeInfo)
"Method '%s' not allowed for access %s, Allow: %s",
$request->getMethod(),
$path,
implode(',', $routeInfo[2])
\implode(',', $routeInfo[2])
));
}

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

// response
if (!$response instanceof Response) {
/* @var Response $contextResponse*/
$contextResponse = RequestContext::getResponse();
$response = $contextResponse->withAttribute(AttributeEnum::RESPONSE_ATTRIBUTE , $response);
/* @var Response $newResponse*/
$newResponse = RequestContext::getResponse();

// if is Payload
if ($response instanceof Payload) {
$response = $newResponse
->withStatus($response->getStatus())
->withAttribute(AttributeEnum::RESPONSE_ATTRIBUTE , $response->data);
} else {
$response = $newResponse->withAttribute(AttributeEnum::RESPONSE_ATTRIBUTE , $response);
}
}

return $response;
Expand Down

0 comments on commit 67a7ae0

Please sign in to comment.