1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/twig-extra-bundle |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
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 Ocubom\TwigExtraBundle\Twig; |
13
|
|
|
|
14
|
|
|
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface; |
15
|
|
|
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; |
16
|
|
|
use Twig\Extension\AbstractExtension; |
17
|
|
|
use Twig\TwigFunction; |
18
|
|
|
|
19
|
|
|
class WebpackEncoreExtension extends AbstractExtension |
20
|
|
|
{ |
21
|
|
|
private EntrypointLookupCollectionInterface $collection; |
22
|
|
|
|
23
|
|
|
/** @var string[] */ |
24
|
|
|
private array $buildPaths; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string[] $buildPaths |
28
|
|
|
*/ |
29
|
|
|
public function __construct(EntrypointLookupCollectionInterface $collection, array $buildPaths) |
30
|
|
|
{ |
31
|
|
|
$this->collection = $collection; |
32
|
|
|
$this->buildPaths = $buildPaths; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return TwigFunction[] |
37
|
|
|
*/ |
38
|
|
|
public function getFunctions(): array |
39
|
|
|
{ |
40
|
|
|
// Alternative implementation to https://github.com/symfony/webpack-encore-bundle/pull/91 |
41
|
|
|
// Based on https://symfonycasts.com/screencast/mailer/encore-inline_css |
42
|
|
|
return [ |
43
|
|
|
new TwigFunction('encore_entry_css_source', [$this, 'getWebpackCssSource']), |
44
|
|
|
new TwigFunction('encore_entry_js_source', [$this, 'getWebpackJsSource']), |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string[] |
50
|
|
|
*/ |
51
|
|
|
public function getWebpackCssFiles(string $entryName, string $entrypointName = '_default'): array |
52
|
|
|
{ |
53
|
|
|
$entrypoint = $this->getEntrypointLookup($entrypointName); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$entrypoint->reset(); |
57
|
|
|
|
58
|
|
|
return $entrypoint->getCssFiles($entryName); |
59
|
|
|
} finally { |
60
|
|
|
$entrypoint->reset(); // Ensure reset after access files |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getWebpackCssSource(string $entryName, string $entrypointName = '_default'): string |
65
|
|
|
{ |
66
|
|
|
return $this->concatenateFileSources($this->getWebpackCssFiles($entryName, $entrypointName)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string[] |
71
|
|
|
*/ |
72
|
|
|
public function getWebpackJsFiles(string $entryName, string $entrypointName = '_default'): array |
73
|
|
|
{ |
74
|
|
|
$entrypoint = $this->getEntrypointLookup($entrypointName); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$entrypoint->reset(); |
78
|
|
|
|
79
|
|
|
return $entrypoint->getJavaScriptFiles($entryName); |
80
|
|
|
} finally { |
81
|
|
|
$entrypoint->reset(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getWebpackJsSource(string $entryName, string $entrypointName = '_default'): string |
86
|
|
|
{ |
87
|
|
|
return $this->concatenateFileSources($this->getWebpackJsFiles($entryName, $entrypointName)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string[] $files |
92
|
|
|
*/ |
93
|
|
|
private function concatenateFileSources(array $files): string |
94
|
|
|
{ |
95
|
|
|
return array_reduce( |
96
|
|
|
$files, |
97
|
|
|
function ($source, $entry) { |
98
|
|
|
if (empty(parse_url($entry)['scheme'])) { |
99
|
|
|
foreach ($this->buildPaths as $path) { |
100
|
|
|
if (is_readable($path.'/'.$entry)) { |
101
|
|
|
$entry = $path.$entry; |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $source.file_get_contents($entry); |
108
|
|
|
}, |
109
|
|
|
'' |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface |
114
|
|
|
{ |
115
|
|
|
return $this->collection->getEntrypointLookup($entrypointName); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|