PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/sonata-project/core-bundle/Form/Extension/DependencyInjectionExtension.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 198 lines | 104 code | 32 blank | 62 comment | 16 complexity | 19d5d29131e5a66fc95401bc01b88676 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\CoreBundle\Form\Extension;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Form\Exception\InvalidArgumentException;
  13. use Symfony\Component\Form\FormExtensionInterface;
  14. use Symfony\Component\Form\FormTypeGuesserChain;
  15. use Symfony\Component\Form\FormTypeGuesserInterface;
  16. /**
  17. * This proxy class help to keep BC code with < SF2.8 form behavior by restoring
  18. * the type as a code and not as a class.
  19. */
  20. class DependencyInjectionExtension implements FormExtensionInterface
  21. {
  22. /**
  23. * @var FormExtensionInterface
  24. */
  25. protected $extension;
  26. protected $mappingTypes;
  27. protected $extensionTypes;
  28. /**
  29. * @var ContainerInterface
  30. */
  31. private $container;
  32. /**
  33. * @var []string
  34. */
  35. private $typeServiceIds;
  36. /**
  37. * @var []string
  38. */
  39. private $typeExtensionServiceIds;
  40. /**
  41. * @var []string
  42. */
  43. private $guesserServiceIds;
  44. /**
  45. * @var FormTypeGuesserInterface
  46. */
  47. private $guesser;
  48. private $guesserLoaded = false;
  49. /**
  50. * DependencyInjectionExtension constructor.
  51. *
  52. * @param ContainerInterface $container
  53. * @param array $typeServiceIds
  54. * @param array $typeExtensionServiceIds
  55. * @param array $guesserServiceIds
  56. * @param array $mappingTypes
  57. * @param array $extensionTypes
  58. */
  59. public function __construct(ContainerInterface $container, array $typeServiceIds, array $typeExtensionServiceIds, array $guesserServiceIds, array $mappingTypes = array(), array $extensionTypes = array())
  60. {
  61. $this->container = $container;
  62. $this->typeServiceIds = $typeServiceIds;
  63. $this->typeExtensionServiceIds = $typeExtensionServiceIds;
  64. $this->guesserServiceIds = $guesserServiceIds;
  65. $this->mappingTypes = $mappingTypes;
  66. $this->mappingExtensionTypes = $extensionTypes;
  67. $this->reverseMappingTypes = array_flip($mappingTypes);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getType($name)
  73. {
  74. // resolve code to FQCN
  75. $name = self::findClass($this->mappingTypes, $name);
  76. if (!isset($this->typeServiceIds[$name])) {
  77. if (class_exists($name) && in_array('Symfony\Component\Form\FormTypeInterface', class_implements($name), true)) {
  78. return new $name();
  79. } else {
  80. throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name));
  81. }
  82. }
  83. $type = $this->container->get($this->typeServiceIds[$name]);
  84. if ($name !== get_class($type) && (method_exists($type, 'getName') && $type->getName() !== $name)) {
  85. throw new InvalidArgumentException(
  86. sprintf('The type name specified for the service "%s" does not match the actual name. Expected "%s", given "%s"',
  87. $this->typeServiceIds[$name],
  88. $name,
  89. get_class($type)
  90. ));
  91. }
  92. return $type;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function hasType($name)
  98. {
  99. return isset($this->mappingTypes[$name]) || isset($this->typeServiceIds[$name]);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getTypeExtensions($name)
  105. {
  106. // lookup inside the extension mapping
  107. $serviceIdx = array();
  108. if (isset($this->reverseMappingTypes[$name])) {
  109. $code = $this->reverseMappingTypes[$name];
  110. if (isset($this->mappingExtensionTypes[$code])) {
  111. $serviceIdx = array_merge($serviceIdx, $this->mappingExtensionTypes[$code]);
  112. }
  113. }
  114. $serviceIdx = array_unique(array_merge(isset($this->typeExtensionServiceIds[$name]) ? $this->typeExtensionServiceIds[$name] : array(), $serviceIdx));
  115. $extensions = array();
  116. foreach ($serviceIdx as $serviceId) {
  117. if ($this->container->has($serviceId)) {
  118. $extensions[] = $this->container->get($serviceId);
  119. }
  120. }
  121. return $extensions;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function hasTypeExtensions($name)
  127. {
  128. return isset($this->reverseMappingTypes[$name]) || isset($this->typeExtensionServiceIds[$name]);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getTypeGuesser()
  134. {
  135. if (!$this->guesserLoaded) {
  136. $this->guesserLoaded = true;
  137. $guessers = array();
  138. foreach ($this->guesserServiceIds as $serviceId) {
  139. if ($this->container->has($serviceId)) {
  140. $guessers[] = $this->container->get($serviceId);
  141. }
  142. }
  143. if ($guessers) {
  144. $this->guesser = new FormTypeGuesserChain($guessers);
  145. }
  146. }
  147. return $this->guesser;
  148. }
  149. /**
  150. * @param string $type
  151. *
  152. * @return string
  153. */
  154. protected static function findClass($mapping, $type)
  155. {
  156. if (strpos($type, '\\')) {
  157. return $type;
  158. }
  159. if (!isset($mapping[$type])) {
  160. return $type;
  161. }
  162. return $mapping[$type];
  163. }
  164. }