BaseApplication.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Routing\DispatcherFactory;
  17. use Psr\Http\Message\ResponseInterface;
  18. use Psr\Http\Message\ServerRequestInterface;
  19. /**
  20. * Base class for application classes.
  21. *
  22. * The application class is responsible for bootstrapping the application,
  23. * and ensuring that middleware is attached. It is also invoked as the last piece
  24. * of middleware, and delegates request/response handling to the correct controller.
  25. */
  26. abstract class BaseApplication
  27. {
  28. /**
  29. * @var string Contains the path of the config directory
  30. */
  31. protected $configDir;
  32. /**
  33. * Constructor
  34. *
  35. * @param string $configDir The directory the bootstrap configuration is held in.
  36. */
  37. public function __construct($configDir)
  38. {
  39. $this->configDir = $configDir;
  40. }
  41. /**
  42. * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
  43. * @return \Cake\Http\MiddlewareQueue
  44. */
  45. abstract public function middleware($middleware);
  46. /**
  47. * Load all the application configuration and bootstrap logic.
  48. *
  49. * Override this method to add additional bootstrap logic for your application.
  50. *
  51. * @return void
  52. */
  53. public function bootstrap()
  54. {
  55. require_once $this->configDir . '/bootstrap.php';
  56. }
  57. /**
  58. * Invoke the application.
  59. *
  60. * - Convert the PSR response into CakePHP equivalents.
  61. * - Create the controller that will handle this request.
  62. * - Invoke the controller.
  63. *
  64. * @param \Psr\Http\Message\ServerRequestInterface $request The request
  65. * @param \Psr\Http\Message\ResponseInterface $response The response
  66. * @param callable $next The next middleware
  67. * @return \Psr\Http\Message\ResponseInterface
  68. */
  69. public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
  70. {
  71. return $this->getDispatcher()->dispatch($request, $response);
  72. }
  73. /**
  74. * Get the ActionDispatcher.
  75. *
  76. * @return \Cake\Http\ActionDispatcher
  77. */
  78. protected function getDispatcher()
  79. {
  80. return new ActionDispatcher(null, null, DispatcherFactory::filters());
  81. }
  82. }