init without trunk

This commit is contained in:
Kevin Adametz 2020-07-09 12:49:32 +02:00
parent ed24ac4994
commit bb809e7233
14652 changed files with 177862 additions and 94817 deletions

View file

@ -0,0 +1,71 @@
<?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\AsseticExtension as BaseAsseticExtension;
use Assetic\Factory\AssetFactory;
use Assetic\ValueSupplierInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* Assetic extension.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticExtension extends BaseAsseticExtension
{
private $useController;
private $templateNameParser;
private $enabledBundles;
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,106 @@
<?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)
{
$vars = array();
foreach ($asset->getVars() as $var) {
$vars[] = new \Twig_Node_Expression_Constant($var, $this->getTemplateLine());
// Retrieves values of assetic vars from the context, $context['assetic']['vars'][$var].
$vars[] = new \Twig_Node_Expression_GetAttr(
new \Twig_Node_Expression_GetAttr(
new \Twig_Node_Expression_Name('assetic', $this->getTemplateLine()),
new \Twig_Node_Expression_Constant('vars', $this->getTemplateLine()),
new \Twig_Node_Expression_Array(array(), $this->getTemplateLine()),
\Twig_Template::ARRAY_CALL,
$this->getTemplateLine()
),
new \Twig_Node_Expression_Constant($var, $this->getTemplateLine()),
new \Twig_Node_Expression_Array(array(), $this->getTemplateLine()),
\Twig_Template::ARRAY_CALL,
$this->getTemplateLine()
);
}
$compiler
->raw('isset($context[\'assetic\'][\'use_controller\']) && $context[\'assetic\'][\'use_controller\'] ? ')
->subcompile($this->getPathFunction($name, $vars))
->raw(' : ')
->subcompile($this->getAssetFunction(new TargetPathNode($this, $asset, $name)))
;
}
private function getPathFunction($name, array $vars = array())
{
$nodes = array(new \Twig_Node_Expression_Constant('_assetic_'.$name, $this->getTemplateLine()));
if (!empty($vars)) {
$nodes[] = new \Twig_Node_Expression_Array($vars, $this->getTemplateLine());
}
return new \Twig_Node_Expression_Function(
'path',
new \Twig_Node($nodes),
$this->getTemplateLine()
);
}
private function getAssetFunction($path)
{
$arguments = array($path);
if ($this->hasAttribute('package')) {
$arguments[] = new \Twig_Node_Expression_Constant($this->getAttribute('package'), $this->getTemplateLine());
}
return new \Twig_Node_Expression_Function(
'asset',
new \Twig_Node($arguments),
$this->getTemplateLine()
);
}
}
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,116 @@
<?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\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* Assetic node visitor.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticNodeVisitor extends \Twig_BaseNodeVisitor
{
private $templateNameParser;
private $enabledBundles;
public function __construct(TemplateNameParserInterface $templateNameParser, array $enabledBundles)
{
$this->templateNameParser = $templateNameParser;
$this->enabledBundles = $enabledBundles;
}
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
{
return $node;
}
protected function doLeaveNode(\Twig_Node $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 instanceof TemplateReference ? $templateRef->get('bundle') : null;
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_Expression_Array(array(), 0),
\Twig_Template::ARRAY_CALL,
$line
),
new \Twig_Node_Expression_Function(
'path',
new \Twig_Node(array(
new \Twig_Node_Expression_Constant('_assetic_'.$options['name'], $line),
)),
$line
),
new \Twig_Node_Expression_Function(
'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_Node $node, \Twig_Environment $env, &$name = null)
{
if ($node instanceof \Twig_Node_Expression_Function) {
$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,75 @@
<?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\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateNameParserInterface;
/**
* Assetic token parser.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class AsseticTokenParser extends BaseAsseticTokenParser
{
/**
* @var TemplateNameParserInterface|null
*/
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()->getSourceContext()->getName());
} 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 instanceof TemplateReference ? $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 createBodyNode(AssetInterface $asset, \Twig_Node $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
{
return new AsseticNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $tag);
}
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);
}
}