2011-04-22 16:41:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
2013-04-15 21:11:02 +01:00
|
|
|
use Symfony\Component\Form\Exception\InvalidArgumentException;
|
2011-04-22 16:41:21 +01:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
|
|
|
|
/**
|
2012-05-26 08:48:33 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-04-22 16:41:21 +01:00
|
|
|
*/
|
|
|
|
abstract class AbstractExtension implements FormExtensionInterface
|
|
|
|
{
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* The types provided by this extension.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @var FormTypeInterface[] An array of FormTypeInterface
|
2011-04-22 16:41:21 +01:00
|
|
|
*/
|
|
|
|
private $types;
|
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* The type extensions provided by this extension.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @var FormTypeExtensionInterface[] An array of FormTypeExtensionInterface
|
2011-04-22 16:41:21 +01:00
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
private $typeExtensions;
|
2011-04-22 16:41:21 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* The type guesser provided by this extension.
|
|
|
|
*
|
2011-04-22 16:41:21 +01:00
|
|
|
* @var FormTypeGuesserInterface
|
|
|
|
*/
|
|
|
|
private $typeGuesser;
|
|
|
|
|
|
|
|
/**
|
2014-12-21 17:00:50 +00:00
|
|
|
* Whether the type guesser has been loaded.
|
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var bool
|
2011-04-22 16:41:21 +01:00
|
|
|
*/
|
|
|
|
private $typeGuesserLoaded = false;
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-05-06 11:48:43 +01:00
|
|
|
public function getType($name)
|
|
|
|
{
|
|
|
|
if (null === $this->types) {
|
|
|
|
$this->initTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->types[$name])) {
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new InvalidArgumentException(sprintf('The type "%s" can not be loaded by this extension', $name));
|
2011-05-06 11:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->types[$name];
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-05-06 11:48:43 +01:00
|
|
|
public function hasType($name)
|
|
|
|
{
|
|
|
|
if (null === $this->types) {
|
|
|
|
$this->initTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($this->types[$name]);
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-05-06 11:48:43 +01:00
|
|
|
public function getTypeExtensions($name)
|
|
|
|
{
|
|
|
|
if (null === $this->typeExtensions) {
|
|
|
|
$this->initTypeExtensions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($this->typeExtensions[$name])
|
|
|
|
? $this->typeExtensions[$name]
|
|
|
|
: array();
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-05-06 11:48:43 +01:00
|
|
|
public function hasTypeExtensions($name)
|
|
|
|
{
|
|
|
|
if (null === $this->typeExtensions) {
|
|
|
|
$this->initTypeExtensions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-05-06 11:48:43 +01:00
|
|
|
public function getTypeGuesser()
|
|
|
|
{
|
|
|
|
if (!$this->typeGuesserLoaded) {
|
|
|
|
$this->initTypeGuesser();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->typeGuesser;
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Registers the types.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return FormTypeInterface[] An array of FormTypeInterface instances
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
protected function loadTypes()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Registers the type extensions.
|
|
|
|
*
|
2012-11-01 15:08:59 +00:00
|
|
|
* @return FormTypeExtensionInterface[] An array of FormTypeExtensionInterface instances
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
protected function loadTypeExtensions()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Registers the type guesser.
|
|
|
|
*
|
|
|
|
* @return FormTypeGuesserInterface|null A type guesser
|
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
protected function loadTypeGuesser()
|
2011-04-22 16:41:21 +01:00
|
|
|
{
|
2011-04-22 18:22:26 +01:00
|
|
|
}
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Initializes the types.
|
|
|
|
*
|
|
|
|
* @throws UnexpectedTypeException if any registered type is not an instance of FormTypeInterface
|
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
private function initTypes()
|
|
|
|
{
|
2011-05-10 17:23:58 +01:00
|
|
|
$this->types = array();
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
foreach ($this->loadTypes() as $type) {
|
2011-04-22 16:41:21 +01:00
|
|
|
if (!$type instanceof FormTypeInterface) {
|
|
|
|
throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
|
|
|
|
}
|
|
|
|
|
2015-10-01 08:27:49 +01:00
|
|
|
$this->types[get_class($type)] = $type;
|
2011-04-22 16:41:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Initializes the type extensions.
|
|
|
|
*
|
|
|
|
* @throws UnexpectedTypeException if any registered type extension is not
|
|
|
|
* an instance of FormTypeExtensionInterface
|
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
private function initTypeExtensions()
|
|
|
|
{
|
2011-05-10 17:23:58 +01:00
|
|
|
$this->typeExtensions = array();
|
2011-04-22 18:22:26 +01:00
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
foreach ($this->loadTypeExtensions() as $extension) {
|
2011-04-22 18:22:26 +01:00
|
|
|
if (!$extension instanceof FormTypeExtensionInterface) {
|
|
|
|
throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $extension->getExtendedType();
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
$this->typeExtensions[$type][] = $extension;
|
2011-04-22 18:22:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Initializes the type guesser.
|
|
|
|
*
|
|
|
|
* @throws UnexpectedTypeException if the type guesser is not an instance of FormTypeGuesserInterface
|
|
|
|
*/
|
2011-04-22 16:41:21 +01:00
|
|
|
private function initTypeGuesser()
|
|
|
|
{
|
|
|
|
$this->typeGuesserLoaded = true;
|
|
|
|
|
2011-06-15 10:25:50 +01:00
|
|
|
$this->typeGuesser = $this->loadTypeGuesser();
|
2011-06-16 07:06:24 +01:00
|
|
|
if (null !== $this->typeGuesser && !$this->typeGuesser instanceof FormTypeGuesserInterface) {
|
2011-06-15 10:25:50 +01:00
|
|
|
throw new UnexpectedTypeException($this->typeGuesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
|
2011-04-22 16:41:21 +01:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|