TestContainer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Bundle\FrameworkBundle\Test;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Container;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. /**
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. *
  18. * @internal
  19. */
  20. class TestContainer extends Container
  21. {
  22. private $kernel;
  23. private $privateServicesLocatorId;
  24. public function __construct(KernelInterface $kernel, string $privateServicesLocatorId)
  25. {
  26. $this->kernel = $kernel;
  27. $this->privateServicesLocatorId = $privateServicesLocatorId;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function compile()
  33. {
  34. $this->getPublicContainer()->compile();
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function isCompiled(): bool
  40. {
  41. return $this->getPublicContainer()->isCompiled();
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getParameterBag(): ParameterBagInterface
  47. {
  48. return $this->getPublicContainer()->getParameterBag();
  49. }
  50. /**
  51. * {@inheritdoc}
  52. *
  53. * @return array|bool|float|int|string|null
  54. */
  55. public function getParameter(string $name)
  56. {
  57. return $this->getPublicContainer()->getParameter($name);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function hasParameter(string $name): bool
  63. {
  64. return $this->getPublicContainer()->hasParameter($name);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function setParameter(string $name, $value)
  70. {
  71. $this->getPublicContainer()->setParameter($name, $value);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function set(string $id, $service)
  77. {
  78. $this->getPublicContainer()->set($id, $service);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function has($id): bool
  84. {
  85. return $this->getPublicContainer()->has($id) || $this->getPrivateContainer()->has($id);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function get($id, int $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1): ?object
  91. {
  92. return $this->getPrivateContainer()->has($id) ? $this->getPrivateContainer()->get($id) : $this->getPublicContainer()->get($id, $invalidBehavior);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function initialized(string $id): bool
  98. {
  99. return $this->getPublicContainer()->initialized($id);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function reset()
  105. {
  106. // ignore the call
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getServiceIds(): array
  112. {
  113. return $this->getPublicContainer()->getServiceIds();
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getRemovedIds(): array
  119. {
  120. return $this->getPublicContainer()->getRemovedIds();
  121. }
  122. private function getPublicContainer(): Container
  123. {
  124. if (null === $container = $this->kernel->getContainer()) {
  125. throw new \LogicException('Cannot access the container on a non-booted kernel. Did you forget to boot it?');
  126. }
  127. return $container;
  128. }
  129. private function getPrivateContainer(): ContainerInterface
  130. {
  131. return $this->getPublicContainer()->get($this->privateServicesLocatorId);
  132. }
  133. }