Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Twig;
use Assetic\ValueSupplierInterface;
use Assetic\Extension\Twig\AsseticExtension as BaseAsseticExtension;
use Assetic\Factory\AssetFactory;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* Assetic extension.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticExtension extends BaseAsseticExtension
{
private $useController;
public function __construct(AssetFactory $factory, TemplateNameParserInterface $templateNameParser, $useController = false, $functions = array(), $enabledBundles = array(), ValueSupplierInterface $valueSupplier = null)
{
parent::__construct($factory, $functions, $valueSupplier);
$this->useController = $useController;
$this->templateNameParser = $templateNameParser;
$this->enabledBundles = $enabledBundles;
}
public function getTokenParsers()
{
return array(
$this->createTokenParser('javascripts', 'js/*.js'),
$this->createTokenParser('stylesheets', 'css/*.css'),
$this->createTokenParser('image', 'images/*', true),
);
}
public function getNodeVisitors()
{
return array(
new AsseticNodeVisitor($this->templateNameParser, $this->enabledBundles),
);
}
public function getGlobals()
{
$globals = parent::getGlobals();
$globals['assetic']['use_controller'] = $this->useController;
return $globals;
}
private function createTokenParser($tag, $output, $single = false)
{
$tokenParser = new AsseticTokenParser($this->factory, $tag, $output, $single, array('package'));
$tokenParser->setTemplateNameParser($this->templateNameParser);
$tokenParser->setEnabledBundles($this->enabledBundles);
return $tokenParser;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Twig;
use Assetic\Asset\AssetInterface;
use Assetic\Extension\Twig\AsseticNode as BaseAsseticNode;
/**
* Assetic node.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticNode extends BaseAsseticNode
{
protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset, $name)
{
$compiler
->raw('isset($context[\'assetic\'][\'use_controller\']) && $context[\'assetic\'][\'use_controller\'] ? ')
->subcompile($this->getPathFunction($name))
->raw(' : ')
->subcompile($this->getAssetFunction(new TargetPathNode($this, $asset, $name)))
;
}
private function getPathFunction($name)
{
return new \Twig_Node_Expression_Function(
version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
? new \Twig_Node_Expression_Name('path', $this->getLine()) : 'path',
new \Twig_Node(array(new \Twig_Node_Expression_Constant('_assetic_'.$name, $this->getLine()))),
$this->getLine()
);
}
private function getAssetFunction($path)
{
$arguments = array($path);
if ($this->hasAttribute('package')) {
$arguments[] = new \Twig_Node_Expression_Constant($this->getAttribute('package'), $this->getLine());
}
return new \Twig_Node_Expression_Function(
version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
? new \Twig_Node_Expression_Name('asset', $this->getLine()) : 'asset',
new \Twig_Node($arguments),
$this->getLine()
);
}
}
class TargetPathNode extends AsseticNode
{
private $node;
private $asset;
private $name;
public function __construct(AsseticNode $node, AssetInterface $asset, $name)
{
$this->node = $node;
$this->asset = $asset;
$this->name = $name;
}
public function compile(\Twig_Compiler $compiler)
{
BaseAsseticNode::compileAssetUrl($compiler, $this->asset, $this->name);
}
public function getLine()
{
return $this->node->getLine();
}
}

View File

@@ -0,0 +1,120 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Twig;
use Assetic\Extension\Twig\AsseticFilterFunction;
use Symfony\Bundle\AsseticBundle\Exception\InvalidBundleException;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* Assetic node visitor.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticNodeVisitor implements \Twig_NodeVisitorInterface
{
private $templateNameParser;
private $enabledBundles;
private $useNodeName;
public function __construct(TemplateNameParserInterface $templateNameParser, array $enabledBundles)
{
$this->templateNameParser = $templateNameParser;
$this->enabledBundles = $enabledBundles;
$this->useNodeName = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<');
}
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
return $node;
}
public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if (!$formula = $this->checkNode($node, $env, $name)) {
return $node;
}
// check the bundle
$templateRef = $this->templateNameParser->parse($env->getParser()->getStream()->getFilename());
$bundle = $templateRef->get('bundle');
if ($bundle && !in_array($bundle, $this->enabledBundles)) {
throw new InvalidBundleException($bundle, "the $name() function", $templateRef->getLogicalName(), $this->enabledBundles);
}
list($input, $filters, $options) = $formula;
$line = $node->getLine();
// check context and call either asset() or path()
return new \Twig_Node_Expression_Conditional(
new \Twig_Node_Expression_GetAttr(
new \Twig_Node_Expression_Name('assetic', $line),
new \Twig_Node_Expression_Constant('use_controller', $line),
new \Twig_Node(),
\Twig_TemplateInterface::ARRAY_CALL,
$line
),
new \Twig_Node_Expression_Function(
$this->useNodeName ? new \Twig_Node_Expression_Name('path', $line) : 'path',
new \Twig_Node(array(
new \Twig_Node_Expression_Constant('_assetic_'.$options['name'], $line),
)),
$line
),
new \Twig_Node_Expression_Function(
$this->useNodeName ? new \Twig_Node_Expression_Name('asset', $line) : 'asset',
new \Twig_Node(array($node, new \Twig_Node_Expression_Constant(isset($options['package']) ? $options['package'] : null, $line))),
$line
),
$line
);
}
/**
* Extracts formulae from filter function nodes.
*
* @return array|null The formula
*/
private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env, & $name = null)
{
if ($node instanceof \Twig_Node_Expression_Function) {
$name = $this->useNodeName
? $node->getNode('name')->getAttribute('name')
: $node->getAttribute('name');
if ($env->getFunction($name) instanceof AsseticFilterFunction) {
$arguments = array();
foreach ($node->getNode('arguments') as $argument) {
$arguments[] = eval('return '.$env->compile($argument).';');
}
$invoker = $env->getExtension('assetic')->getFilterInvoker($name);
$factory = $invoker->getFactory();
$inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
$filters = $invoker->getFilters();
$options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
if (!isset($options['name'])) {
$options['name'] = $factory->generateAssetName($inputs, $filters);
}
return array($inputs, $filters, $options);
}
}
}
public function getPriority()
{
return 0;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Twig;
use Assetic\Asset\AssetInterface;
use Assetic\Extension\Twig\AsseticTokenParser as BaseAsseticTokenParser;
use Symfony\Bundle\AsseticBundle\Exception\InvalidBundleException;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* Assetic token parser.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticTokenParser extends BaseAsseticTokenParser
{
private $templateNameParser;
private $enabledBundles;
public function setTemplateNameParser(TemplateNameParserInterface $templateNameParser)
{
$this->templateNameParser = $templateNameParser;
}
public function setEnabledBundles(array $enabledBundles = null)
{
$this->enabledBundles = $enabledBundles;
}
public function parse(\Twig_Token $token)
{
if ($this->templateNameParser && is_array($this->enabledBundles)) {
// check the bundle
$templateRef = null;
try {
$templateRef = $this->templateNameParser->parse($this->parser->getStream()->getFilename());
} catch (\RuntimeException $e) {
// this happens when the filename isn't a Bundle:* url
// and it contains ".."
} catch (\InvalidArgumentException $e) {
// this happens when the filename isn't a Bundle:* url
// but an absolute path instead
}
$bundle = $templateRef ? $templateRef->get('bundle') : null;
if ($bundle && !in_array($bundle, $this->enabledBundles)) {
throw new InvalidBundleException($bundle, "the {% {$this->getTag()} %} tag", $templateRef->getLogicalName(), $this->enabledBundles);
}
}
return parent::parse($token);
}
protected function createNode(AssetInterface $asset, \Twig_NodeInterface $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
{
return new AsseticNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $tag);
}
}