From f83c1376a1796b7a08c704c97bf0186b8699c31f Mon Sep 17 00:00:00 2001 From: alexandresalome Date: Thu, 19 May 2011 14:32:24 +0200 Subject: [PATCH] [TwigBundle] Move the code filters to a dedicated extensions A dedicated extension now exists for the code-related filters for Twig. The dependency to service_container was also removed, to use CodeHelper, instead --- .../TwigBundle/Extension/CodeExtension.php | 97 +++++++++++++++++++ .../Extension/TemplatingExtension.php | 57 ----------- .../TwigBundle/Resources/config/twig.xml | 5 + 3 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Extension/CodeExtension.php diff --git a/src/Symfony/Bundle/TwigBundle/Extension/CodeExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/CodeExtension.php new file mode 100644 index 000000000000..93e0b3f70148 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Extension/CodeExtension.php @@ -0,0 +1,97 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\TwigBundle\Extension; + +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper; + +/** + * + * @author Fabien Potencier + * @author Alexandre Salomé + */ +class CodeExtension extends \Twig_Extension +{ + private $helper; + + /** + * Constructor of Twig Extension to provide functions for code formatting + * + * @param Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper $helper Helper to use + */ + public function __construct(CodeHelper $helper) + { + $this->helper = $helper; + } + + /** + * {@inheritdoc} + */ + public function getFilters() + { + return array( + 'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))), + 'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))), + 'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))), + 'format_args_as_text' => new \Twig_Filter_Method($this, 'formatArgsAsText'), + 'file_excerpt' => new \Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))), + 'format_file' => new \Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))), + 'format_file_from_text' => new \Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))), + 'file_link' => new \Twig_Filter_Method($this, 'getFileLink', array('is_safe' => array('html'))), + ); + } + + public function abbrClass($class) + { + return $this->helper->abbrClass($class); + } + + public function abbrMethod($method) + { + return $this->helper->abbrMethod($method); + } + + public function formatArgs($args) + { + return $this->helper->formatArgs($args); + } + + public function formatArgsAsText($args) + { + return $this->helper->formatArgsAsText($args); + } + + public function fileExcerpt($file, $line) + { + return $this->helper->fileExcerpt($file, $line); + } + + public function formatFile($file, $line, $text = null) + { + return $this->helper->formatFile($file, $line, $text); + } + + public function getFileLink($file, $line) + { + return $this->helper->getFileLink($file, $line); + } + + public function formatFileFromText($text) + { + return $this->helper->formatFileFromText($text); + } + + public function getName() + { + return 'code'; + } +} diff --git a/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php index 2a55a89be700..7550d337cea5 100644 --- a/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php +++ b/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php @@ -26,23 +26,6 @@ public function __construct(ContainerInterface $container) $this->container = $container; } - /** - * {@inheritdoc} - */ - public function getFilters() - { - return array( - 'abbr_class' => new \Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))), - 'abbr_method' => new \Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))), - 'format_args' => new \Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))), - 'format_args_as_text' => new \Twig_Filter_Method($this, 'formatArgsAsText'), - 'file_excerpt' => new \Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))), - 'format_file' => new \Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))), - 'format_file_from_text' => new \Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))), - 'file_link' => new \Twig_Filter_Method($this, 'getFileLink', array('is_safe' => array('html'))), - ); - } - /** * Returns a list of functions to add to the existing list. * @@ -82,46 +65,6 @@ public function getAssetsVersion($packageName = null) return $this->container->get('templating.helper.assets')->getVersion($packageName); } - public function abbrClass($class) - { - return $this->container->get('templating.helper.code')->abbrClass($class); - } - - public function abbrMethod($method) - { - return $this->container->get('templating.helper.code')->abbrMethod($method); - } - - public function formatArgs($args) - { - return $this->container->get('templating.helper.code')->formatArgs($args); - } - - public function formatArgsAsText($args) - { - return $this->container->get('templating.helper.code')->formatArgsAsText($args); - } - - public function fileExcerpt($file, $line) - { - return $this->container->get('templating.helper.code')->fileExcerpt($file, $line); - } - - public function formatFile($file, $line, $text = null) - { - return $this->container->get('templating.helper.code')->formatFile($file, $line, $text); - } - - public function getFileLink($file, $line) - { - return $this->container->get('templating.helper.code')->getFileLink($file, $line); - } - - public function formatFileFromText($text) - { - return $this->container->get('templating.helper.code')->formatFileFromText($text); - } - /** * Returns the name of the extension. * diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index b159b673a634..aba5fb991497 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -48,6 +48,11 @@ + + + + +