Runner.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Http;
  17. use Cake\Routing\Router;
  18. use Cake\Routing\RoutingApplicationInterface;
  19. use Psr\Http\Message\ResponseInterface;
  20. use Psr\Http\Message\ServerRequestInterface;
  21. use Psr\Http\Server\RequestHandlerInterface;
  22. /**
  23. * Executes the middleware queue and provides the `next` callable
  24. * that allows the queue to be iterated.
  25. */
  26. class Runner implements RequestHandlerInterface
  27. {
  28. /**
  29. * The middleware queue being run.
  30. *
  31. * @var \Cake\Http\MiddlewareQueue
  32. */
  33. protected $queue;
  34. /**
  35. * Fallback handler to use if middleware queue does not generate response.
  36. *
  37. * @var \Psr\Http\Server\RequestHandlerInterface|null
  38. */
  39. protected $fallbackHandler;
  40. /**
  41. * @param \Cake\Http\MiddlewareQueue $queue The middleware queue
  42. * @param \Psr\Http\Message\ServerRequestInterface $request The Server Request
  43. * @param \Psr\Http\Server\RequestHandlerInterface|null $fallbackHandler Fallback request handler.
  44. * @return \Psr\Http\Message\ResponseInterface A response object
  45. */
  46. public function run(
  47. MiddlewareQueue $queue,
  48. ServerRequestInterface $request,
  49. ?RequestHandlerInterface $fallbackHandler = null
  50. ): ResponseInterface {
  51. $this->queue = $queue;
  52. $this->queue->rewind();
  53. $this->fallbackHandler = $fallbackHandler;
  54. if (
  55. $fallbackHandler instanceof RoutingApplicationInterface &&
  56. $request instanceof ServerRequest
  57. ) {
  58. Router::setRequest($request);
  59. }
  60. return $this->handle($request);
  61. }
  62. /**
  63. * Handle incoming server request and return a response.
  64. *
  65. * @param \Psr\Http\Message\ServerRequestInterface $request The server request
  66. * @return \Psr\Http\Message\ResponseInterface An updated response
  67. */
  68. public function handle(ServerRequestInterface $request): ResponseInterface
  69. {
  70. if ($this->queue->valid()) {
  71. $middleware = $this->queue->current();
  72. $this->queue->next();
  73. return $middleware->process($request, $this);
  74. }
  75. if ($this->fallbackHandler) {
  76. return $this->fallbackHandler->handle($request);
  77. }
  78. return new Response([
  79. 'body' => 'Middleware queue was exhausted without returning a response '
  80. . 'and no fallback request handler was set for Runner',
  81. 'status' => 500,
  82. ]);
  83. }
  84. }