blob: d1aa62aa032b9f89f59552e026cadd5119a53168 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
<?php
namespace Jasny\View\Plugin;
use Jasny\ViewInterface;
use Jasny\View\Twig as TwigView;
use Jasny\View\PluginInterface;
use Assetic\AssetWriter;
use Assetic\Extension\Twig\AsseticExtension;
use Assetic\Extension\Twig\TwigFormulaLoader;
use Assetic\Extension\Twig\TwigResource;
use Assetic\Factory\AssetFactory;
use Assetic\Factory\LazyAssetManager;
use Jasny\Assetic\TwigCachingFormulaLoader;
/**
* Assetic support for Twig
*/
class TwigAssetic implements PluginInterface
{
/**
* @var AssetFactory
*/
protected $factory;
/**
* @var AssetWriter
*/
protected $writer;
/**
* Class constructor
*
* @param AssetFactory $factory
* @param AssetWriter $writer
*/
public function __construct(AssetFactory $factory, AssetWriter $writer)
{
$this->factory = $factory;
$this->writer = $writer;
}
/**
* Check that the view is a twig view
*
* @param ViewInterface $view
* @throws \InvalidArgumentException
*/
protected function assertView(ViewInterface $view)
{
if (!$view instanceof TwigView) {
throw new \InvalidArgumentException("This plugin only works with a Twig view");
}
}
/**
* Create an assetic extension for Twig.
* @codeCoverageIgnore
*
* @return AsseticExtension
*/
protected function createExtension()
{
return new AsseticExtension($this->factory);
}
/**
* Create an assetic formula loader.
* @codeCoverageIgnore
*
* @param \Twig_Environment $twig
* @return TwigFormulaLoader
*/
protected function createFormulaLoader($twig)
{
$class = class_exists('Jasny\Assetic\TwigCachingFormulaLoader')
? TwigCachingFormulaLoader::class
: TwigFormulaLoader::class;
return new $class($twig);
}
/**
* Create an assetic asset manager.
* @codeCoverageIgnore
*
* @return LazyAssetManager
*/
protected function createAssetManager()
{
return new LazyAssetManager($this->factory);
}
/**
* Create an assetic twig resource.
* @codeCoverageIgnore
*
* @param \Twig_Environment $twig
* @param string $template
* @return TwigResource
*/
protected function createResource($twig, $template)
{
return new TwigResource($twig->getLoader(), $template);
}
/**
* Called when the plugin is added to the view.
*
* @param ViewInterface $view
*/
public function onAdd(ViewInterface $view)
{
$this->assertView($view);
$view->getTwig()->addExtension($this->createExtension());
}
/**
* Called when view renders a template.
*
* @param ViewInterface $view
* @param string $template Template filename
* @param array $context
*/
public function onRender(ViewInterface $view, $template, array $context)
{
$this->assertView($view);
$twig = $view->getTwig();
$loader = $this->createFormulaLoader($twig);
$assetManager = $this->createAssetManager($loader);
$assetManager->setLoader('twig', $loader);
$tmpl = $twig->loadTemplate($template);
do {
$name = (string)$tmpl;
$resource = $this->createResource($twig, $name);
$assetManager->addResource($resource, 'twig');
} while ($tmpl = $tmpl->getParent($context));
$this->writer->writeManagerAssets($assetManager);
}
}
|