ContainerAwareHttpKernel.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Scope;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  17. use Symfony\Component\HttpKernel\HttpKernel;
  18. use Symfony\Component\HttpKernel\HttpKernelInterface;
  19. /**
  20. * Adds a managed request scope.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  24. *
  25. * @deprecated since version 2.7, to be removed in 3.0.
  26. */
  27. class ContainerAwareHttpKernel extends HttpKernel
  28. {
  29. protected $container;
  30. /**
  31. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  32. * @param ContainerInterface $container A ContainerInterface instance
  33. * @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
  34. * @param RequestStack $requestStack A stack for master/sub requests
  35. * @param bool $triggerDeprecation Whether or not to trigger the deprecation warning for the ContainerAwareHttpKernel
  36. */
  37. public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver, RequestStack $requestStack = null, $triggerDeprecation = true)
  38. {
  39. parent::__construct($dispatcher, $controllerResolver, $requestStack);
  40. if ($triggerDeprecation) {
  41. @trigger_error('The '.__CLASS__.' class is deprecated since Symfony 2.7 and will be removed in 3.0. Use the Symfony\Component\HttpKernel\HttpKernel class instead.', E_USER_DEPRECATED);
  42. }
  43. $this->container = $container;
  44. // the request scope might have been created before (see FrameworkBundle)
  45. if (!$container->hasScope('request')) {
  46. $container->addScope(new Scope('request'));
  47. }
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  53. {
  54. $this->container->enterScope('request');
  55. $this->container->set('request', $request, 'request');
  56. try {
  57. $response = parent::handle($request, $type, $catch);
  58. } catch (\Exception $e) {
  59. $this->container->set('request', null, 'request');
  60. $this->container->leaveScope('request');
  61. throw $e;
  62. } catch (\Throwable $e) {
  63. $this->container->set('request', null, 'request');
  64. $this->container->leaveScope('request');
  65. throw $e;
  66. }
  67. $this->container->set('request', null, 'request');
  68. $this->container->leaveScope('request');
  69. return $response;
  70. }
  71. }