-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from inhere/master
update readme. add new class, some modify
- Loading branch information
Showing
9 changed files
with
104 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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]) | ||
)); | ||
} | ||
|
||
|
@@ -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; | ||
|