added CompatAssetsBundle to reintroduce the deprecated css/js helpers/tags

Just add this line in your configuration to enable it:

    <import resource="CompatAssetsBundle/Resources/config/assets.xml" />

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.
This commit is contained in:
Fabien Potencier 2011-01-05 14:11:43 +01:00
parent 3ab82cbd53
commit 598d458a3c
13 changed files with 715 additions and 0 deletions

View File

@ -0,0 +1,21 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* CompatAssetsBundle.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class CompatAssetsBundle extends Bundle
{
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" ?>
<container xmlns="http://www.symfony-project.org/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="templating.helper.javascripts.class">Symfony\Bundle\CompatAssetsBundle\Templating\Helper\JavascriptsHelper</parameter>
<parameter key="templating.helper.stylesheets.class">Symfony\Bundle\CompatAssetsBundle\Templating\Helper\StylesheetsHelper</parameter>
</parameters>
<services>
<service id="templating.helper.javascripts" class="%templating.helper.javascripts.class%">
<tag name="templating.helper" alias="javascripts" />
<argument type="service" id="templating.helper.assets" />
</service>
<service id="templating.helper.stylesheets" class="%templating.helper.stylesheets.class%">
<tag name="templating.helper" alias="stylesheets" />
<argument type="service" id="templating.helper.assets" />
</service>
<service id="twig.extension.assets" class="Symfony\Bundle\CompatAssetsBundle\Twig\Extension\AssetsExtension">
<tag name="twig.extension" />
<argument type="service" id="service_container" />
</service>
</services>
</container>

View File

@ -0,0 +1,113 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Templating\Helper;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\Templating\Helper\AssetsHelper;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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:
*
* <code>
* $view['javascripts']->add('foo.js');
* echo $view['javascripts'];
* </code>
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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('<script type="text/javascript" src="%s"%s></script>', $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';
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Templating\Helper;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\Templating\Helper\AssetsHelper;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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:
*
* <code>
* $view['stylesheets']->add('foo.css', array('media' => 'print'));
* echo $view['stylesheets'];
* </code>
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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('<link href="%s" rel="stylesheet" type="text/css"%s />', $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';
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser\StylesheetTokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser\StylesheetsTokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser\JavascriptTokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser\JavascriptsTokenParser;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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';
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\Node;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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 <fabien.potencier@symfony-project.com>
*/
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")
;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\Node;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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 <fabien.potencier@symfony-project.com>
*/
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")
;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\Node;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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 <fabien.potencier@symfony-project.com>
*/
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")
;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\Node;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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 <fabien.potencier@symfony-project.com>
*/
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")
;
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\Node\JavascriptNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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';
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\Node\JavascriptsNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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';
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\Node\StylesheetNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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';
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Symfony\Bundle\CompatAssetsBundle\Twig\TokenParser;
use Symfony\Bundle\CompatAssetsBundle\Twig\Node\StylesheetsNode;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
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';
}
}