4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\DependencyInjection
;
14 use Symfony\Component\Form\FormExtensionInterface
;
15 use Symfony\Component\Form\FormTypeGuesserChain
;
16 use Symfony\Component\Form\Exception\InvalidArgumentException
;
17 use Symfony\Component\DependencyInjection\ContainerInterface
;
19 class DependencyInjectionExtension
implements FormExtensionInterface
23 private $typeServiceIds;
25 private $guesserServiceIds;
29 private $guesserLoaded = false;
31 public function __construct(ContainerInterface
$container,
32 array $typeServiceIds, array $typeExtensionServiceIds,
33 array $guesserServiceIds)
35 $this->container
= $container;
36 $this->typeServiceIds
= $typeServiceIds;
37 $this->typeExtensionServiceIds
= $typeExtensionServiceIds;
38 $this->guesserServiceIds
= $guesserServiceIds;
41 public function getType($name)
43 if (!isset($this->typeServiceIds
[$name])) {
44 throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name));
47 $type = $this->container
->get($this->typeServiceIds
[$name]);
49 if ($type->getName() !== $name) {
50 throw new InvalidArgumentException(
51 sprintf('The type name specified for the service "%s" does not match the actual name. Expected "%s", given "%s"',
52 $this->typeServiceIds
[$name],
61 public function hasType($name)
63 return isset($this->typeServiceIds
[$name]);
66 public function getTypeExtensions($name)
68 $extensions = array();
70 if (isset($this->typeExtensionServiceIds
[$name])) {
71 foreach ($this->typeExtensionServiceIds
[$name] as $serviceId) {
72 $extensions[] = $this->container
->get($serviceId);
79 public function hasTypeExtensions($name)
81 return isset($this->typeExtensionServiceIds
[$name]);
84 public function getTypeGuesser()
86 if (!$this->guesserLoaded
) {
87 $this->guesserLoaded
= true;
90 foreach ($this->guesserServiceIds
as $serviceId) {
91 $guessers[] = $this->container
->get($serviceId);
94 if (count($guessers) > 0) {
95 $this->guesser
= new FormTypeGuesserChain($guessers);
99 return $this->guesser
;