BaseApplication.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Http;
  16. use Cake\Core\ConsoleApplicationInterface;
  17. use Cake\Core\HttpApplicationInterface;
  18. use Cake\Routing\DispatcherFactory;
  19. use Psr\Http\Message\ResponseInterface;
  20. use Psr\Http\Message\ServerRequestInterface;
  21. /**
  22. * Base class for application classes.
  23. *
  24. * The application class is responsible for bootstrapping the application,
  25. * and ensuring that middleware is attached. It is also invoked as the last piece
  26. * of middleware, and delegates request/response handling to the correct controller.
  27. */
  28. abstract class BaseApplication implements ConsoleApplicationInterface, HttpApplicationInterface
  29. {
  30. /**
  31. * @var string Contains the path of the config directory
  32. */
  33. protected $configDir;
  34. /**
  35. * Constructor
  36. *
  37. * @param string $configDir The directory the bootstrap configuration is held in.
  38. */
  39. public function __construct($configDir)
  40. {
  41. $this->configDir = $configDir;
  42. }
  43. /**
  44. * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
  45. * @return \Cake\Http\MiddlewareQueue
  46. */
  47. abstract public function middleware($middleware);
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function bootstrap()
  52. {
  53. require_once $this->configDir . '/bootstrap.php';
  54. }
  55. /**
  56. * {@inheritDoc}
  57. *
  58. * By default this will load `config/routes.php` for ease of use and backwards compatibility.
  59. *
  60. * @param \Cake\Routing\RouteBuilder $routes A route builder to add routes into.
  61. * @return void
  62. */
  63. public function routes($routes)
  64. {
  65. require $this->configDir . '/routes.php';
  66. }
  67. /**
  68. * Define the console commands for an application.
  69. *
  70. * By default all commands in CakePHP, plugins and the application will be
  71. * loaded using conventions based names.
  72. *
  73. * @param \Cake\Console\CommandCollection $commands The CommandCollection to add commands into.
  74. * @return \Cake\Console\CommandCollection The updated collection.
  75. */
  76. public function console($commands)
  77. {
  78. return $commands->addMany($commands->autoDiscover());
  79. }
  80. /**
  81. * Invoke the application.
  82. *
  83. * - Convert the PSR response into CakePHP equivalents.
  84. * - Create the controller that will handle this request.
  85. * - Invoke the controller.
  86. *
  87. * @param \Psr\Http\Message\ServerRequestInterface $request The request
  88. * @param \Psr\Http\Message\ResponseInterface $response The response
  89. * @param callable $next The next middleware
  90. * @return \Psr\Http\Message\ResponseInterface
  91. */
  92. public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
  93. {
  94. return $this->getDispatcher()->dispatch($request, $response);
  95. }
  96. /**
  97. * Get the ActionDispatcher.
  98. *
  99. * @return \Cake\Http\ActionDispatcher
  100. */
  101. protected function getDispatcher()
  102. {
  103. return new ActionDispatcher(null, null, DispatcherFactory::filters());
  104. }
  105. }