Browse Source

Merge remote branch 'alexandresalome/twig-bundle-extensions-cleanup'

* alexandresalome/twig-bundle-extensions-cleanup:
  [TwigBundle] Remove authoring
  [TwigBundle] Remove use ContainerInterface - not used
  [TwigBundle] Move all class names to parameters of DIC
  [TwigBundle] Rename TemplatingExtension to AssetsExtension
  [TwigBundle] Move the code filters to a dedicated extensions
  [TwigBundle] Change the dependency of ActionsExtension to the actions helper
  [TwigBundle] Move the {% render ... %} node to a dedicated extension + Remove service container
Fabien Potencier 14 years ago
parent
commit
5e1710f136

+ 70 - 0
src/Symfony/Bundle/TwigBundle/Extension/ActionsExtension.php

@@ -0,0 +1,70 @@
+<?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\Bundle\TwigBundle\Extension;
+
+use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
+use Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper;
+
+/**
+ * Twig extension for Symfony actions helper
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class ActionsExtension extends \Twig_Extension
+{
+    /**
+     * @var Symfony\Bundle\FrameworkBundle\Templating\Helper\ActionsHelper
+     */
+    private $helper;
+
+    /**
+     * Construct the extension with a HTTP Kernel
+     *
+     * @param HttpKernel $kernel The kernel to use for sub-requests
+     */
+    public function __construct(ActionsHelper $helper)
+    {
+        $this->helper = $helper;
+    }
+
+    /**
+     * Returns the Response content for a given controller or URI.
+     *
+     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
+     * @param array  $attributes An array of request attributes
+     * @param array  $options    An array of options
+     *
+     * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
+     */
+    public function renderAction($controller, array $attributes = array(), array $options = array())
+    {
+        return $this->helper->render($controller, $attributes, $options);
+    }
+
+    /**
+     * Returns the token parser instance to add to the existing list.
+     *
+     * @return array An array of Twig_TokenParser instances
+     */
+    public function getTokenParsers()
+    {
+        return array(
+            // {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
+            new RenderTokenParser(),
+        );
+    }
+
+    public function getName()
+    {
+        return 'actions';
+    }
+}

+ 79 - 0
src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php

@@ -0,0 +1,79 @@
+<?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\Bundle\TwigBundle\Extension;
+
+use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
+
+/**
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class AssetsExtension extends \Twig_Extension
+{
+    /**
+     * @var Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper
+     */
+    private $helper;
+
+    public function __construct(AssetsHelper $helper)
+    {
+        $this->helper = $helper;
+    }
+
+    /**
+     * Returns a list of functions to add to the existing list.
+     *
+     * @return array An array of functions
+     */
+    public function getFunctions()
+    {
+        return array(
+            'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
+            'assets_version' => new \Twig_Function_Method($this, 'getAssetsVersion'),
+        );
+    }
+
+    /**
+     * Returns the public path of an asset
+     *
+     * Absolute paths (i.e. http://...) are returned unmodified.
+     *
+     * @param string $path        A public path
+     * @param string $packageName The name of the asset package to use
+     *
+     * @return string A public path which takes into account the base path and URL path
+     */
+    public function getAssetUrl($path, $packageName = null)
+    {
+        return $this->helper->getUrl($path, $packageName);
+    }
+
+    /**
+     * Returns the version of the assets in a package
+     *
+     * @param string $packageName
+     * @return int
+     */
+    public function getAssetsVersion($packageName = null)
+    {
+        return $this->helper->getVersion($packageName);
+    }
+
+    /**
+     * Returns the name of the extension.
+     *
+     * @return string The extension name
+     */
+    public function getName()
+    {
+        return 'assets';
+    }
+}

+ 95 - 0
src/Symfony/Bundle/TwigBundle/Extension/CodeExtension.php

@@ -0,0 +1,95 @@
+<?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\Bundle\TwigBundle\Extension;
+
+use Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper;
+
+/**
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+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';
+    }
+}

+ 0 - 164
src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php

@@ -1,164 +0,0 @@
-<?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\Bundle\TwigBundle\Extension;
-
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
-
-/**
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
-class TemplatingExtension extends \Twig_Extension
-{
-    protected $container;
-
-    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.
-     *
-     * @return array An array of functions
-     */
-    public function getFunctions()
-    {
-        return array(
-            'asset' => new \Twig_Function_Method($this, 'getAssetUrl'),
-            'assets_version' => new \Twig_Function_Method($this, 'getAssetsVersion'),
-        );
-    }
-
-    /**
-     * Returns the public path of an asset
-     *
-     * Absolute paths (i.e. http://...) are returned unmodified.
-     *
-     * @param string $path        A public path
-     * @param string $packageName The name of the asset package to use
-     *
-     * @return string A public path which takes into account the base path and URL path
-     */
-    public function getAssetUrl($path, $packageName = null)
-    {
-        return $this->container->get('templating.helper.assets')->getUrl($path, $packageName);
-    }
-
-    /**
-     * Returns the version of the assets in a package
-     *
-     * @param string $packageName
-     * @return int
-     */
-    public function getAssetsVersion($packageName = null)
-    {
-        return $this->container->get('templating.helper.assets')->getVersion($packageName);
-    }
-
-    /**
-     * Returns the Response content for a given controller or URI.
-     *
-     * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
-     * @param array  $attributes An array of request attributes
-     * @param array  $options    An array of options
-     *
-     * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
-     */
-    public function renderAction($controller, array $attributes = array(), array $options = array())
-    {
-        $options['attributes'] = $attributes;
-
-        return $this->container->get('http_kernel')->render($controller, $options);
-    }
-
-    /**
-     * Returns the token parser instance to add to the existing list.
-     *
-     * @return array An array of Twig_TokenParser instances
-     */
-    public function getTokenParsers()
-    {
-        return array(
-            // {% render 'BlogBundle:Post:list' with { 'limit': 2 }, { 'alt': 'BlogBundle:Post:error' } %}
-            new RenderTokenParser(),
-        );
-    }
-
-    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.
-     *
-     * @return string The extension name
-     */
-    public function getName()
-    {
-        return 'templating';
-    }
-}

+ 1 - 1
src/Symfony/Bundle/TwigBundle/Node/RenderNode.php

@@ -32,7 +32,7 @@ class RenderNode extends \Twig_Node
     {
         $compiler
             ->addDebugInfo($this)
-            ->write("echo \$this->env->getExtension('templating')->renderAction(")
+            ->write("echo \$this->env->getExtension('actions')->renderAction(")
             ->subcompile($this->getNode('expr'))
             ->raw(', ')
             ->subcompile($this->getNode('attributes'))

+ 27 - 8
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -9,6 +9,15 @@
         <parameter key="twig.loader.class">Symfony\Bundle\TwigBundle\Loader\FilesystemLoader</parameter>
         <parameter key="templating.engine.twig.class">Symfony\Bundle\TwigBundle\TwigEngine</parameter>
         <parameter key="twig.cache_warmer.class">Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheCacheWarmer</parameter>
+        <parameter key="twig.extension.trans.class">Symfony\Bridge\Twig\Extension\TranslationExtension</parameter>
+        <parameter key="twig.extension.assets.class">Symfony\Bundle\TwigBundle\Extension\AssetsExtension</parameter>
+        <parameter key="twig.extension.actions.class">Symfony\Bundle\TwigBundle\Extension\ActionsExtension</parameter>
+        <parameter key="twig.extension.code.class">Symfony\Bundle\TwigBundle\Extension\CodeExtension</parameter>
+        <parameter key="twig.extension.routing.class">Symfony\Bridge\Twig\Extension\RoutingExtension</parameter>
+        <parameter key="twig.extension.yaml.class">Symfony\Bridge\Twig\Extension\YamlExtension</parameter>
+        <parameter key="twig.extension.form.class">Symfony\Bridge\Twig\Extension\FormExtension</parameter>
+        <parameter key="twig.extension.text.class">Twig_Extensions_Extension_Text</parameter>
+        <parameter key="twig.extension.debug.class">Twig_Extensions_Extension_Debug</parameter>
     </parameters>
 
     <services>
@@ -33,32 +42,42 @@
             <argument type="service" id="templating.globals" />
         </service>
 
-        <service id="twig.extension.trans" class="Symfony\Bridge\Twig\Extension\TranslationExtension" public="false">
+        <service id="twig.extension.trans" class="%twig.extension.trans.class%" public="false">
             <tag name="twig.extension" />
             <argument type="service" id="translator" />
         </service>
 
-        <service id="twig.extension.helpers" class="Symfony\Bundle\TwigBundle\Extension\TemplatingExtension" public="false">
+        <service id="twig.extension.assets" class="%twig.extension.assets.class%" public="false">
             <tag name="twig.extension" />
-            <argument type="service" id="service_container" />
+            <argument type="service" id="templating.helper.assets" />
+        </service>
+
+        <service id="twig.extension.actions" class="%twig.extension.actions.class%" public="false">
+            <tag name="twig.extension" />
+            <argument type="service" id="templating.helper.actions" />
+        </service>
+
+        <service id="twig.extension.code" class="%twig.extension.code.class%" public="false">
+            <tag name="twig.extension" />
+            <argument type="service" id="templating.helper.code" />
         </service>
 
-        <service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension" public="false">
+        <service id="twig.extension.routing" class="%twig.extension.routing.class%" public="false">
             <tag name="twig.extension" />
             <argument type="service" id="router" />
         </service>
 
-        <service id="twig.extension.yaml" class="Symfony\Bridge\Twig\Extension\YamlExtension" public="false">
+        <service id="twig.extension.yaml" class="%twig.extension.yaml.class%" public="false">
             <tag name="twig.extension" />
         </service>
 
-        <service id="twig.extension.form" class="Symfony\Bridge\Twig\Extension\FormExtension" public="false">
+        <service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
             <tag name="twig.extension" />
             <argument>%twig.form.resources%</argument>
         </service>
 
-        <service id="twig.extension.text" class="Twig_Extensions_Extension_Text" public="false" />
+        <service id="twig.extension.text" class="%twig.extension.text.class%" public="false" />
 
-        <service id="twig.extension.debug" class="Twig_Extensions_Extension_Debug" public="false" />
+        <service id="twig.extension.debug" class="%twig.extension.debug.class%" public="false" />
     </services>
 </container>