Browse Source

[Templating] remove Engine as a dependency for the Helper objects

Fabien Potencier 15 years ago
parent
commit
bf08289e18

+ 1 - 1
src/Symfony/Components/Templating/Engine.php

@@ -182,7 +182,7 @@ class Engine
       $this->helpers[$alias] = $helper;
     }
 
-    $helper->setEngine($this);
+    $helper->setCharset($this->charset);
   }
 
   /**

+ 9 - 11
src/Symfony/Components/Templating/Helper/Helper.php

@@ -2,8 +2,6 @@
 
 namespace Symfony\Components\Templating\Helper;
 
-use Symfony\Components\Templating\Engine;
-
 /*
  * This file is part of the symfony package.
  *
@@ -22,25 +20,25 @@ use Symfony\Components\Templating\Engine;
  */
 abstract class Helper implements HelperInterface
 {
-  protected $engine;
+  protected $charset = 'UTF-8';
 
   /**
-   * Sets the engine associated with this helper.
+   * Sets the default charset.
    *
-   * @param Engine $engine A Engine instance
+   * @param string $charset The charset
    */
-  public function setEngine(Engine $engine = null)
+  public function setCharset($charset)
   {
-    $this->engine = $engine;
+    $this->charset = $charset;
   }
 
   /**
-   * Gets the engine associated with this helper.
+   * Gets the default charset.
    *
-   * @return Engine A Engine instance
+   * @return string The default charset
    */
-  public function getEngine()
+  public function getCharset()
   {
-    return $this->engine;
+    return $this->charset;
   }
 }

+ 6 - 8
src/Symfony/Components/Templating/Helper/HelperInterface.php

@@ -2,8 +2,6 @@
 
 namespace Symfony\Components\Templating\Helper;
 
-use Symfony\Components\Templating\Engine;
-
 /*
  * This file is part of the symfony package.
  *
@@ -30,16 +28,16 @@ interface HelperInterface
   function getName();
 
   /**
-   * Sets the engine associated with this helper.
+   * Sets the default charset.
    *
-   * @param Engine $engine A Engine instance
+   * @param string $charset The charset
    */
-  function setEngine(Engine $engine = null);
+  function setCharset($charset);
 
   /**
-   * Gets the engine associated with this helper.
+   * Gets the default charset.
    *
-   * @return Engine A Engine instance
+   * @return string The default charset
    */
-  function getEngine();
+  function getCharset();
 }

+ 13 - 2
src/Symfony/Components/Templating/Helper/JavascriptsHelper.php

@@ -28,6 +28,17 @@ namespace Symfony\Components\Templating\Helper;
 class JavascriptsHelper extends Helper
 {
   protected $javascripts = array();
+  protected $assetHelper;
+
+  /**
+   * Constructor.
+   *
+   * @param AssetsHelper $assetHelper A AssetsHelper instance
+   */
+  public function __construct(AssetsHelper $assetHelper)
+  {
+    $this->assetHelper = $assetHelper;
+  }
 
   /**
    * Adds a JavaScript file.
@@ -37,7 +48,7 @@ class JavascriptsHelper extends Helper
    */
   public function add($javascript, $attributes = array())
   {
-    $this->javascripts[$this->engine->get('assets')->getUrl($javascript)] = $attributes;
+    $this->javascripts[$this->assetHelper->getUrl($javascript)] = $attributes;
   }
 
   /**
@@ -63,7 +74,7 @@ class JavascriptsHelper extends Helper
       $atts = '';
       foreach ($attributes as $key => $value)
       {
-        $atts .= ' '.sprintf('%s="%s"', $key, $this->engine->escape($value));
+        $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset));
       }
 
       $html .= sprintf('<script type="text/javascript" src="%s"%s></script>', $path, $atts)."\n";

+ 13 - 2
src/Symfony/Components/Templating/Helper/StylesheetsHelper.php

@@ -28,6 +28,17 @@ namespace Symfony\Components\Templating\Helper;
 class StylesheetsHelper extends Helper
 {
   protected $stylesheets = array();
+  protected $assetHelper;
+
+  /**
+   * Constructor.
+   *
+   * @param AssetsHelper $assetHelper A AssetsHelper instance
+   */
+  public function __construct(AssetsHelper $assetHelper)
+  {
+    $this->assetHelper = $assetHelper;
+  }
 
   /**
    * Adds a stylesheets file.
@@ -37,7 +48,7 @@ class StylesheetsHelper extends Helper
    */
   public function add($stylesheet, $attributes = array())
   {
-    $this->stylesheets[$this->engine->get('assets')->getUrl($stylesheet)] = $attributes;
+    $this->stylesheets[$this->assetHelper->getUrl($stylesheet)] = $attributes;
   }
 
   /**
@@ -63,7 +74,7 @@ class StylesheetsHelper extends Helper
       $atts = '';
       foreach ($attributes as $key => $value)
       {
-        $atts .= ' '.sprintf('%s="%s"', $key, $this->engine->escape($value));
+        $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset));
       }
 
       $html .= sprintf('<link href="%s" rel="stylesheet" type="text/css"%s />', $path, $atts)."\n";

+ 1 - 1
src/Symfony/Framework/WebBundle/Templating/Engine.php

@@ -87,7 +87,7 @@ class Engine extends BaseEngine
     if (is_string($this->helpers[$name]))
     {
       $this->helpers[$name] = $this->container->getService('templating.helper.'.$name);
-      $this->helpers[$name]->setEngine($this);
+      $this->helpers[$name]->setCharset($this->charset);
     }
 
     return $this->helpers[$name];