-
Notifications
You must be signed in to change notification settings - Fork 18
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 #20 from prolic/expressive3
Support Expressive 3
- Loading branch information
Showing
18 changed files
with
250 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
<?php | ||
declare (strict_types=1); | ||
|
||
namespace PhpMiddleware\PhpDebugBar; | ||
|
||
use DebugBar\JavascriptRenderer as DebugBarRenderer; | ||
use Interop\Http\ServerMiddleware\DelegateInterface; | ||
use Interop\Http\ServerMiddleware\MiddlewareInterface; | ||
use PhpMiddleware\DoublePassCompatibilityTrait; | ||
use Psr\Http\Message\MessageInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Slim\Http\Uri; | ||
use Zend\Diactoros\Response; | ||
use Zend\Diactoros\Response\HtmlResponse; | ||
|
@@ -21,10 +21,8 @@ | |
* | ||
* @author Witold Wasiczko <[email protected]> | ||
*/ | ||
class PhpDebugBarMiddleware implements MiddlewareInterface | ||
final class PhpDebugBarMiddleware implements MiddlewareInterface | ||
{ | ||
use DoublePassCompatibilityTrait; | ||
|
||
protected $debugBarRenderer; | ||
|
||
public function __construct(DebugBarRenderer $debugbarRenderer) | ||
|
@@ -35,13 +33,13 @@ public function __construct(DebugBarRenderer $debugbarRenderer) | |
/** | ||
* @inheritDoc | ||
*/ | ||
public function process(ServerRequestInterface $request, DelegateInterface $delegate) | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
if ($staticFile = $this->getStaticFile($request->getUri())) { | ||
return $staticFile; | ||
} | ||
|
||
$response = $delegate->process($request); | ||
$response = $handler->handle($request); | ||
|
||
if (!$this->isHtmlAccepted($request)) { | ||
return $response; | ||
|
@@ -53,6 +51,26 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele | |
return $this->prepareHtmlResponseWithDebugBar($response); | ||
} | ||
|
||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface | ||
{ | ||
$handler = new class($next, $response) implements RequestHandlerInterface { | ||
private $next; | ||
private $response; | ||
|
||
public function __construct(callable $next, ResponseInterface $response) | ||
{ | ||
$this->next = $next; | ||
$this->response = $response; | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
return ($this->next)($request, $this->response); | ||
} | ||
}; | ||
return $this->process($request, $handler); | ||
} | ||
|
||
/** | ||
* @return HtmlResponse | ||
*/ | ||
|
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,40 @@ | ||
<?php | ||
declare (strict_types=1); | ||
|
||
namespace PhpMiddleware\PhpDebugBar\ResponseInjector; | ||
|
||
use DebugBar\JavascriptRenderer; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Zend\Diactoros\Response\HtmlResponse; | ||
use Zend\Diactoros\Response\Serializer; | ||
|
||
final class AlwaysInjector implements ResponseInjectorInterface | ||
{ | ||
public function injectPhpDebugBar(ResponseInterface $response, JavascriptRenderer $debugBarRenderer): ResponseInterface | ||
{ | ||
$debugBarHead = $debugBarRenderer->renderHead(); | ||
$debugBarBody = $debugBarRenderer->render(); | ||
|
||
if ($this->isHtmlResponse($outResponse)) { | ||
$body = $outResponse->getBody(); | ||
if (! $body->eof() && $body->isSeekable()) { | ||
$body->seek(0, SEEK_END); | ||
} | ||
$body->write($debugBarHead . $debugBarBody); | ||
|
||
return $outResponse; | ||
} | ||
|
||
$outResponseBody = Serializer::toString($outResponse); | ||
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>'; | ||
$escapedOutResponseBody = htmlspecialchars($outResponseBody); | ||
$result = sprintf($template, $debugBarHead, $escapedOutResponseBody, $debugBarBody); | ||
|
||
return new HtmlResponse($result); | ||
} | ||
|
||
private function isHtmlResponse(ResponseInterface $response): bool | ||
{ | ||
return $this->hasHeaderContains($response, 'Content-Type', 'text/html'); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
declare (strict_types=1); | ||
|
||
namespace PhpMiddleware\PhpDebugBar\ResponseInjector; | ||
|
||
use DebugBar\JavascriptRenderer; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @author Witold Wasiczko <[email protected]> | ||
*/ | ||
interface ResponseInjectorInterface | ||
{ | ||
public function injectPhpDebugBar(ResponseInterface $response, JavascriptRenderer $debugBar): ResponseInterface; | ||
} |
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
Oops, something went wrong.