ContainerAwareHttpKernel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpKernel\HttpKernel;
  14. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. use Symfony\Component\DependencyInjection\Scope;
  18. /**
  19. * Adds a managed request scope.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. */
  24. class ContainerAwareHttpKernel extends HttpKernel
  25. {
  26. protected $container;
  27. /**
  28. * Constructor.
  29. *
  30. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  31. * @param ContainerInterface $container A ContainerInterface instance
  32. * @param ControllerResolverInterface $controllerResolver A ControllerResolverInterface instance
  33. */
  34. public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
  35. {
  36. parent::__construct($dispatcher, $controllerResolver);
  37. $this->container = $container;
  38. // the request scope might have been created before (see FrameworkBundle)
  39. if (!$container->hasScope('request')) {
  40. $container->addScope(new Scope('request'));
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  47. {
  48. $request->headers->set('X-Php-Ob-Level', ob_get_level());
  49. $this->container->enterScope('request');
  50. $this->container->set('request', $request, 'request');
  51. try {
  52. $response = parent::handle($request, $type, $catch);
  53. } catch (\Exception $e) {
  54. $this->container->set('request', null, 'request');
  55. $this->container->leaveScope('request');
  56. throw $e;
  57. }
  58. $this->container->set('request', null, 'request');
  59. $this->container->leaveScope('request');
  60. return $response;
  61. }
  62. }