Passed
Push — master ( c2c9e6...d51752 )
by Enjoys
53s queued 13s
created

AssetsExtension::getExternJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\AssetsCollector\Extensions\Twig;
6
7
use Enjoys\AssetsCollector\Assets;
8
use Twig\Error\LoaderError;
9
use Twig\Extension\AbstractExtension;
10
use Twig\Loader\LoaderInterface;
11
use Twig\TwigFunction;
12
13
/**
14
 * Class AssetsExtension
15
 * Set assets from Twig. In the css example, but for js the same
16
 * {{  asset('css', [{0: '//cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css', 'minify': false}]) }}
17
 * {{  asset('css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css') }}
18
 * {{  asset('css', ['path/style1.css', 'style2.css']) }}
19
 *
20
 * Output
21
 * {{ eCSS() }}
22
 * {{ eJS() }}
23
 *
24
 * @package Enjoys\AssetsCollector\Extensions\Twig
25
 */
26
class AssetsExtension extends AbstractExtension
27
{
28
    /**
29
     * @var Assets
30
     */
31
    private Assets $assetsCollector;
32
    private ?LoaderInterface $loader;
33
34 6
    public function __construct(Assets $assetsCollector, LoaderInterface $loader = null)
35
    {
36 6
        $this->assetsCollector = $assetsCollector;
37 6
        $this->loader = $loader;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    public function getFunctions(): array
44
    {
45
        return [
46 1
            new TwigFunction('asset', [$this, 'asset']),
47 1
            new TwigFunction('eCSS', [$this, 'getExternCss'], ['is_safe' => ['html']]),
48 1
            new TwigFunction('eJS', [$this, 'getExternJs'], ['is_safe' => ['html']]),
49
        ];
50
    }
51
52
    /**
53
     * @throws LoaderError
54
     */
55 6
    public function asset(
56
        string $type,
57
        array $paths = [],
58
        string $namespace = Assets::NAMESPACE_COMMON,
59
        string $method = 'push'
60
    ): void {
61 6
        $this->assetsCollector->add(
62
            $type,
63 6
            array_map(function ($item) {
64 6
                if ($this->loader === null) {
65 6
                    return $item;
66
                }
67 1
                $path = (array)$item;
68 1
                if ($this->loader->exists($path[0])) {
69 1
                    $path[0] = $this->loader->getSourceContext($path[0])->getPath();
70
                }
71 1
                return $path;
72
            }, $paths),
73
            $namespace,
74
            $method
75
        );
76
    }
77
78
    /**
79
     * @param string $namespace
80
     * @return string
81
     * @throws \Exception
82
     */
83 3
    public function getExternCss(string $namespace = Assets::NAMESPACE_COMMON): string
84
    {
85 3
        return $this->assetsCollector->get('css', $namespace);
86
    }
87
88
    /**
89
     * @param string $namespace
90
     * @return string
91
     * @throws \Exception
92
     */
93 1
    public function getExternJs(string $namespace = Assets::NAMESPACE_COMMON): string
94
    {
95 1
        return $this->assetsCollector->get('js', $namespace);
96
    }
97
98
    /**
99
     * @return Assets
100
     */
101
    public function getAssetsCollector(): Assets
102
    {
103
        return $this->assetsCollector;
104
    }
105
}
106