TemplatingExtension.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Extension\Templating;
  11. use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
  12. use Symfony\Component\Form\AbstractExtension;
  13. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
  15. use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
  16. use Symfony\Component\Form\FormRenderer;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Templating\PhpEngine;
  19. /**
  20. * Integrates the Templating component with the Form library.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. */
  24. class TemplatingExtension extends AbstractExtension
  25. {
  26. public function __construct(PhpEngine $engine, $csrfTokenManager = null, array $defaultThemes = array())
  27. {
  28. if ($csrfTokenManager instanceof CsrfProviderInterface) {
  29. $csrfTokenManager = new CsrfProviderAdapter($csrfTokenManager);
  30. } elseif (null !== $csrfTokenManager && !$csrfTokenManager instanceof CsrfTokenManagerInterface) {
  31. throw new UnexpectedTypeException($csrfTokenManager, 'CsrfProviderInterface or CsrfTokenManagerInterface');
  32. }
  33. $engine->addHelpers(array(
  34. new FormHelper(new FormRenderer(new TemplatingRendererEngine($engine, $defaultThemes), $csrfTokenManager)),
  35. ));
  36. }
  37. }