2012-07-12 18:17:14 +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\UnexpectedTypeException ;
use Symfony\Component\Form\Exception\FormException ;
/**
* The central registry of the Form component .
*
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
class FormRegistry implements FormRegistryInterface
{
/**
* Extensions
2012-11-01 15:08:59 +00:00
*
* @ var FormExtensionInterface [] An array of FormExtensionInterface
2012-07-12 18:17:14 +01:00
*/
private $extensions = array ();
/**
* @ var array
*/
private $types = array ();
/**
2012-11-08 18:49:46 +00:00
* @ var FormTypeGuesserInterface | false | null
2012-07-12 18:17:14 +01:00
*/
2012-11-08 18:49:46 +00:00
private $guesser = false ;
2012-07-12 18:17:14 +01:00
2012-07-29 18:10:47 +01:00
/**
* @ var ResolvedFormTypeFactoryInterface
*/
private $resolvedTypeFactory ;
2012-07-12 18:17:14 +01:00
/**
* Constructor .
*
2012-11-01 15:08:59 +00:00
* @ param FormExtensionInterface [] $extensions An array of FormExtensionInterface
2012-07-29 18:10:47 +01:00
* @ param ResolvedFormTypeFactoryInterface $resolvedTypeFactory The factory for resolved form types .
2012-07-12 18:17:14 +01:00
*
* @ throws UnexpectedTypeException if any extension does not implement FormExtensionInterface
*/
2012-07-29 18:10:47 +01:00
public function __construct ( array $extensions , ResolvedFormTypeFactoryInterface $resolvedTypeFactory )
2012-07-12 18:17:14 +01:00
{
foreach ( $extensions as $extension ) {
if ( ! $extension instanceof FormExtensionInterface ) {
throw new UnexpectedTypeException ( $extension , 'Symfony\Component\Form\FormExtensionInterface' );
}
}
$this -> extensions = $extensions ;
2012-07-29 18:10:47 +01:00
$this -> resolvedTypeFactory = $resolvedTypeFactory ;
2012-07-12 18:17:14 +01:00
}
/**
* { @ inheritdoc }
*/
public function addType ( ResolvedFormTypeInterface $type )
{
2012-12-12 16:43:13 +00:00
trigger_error ( 'addType() is deprecated since version 2.1 and will be removed in 2.3. Use form extensions or type registration in the Dependency Injection Container instead.' , E_USER_DEPRECATED );
2012-07-12 18:17:14 +01:00
$this -> types [ $type -> getName ()] = $type ;
}
/**
* { @ inheritdoc }
*/
public function getType ( $name )
{
if ( ! is_string ( $name )) {
throw new UnexpectedTypeException ( $name , 'string' );
}
if ( ! isset ( $this -> types [ $name ])) {
2012-07-29 18:10:47 +01:00
/** @var FormTypeInterface $type */
2012-07-12 18:17:14 +01:00
$type = null ;
foreach ( $this -> extensions as $extension ) {
/* @var FormExtensionInterface $extension */
if ( $extension -> hasType ( $name )) {
$type = $extension -> getType ( $name );
break ;
}
}
if ( ! $type ) {
throw new FormException ( sprintf ( 'Could not load type "%s"' , $name ));
}
2012-08-22 15:00:43 +01:00
$this -> resolveAndAddType ( $type );
}
2012-07-29 18:10:47 +01:00
2012-08-22 15:00:43 +01:00
return $this -> types [ $name ];
}
/**
* Wraps a type into a ResolvedFormTypeInterface implementation and connects
* it with its parent type .
*
* @ param FormTypeInterface $type The type to resolve .
*
* @ return ResolvedFormTypeInterface The resolved type .
*/
private function resolveAndAddType ( FormTypeInterface $type )
{
$parentType = $type -> getParent ();
2012-07-29 18:10:47 +01:00
2012-08-22 15:00:43 +01:00
if ( $parentType instanceof FormTypeInterface ) {
$this -> resolveAndAddType ( $parentType );
$parentType = $parentType -> getName ();
}
$typeExtensions = array ();
foreach ( $this -> extensions as $extension ) {
/* @var FormExtensionInterface $extension */
$typeExtensions = array_merge (
2012-07-29 18:10:47 +01:00
$typeExtensions ,
2012-08-22 15:00:43 +01:00
$extension -> getTypeExtensions ( $type -> getName ())
);
2012-07-12 18:17:14 +01:00
}
2012-12-14 06:33:36 +00:00
set_error_handler ( array ( 'Symfony\Component\Form\Tests\DeprecationErrorHandler' , 'handleBC' ));
2012-08-22 15:00:43 +01:00
$this -> addType ( $this -> resolvedTypeFactory -> createResolvedType (
$type ,
$typeExtensions ,
$parentType ? $this -> getType ( $parentType ) : null
));
2012-12-14 06:33:36 +00:00
restore_error_handler ();
2012-07-12 18:17:14 +01:00
}
/**
* { @ inheritdoc }
*/
public function hasType ( $name )
{
if ( isset ( $this -> types [ $name ])) {
return true ;
}
try {
$this -> getType ( $name );
} catch ( FormException $e ) {
return false ;
}
return true ;
}
/**
* { @ inheritdoc }
*/
public function getTypeGuesser ()
{
2012-11-08 18:49:46 +00:00
if ( false === $this -> guesser ) {
2012-07-12 18:17:14 +01:00
$guessers = array ();
foreach ( $this -> extensions as $extension ) {
/* @var FormExtensionInterface $extension */
$guesser = $extension -> getTypeGuesser ();
if ( $guesser ) {
$guessers [] = $guesser ;
}
}
2012-11-08 18:49:46 +00:00
$this -> guesser = ! empty ( $guessers ) ? new FormTypeGuesserChain ( $guessers ) : null ;
2012-07-12 18:17:14 +01:00
}
return $this -> guesser ;
}
/**
* { @ inheritdoc }
*/
public function getExtensions ()
{
return $this -> extensions ;
}
}