Passed
Push — main ( 3acca2...274d1b )
by Oscar
03:30
created

WebpackEncoreExtension::getWebpackJsSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
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
            dump('reset');
61
            $entrypoint->reset(); // Ensure reset after access files
62
        }
63
    }
64
65
    public function getWebpackCssSource(string $entryName, string $entrypointName = '_default'): string
66
    {
67
        return $this->concatenateFileSources($this->getWebpackCssFiles($entryName, $entrypointName));
68
    }
69
70
    /**
71
     * @return string[]
72
     */
73
    public function getWebpackJsFiles(string $entryName, string $entrypointName = '_default'): array
74
    {
75
        $entrypoint = $this->getEntrypointLookup($entrypointName);
76
77
        try {
78
            $entrypoint->reset();
79
80
            return $entrypoint->getJavaScriptFiles($entryName);
81
        } finally {
82
            $entrypoint->reset();
83
        }
84
    }
85
86
    public function getWebpackJsSource(string $entryName, string $entrypointName = '_default'): string
87
    {
88
        return $this->concatenateFileSources($this->getWebpackJsFiles($entryName, $entrypointName));
89
    }
90
91
    /**
92
     * @param string[] $files
93
     */
94
    private function concatenateFileSources(array $files): string
95
    {
96
        return array_reduce(
97
            $files,
98
            function ($source, $entry) {
99
                if (empty(parse_url($entry)['scheme'])) {
100
                    foreach ($this->buildPaths as $path) {
101
                        if (is_readable($path.'/'.$entry)) {
102
                            $entry = $path.$entry;
103
                            break;
104
                        }
105
                    }
106
                }
107
108
                return $source.file_get_contents($entry);
109
            },
110
            ''
111
        );
112
    }
113
114
    private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
115
    {
116
        // $this->container->get('webpack_encore.entrypoint_lookup_collection')
117
        return $this->collection->getEntrypointLookup($entrypointName);
118
    }
119
}
120