Application.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 TestApp;
  17. use Cake\Console\CommandCollection;
  18. use Cake\Core\Configure;
  19. use Cake\Core\ContainerInterface;
  20. use Cake\Error\Middleware\ErrorHandlerMiddleware;
  21. use Cake\Http\BaseApplication;
  22. use Cake\Http\MiddlewareQueue;
  23. use Cake\Routing\Exception\DuplicateNamedRouteException;
  24. use Cake\Routing\Middleware\RoutingMiddleware;
  25. use Cake\Routing\RouteBuilder;
  26. use League\Container\ReflectionContainer;
  27. use stdClass;
  28. use TestApp\Command\AbortCommand;
  29. use TestApp\Command\DependencyCommand;
  30. use TestApp\Command\FormatSpecifierCommand;
  31. use TestApp\Middleware\DumbMiddleware;
  32. use TestApp\Middleware\SampleMiddleware;
  33. class Application extends BaseApplication
  34. {
  35. public function bootstrap(): void
  36. {
  37. parent::bootstrap();
  38. // Load plugins defined in Configure.
  39. if (Configure::check('Plugins.autoload')) {
  40. foreach (Configure::read('Plugins.autoload') as $value) {
  41. $this->addPlugin($value);
  42. }
  43. }
  44. // Check plugins added here
  45. }
  46. public function console(CommandCollection $commands): CommandCollection
  47. {
  48. return $commands
  49. ->add('abort_command', new AbortCommand())
  50. ->add('format_specifier_command', new FormatSpecifierCommand())
  51. ->addMany($commands->autoDiscover());
  52. }
  53. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  54. {
  55. $middlewareQueue->add(function ($request, $handler) {
  56. return $handler->handle($request)->withHeader('X-Middleware', 'true');
  57. });
  58. $middlewareQueue->add(new ErrorHandlerMiddleware(Configure::read('Error', [])));
  59. $middlewareQueue->add(new RoutingMiddleware($this));
  60. return $middlewareQueue;
  61. }
  62. /**
  63. * Routes hook, used for testing with RoutingMiddleware.
  64. */
  65. public function routes(RouteBuilder $routes): void
  66. {
  67. // Additional routes to load
  68. if (Configure::check('TestApp.routes')) {
  69. $func = Configure::read('TestApp.routes');
  70. $func($routes);
  71. }
  72. $routes->registerMiddleware('dumb', new DumbMiddleware());
  73. $routes->registerMiddleware('sample', new SampleMiddleware());
  74. $routes->scope('/app', function (RouteBuilder $routes): void {
  75. $routes->applyMiddleware('dumb', 'sample');
  76. $routes->connect('/articles', ['controller' => 'Articles']);
  77. $routes->connect('/articles/{action}/*', ['controller' => 'Articles']);
  78. try {
  79. $routes->connect('/tests/{action}/*', ['controller' => 'Tests'], ['_name' => 'testName']);
  80. } catch (DuplicateNamedRouteException) {
  81. // do nothing. This happens when one test does multiple requests.
  82. }
  83. $routes->redirect('/redirect', 'http://example.com/test.html');
  84. $routes->fallbacks();
  85. });
  86. $routes->connect('/posts', ['controller' => 'Posts', 'action' => 'index']);
  87. $routes->connect('/bake/{controller}/{action}', ['plugin' => 'Bake']);
  88. }
  89. /**
  90. * Container register hook
  91. *
  92. * @param \Cake\Core\ContainerInterface $container The container to update
  93. */
  94. public function services(ContainerInterface $container): void
  95. {
  96. $container->add(stdClass::class, json_decode('{"key":"value"}'));
  97. $container->add(DependencyCommand::class)
  98. ->addArgument(stdClass::class);
  99. $container->delegate(new ReflectionContainer());
  100. }
  101. }