generated from yiisoft/package-template
-
-
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.
Wrap application's fallback handler to provider debug headers
- Loading branch information
Showing
5 changed files
with
97 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Api\Debug\Http; | ||
|
||
use Closure; | ||
use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugFallbackHandler; | ||
use Yiisoft\Yii\Http\Application; | ||
|
||
final readonly class DebugHttpApplicationWrapper | ||
{ | ||
public function __construct( | ||
private DebugFallbackHandler $debugFallbackHandler | ||
) { | ||
} | ||
|
||
public function wrap(Application $application): void | ||
{ | ||
$debugFallbackHandler = $this->debugFallbackHandler; | ||
$closure = Closure::bind(static function (Application $application) use ($debugFallbackHandler) { | ||
$application->fallbackHandler = $debugFallbackHandler->withFallbackRequestHandler($application->fallbackHandler); | ||
}, null, $application); | ||
|
||
$closure($application); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Debug\Api\Debug\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; | ||
|
||
final class DebugFallbackHandler implements MiddlewareInterface, RequestHandlerInterface | ||
{ | ||
private ?RequestHandlerInterface $fallbackHandler = null; | ||
|
||
public function __construct( | ||
private readonly MiddlewareDispatcher $middlewareDispatcher, | ||
private readonly array $middlewareDefinitions, | ||
) { | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
return $this->middlewareDispatcher | ||
->withMiddlewares($this->middlewareDefinitions) | ||
->dispatch($request, $handler); | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
if ($this->fallbackHandler === null) { | ||
throw new \RuntimeException('No fallback handler defined.'); | ||
} | ||
|
||
return $this->process($request, $this->fallbackHandler); | ||
} | ||
|
||
public function withFallbackRequestHandler(RequestHandlerInterface $fallbackHandler): self | ||
{ | ||
$new = clone $this; | ||
$new->fallbackHandler = $fallbackHandler; | ||
|
||
return $new; | ||
} | ||
} |
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