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;
|
|
|
|
|
|
|
|
use Symfony\Component\Form\Exception\FormException;
|
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
|
|
|
*/
|
|
|
|
abstract class AbstractExtension implements FormExtensionInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $types;
|
|
|
|
|
|
|
|
/**
|
2011-04-22 18:22:26 +01:00
|
|
|
* @var array
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormTypeGuesserInterface
|
|
|
|
*/
|
|
|
|
private $typeGuesser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
|
|
|
private $typeGuesserLoaded = false;
|
|
|
|
|
2011-05-06 11:48:43 +01:00
|
|
|
public function getType($name)
|
|
|
|
{
|
|
|
|
if (null === $this->types) {
|
|
|
|
$this->initTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->types[$name])) {
|
|
|
|
throw new FormException(sprintf('The type "%s" can not be loaded by this extension', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->types[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasType($name)
|
|
|
|
{
|
|
|
|
if (null === $this->types) {
|
|
|
|
$this->initTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($this->types[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTypeExtensions($name)
|
|
|
|
{
|
|
|
|
if (null === $this->typeExtensions) {
|
|
|
|
$this->initTypeExtensions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($this->typeExtensions[$name])
|
|
|
|
? $this->typeExtensions[$name]
|
|
|
|
: array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasTypeExtensions($name)
|
|
|
|
{
|
|
|
|
if (null === $this->typeExtensions) {
|
|
|
|
$this->initTypeExtensions();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($this->typeExtensions[$name]) && count($this->typeExtensions[$name]) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTypeGuesser()
|
|
|
|
{
|
|
|
|
if (!$this->typeGuesserLoaded) {
|
|
|
|
$this->initTypeGuesser();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->typeGuesser;
|
|
|
|
}
|
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
protected function loadTypes()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
protected function loadTypeExtensions()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2011-04-22 16:41:21 +01:00
|
|
|
|
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
|
|
|
return null;
|
|
|
|
}
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
private function initTypes()
|
|
|
|
{
|
2011-04-22 16:41:21 +01:00
|
|
|
$types = $this->loadTypes();
|
|
|
|
$typesByName = array();
|
|
|
|
|
|
|
|
foreach ($types as $type) {
|
|
|
|
if (!$type instanceof FormTypeInterface) {
|
|
|
|
throw new UnexpectedTypeException($type, 'Symfony\Component\Form\FormTypeInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
$typesByName[$type->getName()] = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->types = $typesByName;
|
|
|
|
}
|
|
|
|
|
2011-04-22 18:22:26 +01:00
|
|
|
private function initTypeExtensions()
|
|
|
|
{
|
|
|
|
$extensions = $this->loadTypeExtensions();
|
|
|
|
$extensionsByType = array();
|
|
|
|
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
if (!$extension instanceof FormTypeExtensionInterface) {
|
|
|
|
throw new UnexpectedTypeException($extension, 'Symfony\Component\Form\FormTypeExtensionInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $extension->getExtendedType();
|
|
|
|
|
|
|
|
if (!isset($extensionsByType[$type])) {
|
|
|
|
$extensionsByType[$type] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$extensionsByType[$type][] = $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->typeExtensions = $extensionsByType;
|
|
|
|
}
|
|
|
|
|
2011-04-22 16:41:21 +01:00
|
|
|
private function initTypeGuesser()
|
|
|
|
{
|
|
|
|
$this->typeGuesserLoaded = true;
|
|
|
|
|
|
|
|
$guesser = $this->loadTypeGuesser();
|
|
|
|
|
|
|
|
if (!$guesser instanceof FormTypeGuesserInterface) {
|
2011-04-24 02:08:24 +01:00
|
|
|
throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\FormTypeGuesserInterface');
|
2011-04-22 16:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->guesser = $guesser;
|
|
|
|
}
|
|
|
|
}
|