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 .
*
2019-03-12 20:16:42 +00:00
* @ var FormTypeGuesserInterface | null
2011-04-22 16:41:21 +01:00
*/
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 ]
2019-01-16 09:39:14 +00:00
: [];
2011-05-06 11:48:43 +01:00
}
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 ();
}
2018-07-05 12:24:53 +01:00
return isset ( $this -> typeExtensions [ $name ]) && \count ( $this -> typeExtensions [ $name ]) > 0 ;
2011-05-06 11:48:43 +01:00
}
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 ()
{
2019-01-16 09:39:14 +00:00
return [];
2011-04-22 18:22:26 +01:00
}
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 ()
{
2019-01-16 09:39:14 +00:00
return [];
2011-04-22 18:22:26 +01:00
}
2011-04-22 16:41:21 +01:00
2011-05-10 17:23:58 +01:00
/**
* Registers the type guesser .
*
2019-03-12 20:16:42 +00:00
* @ return FormTypeGuesserInterface | null
2011-05-10 17:23:58 +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
}
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 ()
{
2019-01-16 09:39:14 +00:00
$this -> types = [];
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' );
}
2018-07-26 09:45:46 +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 ()
{
2019-01-16 09:39:14 +00:00
$this -> typeExtensions = [];
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' );
}
2017-10-12 09:26:52 +01:00
if ( method_exists ( $extension , 'getExtendedTypes' )) {
2019-01-16 20:35:37 +00:00
$extendedTypes = [];
2011-04-22 18:22:26 +01:00
2017-10-12 09:26:52 +01:00
foreach ( $extension :: getExtendedTypes () as $extendedType ) {
$extendedTypes [] = $extendedType ;
}
} else {
@ trigger_error ( sprintf ( 'Not implementing the static getExtendedTypes() method in %s when implementing the %s is deprecated since Symfony 4.2. The method will be added to the interface in 5.0.' , \get_class ( $extension ), FormTypeExtensionInterface :: class ), E_USER_DEPRECATED );
2019-01-16 20:35:37 +00:00
$extendedTypes = [ $extension -> getExtendedType ()];
2017-10-12 09:26:52 +01:00
}
foreach ( $extendedTypes as $extendedType ) {
$this -> typeExtensions [ $extendedType ][] = $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
}