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