Server.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Http;
  16. use Cake\Event\EventDispatcherTrait;
  17. use Psr\Http\Message\ResponseInterface;
  18. use Psr\Http\Message\ServerRequestInterface;
  19. use RuntimeException;
  20. use Zend\Diactoros\Response;
  21. use Zend\Diactoros\Response\EmitterInterface;
  22. use Zend\Diactoros\Response\SapiStreamEmitter;
  23. /**
  24. * Runs an application invoking all the PSR7 middleware and the registered application.
  25. */
  26. class Server
  27. {
  28. use EventDispatcherTrait;
  29. /**
  30. * @var \Cake\Http\BaseApplication
  31. */
  32. protected $app;
  33. /**
  34. * @var \Cake\Http\Runner
  35. */
  36. protected $runner;
  37. /**
  38. * Constructor
  39. *
  40. * @param \Cake\Http\BaseApplication $app The application to use.
  41. */
  42. public function __construct(BaseApplication $app)
  43. {
  44. $this->setApp($app);
  45. $this->setRunner(new Runner());
  46. }
  47. /**
  48. * Run the request/response through the Application and its middleware.
  49. *
  50. * This will invoke the following methods:
  51. *
  52. * - App->bootstrap() - Perform any bootstrapping logic for your application here.
  53. * - App->middleware() - Attach any application middleware here.
  54. * - Trigger the 'Server.buildMiddleware' event. You can use this to modify the
  55. * from event listeners.
  56. * - Run the middleware queue including the application.
  57. *
  58. * @param \Psr\Http\Message\ServerRequestInterface $request The request to use or null.
  59. * @param \Psr\Http\Message\ResponseInterface $response The response to use or null.
  60. * @return \Psr\Http\Message\ResponseInterface
  61. * @throws \RuntimeException When the application does not make a response.
  62. */
  63. public function run(ServerRequestInterface $request = null, ResponseInterface $response = null)
  64. {
  65. $this->app->bootstrap();
  66. $request = $request ?: ServerRequestFactory::fromGlobals();
  67. $response = $response ?: new Response();
  68. $middleware = $this->app->middleware(new MiddlewareQueue());
  69. if (!($middleware instanceof MiddlewareQueue)) {
  70. throw new RuntimeException('The application `middleware` method did not return a middleware queue.');
  71. }
  72. $this->dispatchEvent('Server.buildMiddleware', ['middleware' => $middleware]);
  73. $middleware->add($this->app);
  74. $response = $this->runner->run($middleware, $request, $response);
  75. if (!($response instanceof ResponseInterface)) {
  76. throw new RuntimeException(sprintf(
  77. 'Application did not create a response. Got "%s" instead.',
  78. is_object($response) ? get_class($response) : $response
  79. ));
  80. }
  81. return $response;
  82. }
  83. /**
  84. * Emit the response using the PHP SAPI.
  85. *
  86. * @param \Psr\Http\Message\ResponseInterface $response The response to emit
  87. * @param \Zend\Diactoros\Response\EmitterInterface $emitter The emitter to use.
  88. * When null, a SAPI Stream Emitter will be used.
  89. * @return void
  90. */
  91. public function emit(ResponseInterface $response, EmitterInterface $emitter = null)
  92. {
  93. if (!$emitter) {
  94. $emitter = new SapiStreamEmitter();
  95. }
  96. $emitter->emit($response);
  97. }
  98. /**
  99. * Set the application.
  100. *
  101. * @param BaseApplication $app The application to set.
  102. * @return $this
  103. */
  104. public function setApp(BaseApplication $app)
  105. {
  106. $this->app = $app;
  107. return $this;
  108. }
  109. /**
  110. * Get the current application.
  111. *
  112. * @return BaseApplication The application that will be run.
  113. */
  114. public function getApp()
  115. {
  116. return $this->app;
  117. }
  118. /**
  119. * Set the runner
  120. *
  121. * @param \Cake\Http\Runner $runner The runner to use.
  122. * @return $this
  123. */
  124. public function setRunner(Runner $runner)
  125. {
  126. $this->runner = $runner;
  127. return $this;
  128. }
  129. }