Application.php 3.7 KB

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