From 598d458a3c5e45f67a2f6686433272abde29b1e3 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 5 Jan 2011 14:11:43 +0100 Subject: [PATCH] added CompatAssetsBundle to reintroduce the deprecated css/js helpers/tags Just add this line in your configuration to enable it: This bundle is just to ease the upgrade path. Please don't use it if you don't need to and upgrade your templates as this bundle will be removed before RC1. --- .../CompatAssetsBundle/CompatAssetsBundle.php | 21 ++++ .../Resources/config/assets.xml | 28 +++++ .../Templating/Helper/JavascriptsHelper.php | 113 ++++++++++++++++++ .../Templating/Helper/StylesheetsHelper.php | 113 ++++++++++++++++++ .../Twig/Extension/AssetsExtension.php | 74 ++++++++++++ .../Twig/Node/JavascriptNode.php | 42 +++++++ .../Twig/Node/JavascriptsNode.php | 38 ++++++ .../Twig/Node/StylesheetNode.php | 42 +++++++ .../Twig/Node/StylesheetsNode.php | 38 ++++++ .../TokenParser/JavascriptTokenParser.php | 57 +++++++++ .../TokenParser/JavascriptsTokenParser.php | 46 +++++++ .../TokenParser/StylesheetTokenParser.php | 57 +++++++++ .../TokenParser/StylesheetsTokenParser.php | 46 +++++++ 13 files changed, 715 insertions(+) create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/CompatAssetsBundle.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Resources/config/assets.xml create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/JavascriptsHelper.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/StylesheetsHelper.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/Extension/AssetsExtension.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptNode.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptsNode.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetNode.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetsNode.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptTokenParser.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptsTokenParser.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetTokenParser.php create mode 100644 src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetsTokenParser.php diff --git a/src/Symfony/Bundle/CompatAssetsBundle/CompatAssetsBundle.php b/src/Symfony/Bundle/CompatAssetsBundle/CompatAssetsBundle.php new file mode 100644 index 0000000000..d191604ef6 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/CompatAssetsBundle.php @@ -0,0 +1,21 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * CompatAssetsBundle. + * + * @author Fabien Potencier + */ +class CompatAssetsBundle extends Bundle +{ +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Resources/config/assets.xml b/src/Symfony/Bundle/CompatAssetsBundle/Resources/config/assets.xml new file mode 100644 index 0000000000..11746d0b11 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Resources/config/assets.xml @@ -0,0 +1,28 @@ + + + + + + Symfony\Bundle\CompatAssetsBundle\Templating\Helper\JavascriptsHelper + Symfony\Bundle\CompatAssetsBundle\Templating\Helper\StylesheetsHelper + + + + + + + + + + + + + + + + + + + diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/JavascriptsHelper.php b/src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/JavascriptsHelper.php new file mode 100644 index 0000000000..afb24a5609 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/JavascriptsHelper.php @@ -0,0 +1,113 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * JavascriptsHelper is a helper that manages JavaScripts. + * + * Usage: + * + * + * $view['javascripts']->add('foo.js'); + * echo $view['javascripts']; + * + * + * @author Fabien Potencier + */ +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. + * + * @param string $javascript A JavaScript file path + * @param array $attributes An array of attributes + */ + public function add($javascript, $attributes = array()) + { + $this->javascripts[$this->assetHelper->getUrl($javascript)] = $attributes; + } + + /** + * Returns all JavaScript files. + * + * @return array An array of JavaScript files to include + */ + public function get() + { + return $this->javascripts; + } + + /** + * Returns HTML representation of the links to JavaScripts. + * + * @return string The HTML representation of the JavaScripts + */ + public function render() + { + $html = ''; + foreach ($this->javascripts as $path => $attributes) { + $atts = ''; + foreach ($attributes as $key => $value) { + $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset)); + } + + $html .= sprintf('', $path, $atts)."\n"; + } + + return $html; + } + + /** + * Outputs HTML representation of the links to JavaScripts. + * + */ + public function output() + { + echo $this->render(); + } + + /** + * Returns a string representation of this helper as HTML. + * + * @return string The HTML representation of the JavaScripts + */ + public function __toString() + { + return $this->render(); + } + + /** + * Returns the canonical name of this helper. + * + * @return string The canonical name + */ + public function getName() + { + return 'javascripts'; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/StylesheetsHelper.php b/src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/StylesheetsHelper.php new file mode 100644 index 0000000000..db065f208d --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Templating/Helper/StylesheetsHelper.php @@ -0,0 +1,113 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * StylesheetsHelper is a helper that manages stylesheets. + * + * Usage: + * + * + * $view['stylesheets']->add('foo.css', array('media' => 'print')); + * echo $view['stylesheets']; + * + * + * @author Fabien Potencier + */ +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. + * + * @param string $stylesheet A stylesheet file path + * @param array $attributes An array of attributes + */ + public function add($stylesheet, $attributes = array()) + { + $this->stylesheets[$this->assetHelper->getUrl($stylesheet)] = $attributes; + } + + /** + * Returns all stylesheet files. + * + * @return array An array of stylesheet files to include + */ + public function get() + { + return $this->stylesheets; + } + + /** + * Returns HTML representation of the links to stylesheets. + * + * @return string The HTML representation of the stylesheets + */ + public function render() + { + $html = ''; + foreach ($this->stylesheets as $path => $attributes) { + $atts = ''; + foreach ($attributes as $key => $value) { + $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset)); + } + + $html .= sprintf('', $path, $atts)."\n"; + } + + return $html; + } + + /** + * Outputs HTML representation of the links to stylesheets. + * + */ + public function output() + { + echo $this->render(); + } + + /** + * Returns a string representation of this helper as HTML. + * + * @return string The HTML representation of the stylesheets + */ + public function __toString() + { + return $this->render(); + } + + /** + * Returns the canonical name of this helper. + * + * @return string The canonical name + */ + public function getName() + { + return 'stylesheets'; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/Extension/AssetsExtension.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Extension/AssetsExtension.php new file mode 100644 index 0000000000..883576e8b4 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Extension/AssetsExtension.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * @author Fabien Potencier + */ +class AssetsExtension extends \Twig_Extension +{ + protected $container; + + public function __construct(ContainerInterface $container) + { + $this->container = $container; + } + + public function getContainer() + { + return $this->container; + } + + public function getTemplating() + { + return $this->container->get('templating.engine'); + } + + /** + * Returns the token parser instance to add to the existing list. + * + * @return array An array of Twig_TokenParser instances + */ + public function getTokenParsers() + { + return array( + // {% javascript 'bundles/blog/js/blog.js' %} + new JavascriptTokenParser(), + + // {% javascripts %} + new JavascriptsTokenParser(), + + // {% stylesheet 'bundles/blog/css/blog.css' with { 'media': 'screen' } %} + new StylesheetTokenParser(), + + // {% stylesheets %} + new StylesheetsTokenParser(), + ); + } + + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'assets'; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptNode.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptNode.php new file mode 100644 index 0000000000..a87160baff --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptNode.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Represents a javascript node. + * + * @author Fabien Potencier + */ +class JavascriptNode extends \Twig_Node +{ + public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $attributes, $lineno, $tag = null) + { + parent::__construct(array('expr' => $expr, 'attributes' => $attributes), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param \Twig_Compiler A Twig_Compiler instance + */ + public function compile(\Twig_Compiler $compiler) + { + $compiler + ->addDebugInfo($this) + ->write("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.javascripts')->add(") + ->subcompile($this->getNode('expr')) + ->raw(', ') + ->subcompile($this->getNode('attributes')) + ->raw(");\n") + ; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptsNode.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptsNode.php new file mode 100644 index 0000000000..2cec584cca --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/JavascriptsNode.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Represents a javascripts node. + * + * @author Fabien Potencier + */ +class JavascriptsNode extends \Twig_Node +{ + public function __construct($lineno, $tag = null) + { + parent::__construct(array(), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param \Twig_Compiler A Twig_Compiler instance + */ + public function compile(\Twig_Compiler $compiler) + { + $compiler + ->addDebugInfo($this) + ->write("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.javascripts')->render();\n") + ; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetNode.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetNode.php new file mode 100644 index 0000000000..13054dd72d --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetNode.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Represents a stylesheet node. + * + * @author Fabien Potencier + */ +class StylesheetNode extends \Twig_Node +{ + public function __construct(\Twig_Node_Expression $expr, \Twig_Node_Expression $attributes, $lineno, $tag = null) + { + parent::__construct(array('expr' => $expr, 'attributes' => $attributes), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param \Twig_Compiler A Twig_Compiler instance + */ + public function compile(\Twig_Compiler $compiler) + { + $compiler + ->addDebugInfo($this) + ->write("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.stylesheets')->add(") + ->subcompile($this->getNode('expr')) + ->raw(', ') + ->subcompile($this->getNode('attributes')) + ->raw(");\n") + ; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetsNode.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetsNode.php new file mode 100644 index 0000000000..9843d9d4f5 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/Node/StylesheetsNode.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Represents a stylesheets node. + * + * @author Fabien Potencier + */ +class StylesheetsNode extends \Twig_Node +{ + public function __construct($lineno, $tag = null) + { + parent::__construct(array(), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param \Twig_Compiler A Twig_Compiler instance + */ + public function compile(\Twig_Compiler $compiler) + { + $compiler + ->addDebugInfo($this) + ->write("echo \$this->env->getExtension('templating')->getContainer()->get('templating.helper.stylesheets')->render();\n") + ; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptTokenParser.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptTokenParser.php new file mode 100644 index 0000000000..624b74bc37 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptTokenParser.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * + * @author Fabien Potencier + */ +class JavascriptTokenParser extends \Twig_TokenParser +{ + /** + * Parses a token and returns a node. + * + * @param \Twig_Token $token A \Twig_Token instance + * + * @return \Twig_NodeInterface A \Twig_NodeInterface instance + */ + public function parse(\Twig_Token $token) + { + $expr = $this->parser->getExpressionParser()->parseExpression(); + + // attributes + if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) { + $this->parser->getStream()->next(); + + $attributes = $this->parser->getExpressionParser()->parseExpression(); + } else { + $attributes = new \Twig_Node_Expression_Array(array(), $token->getLine()); + } + + $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + + return new JavascriptNode($expr, $attributes, $token->getLine(), $this->getTag()); + } + + /** + * Gets the tag name associated with this token parser. + * + * @param string The tag name + */ + public function getTag() + { + return 'javascript'; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptsTokenParser.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptsTokenParser.php new file mode 100644 index 0000000000..c25044ac3c --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/JavascriptsTokenParser.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * + * @author Fabien Potencier + */ +class JavascriptsTokenParser extends \Twig_TokenParser +{ + /** + * Parses a token and returns a node. + * + * @param \Twig_Token $token A \Twig_Token instance + * + * @return \Twig_NodeInterface A \Twig_NodeInterface instance + */ + public function parse(\Twig_Token $token) + { + $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + + return new JavascriptsNode($token->getLine(), $this->getTag()); + } + + /** + * Gets the tag name associated with this token parser. + * + * @param string The tag name + */ + public function getTag() + { + return 'javascripts'; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetTokenParser.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetTokenParser.php new file mode 100644 index 0000000000..fd8e1a219c --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetTokenParser.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * + * @author Fabien Potencier + */ +class StylesheetTokenParser extends \Twig_TokenParser +{ + /** + * Parses a token and returns a node. + * + * @param \Twig_Token $token A \Twig_Token instance + * + * @return \Twig_NodeInterface A \Twig_NodeInterface instance + */ + public function parse(\Twig_Token $token) + { + $expr = $this->parser->getExpressionParser()->parseExpression(); + + // attributes + if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) { + $this->parser->getStream()->next(); + + $attributes = $this->parser->getExpressionParser()->parseExpression(); + } else { + $attributes = new \Twig_Node_Expression_Array(array(), $token->getLine()); + } + + $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + + return new StylesheetNode($expr, $attributes, $token->getLine(), $this->getTag()); + } + + /** + * Gets the tag name associated with this token parser. + * + * @param string The tag name + */ + public function getTag() + { + return 'stylesheet'; + } +} diff --git a/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetsTokenParser.php b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetsTokenParser.php new file mode 100644 index 0000000000..54c0ebb5c3 --- /dev/null +++ b/src/Symfony/Bundle/CompatAssetsBundle/Twig/TokenParser/StylesheetsTokenParser.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * + * + * @author Fabien Potencier + */ +class StylesheetsTokenParser extends \Twig_TokenParser +{ + /** + * Parses a token and returns a node. + * + * @param \Twig_Token $token A \Twig_Token instance + * + * @return \Twig_NodeInterface A \Twig_NodeInterface instance + */ + public function parse(\Twig_Token $token) + { + $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + + return new StylesheetsNode($token->getLine(), $this->getTag()); + } + + /** + * Gets the tag name associated with this token parser. + * + * @param string The tag name + */ + public function getTag() + { + return 'stylesheets'; + } +}