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