ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
TwigFormulaLoader.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Assetic package, an OpenSky project.
5  *
6  * (c) 2010-2014 OpenSky Project Inc
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Assetic\Extension\Twig;
13 
17 
24 {
25  private $twig;
26  private $logger;
27 
28  public function __construct(\Twig_Environment $twig, LoggerInterface $logger = null)
29  {
30  $this->twig = $twig;
31  $this->logger = $logger;
32  }
33 
34  public function load(ResourceInterface $resource)
35  {
36  try {
37  $tokens = $this->twig->tokenize($resource->getContent(), (string) $resource);
38  $nodes = $this->twig->parse($tokens);
39  } catch (\Exception $e) {
40  if ($this->logger) {
41  $this->logger->error(sprintf('The template "%s" contains an error: %s', $resource, $e->getMessage()));
42  }
43 
44  return array();
45  }
46 
47  return $this->loadNode($nodes);
48  }
49 
57  private function loadNode(\Twig_Node $node)
58  {
59  $formulae = array();
60 
61  if ($node instanceof AsseticNode) {
62  $formulae[$node->getAttribute('name')] = array(
63  $node->getAttribute('inputs'),
64  $node->getAttribute('filters'),
65  array(
66  'output' => $node->getAttribute('asset')->getTargetPath(),
67  'name' => $node->getAttribute('name'),
68  'debug' => $node->getAttribute('debug'),
69  'combine' => $node->getAttribute('combine'),
70  'vars' => $node->getAttribute('vars'),
71  ),
72  );
73  } elseif ($node instanceof \Twig_Node_Expression_Function) {
74  $name = $node->getAttribute('name');
75 
76  if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) {
77  $arguments = array();
78  foreach ($node->getNode('arguments') as $argument) {
79  $arguments[] = eval('return '.$this->twig->compile($argument).';');
80  }
81 
82  $invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name);
83 
84  $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
85  $filters = $invoker->getFilters();
86  $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
87 
88  if (!isset($options['name'])) {
89  $options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options);
90  }
91 
92  $formulae[$options['name']] = array($inputs, $filters, $options);
93  }
94  }
95 
96  foreach ($node as $child) {
97  if ($child instanceof \Twig_Node) {
98  $formulae += $this->loadNode($child);
99  }
100  }
101 
102  if ($node->hasAttribute('embedded_templates')) {
103  foreach ($node->getAttribute('embedded_templates') as $child) {
104  $formulae += $this->loadNode($child);
105  }
106  }
107 
108  return $formulae;
109  }
110 }
getContent()
Returns the content of the resource.
Add rich text string
The name of the decorator.
loadNode(\Twig_Node $node)
Loads assets from the supplied node.
A resource is something formulae can be loaded from.
load(ResourceInterface $resource)
Loads formulae from a resource.
if(!is_array($argv)) $options
Loads asset formulae from Twig templates.
Create styles array
The data for the language used.
Describes a logger instance.
__construct(\Twig_Environment $twig, LoggerInterface $logger=null)