Passed
Pull Request — master (#15)
by Enjoys
02:34
created

AssetsExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
eloc 24
c 5
b 1
f 3
dl 0
loc 79
ccs 27
cts 29
cp 0.931
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A asset() 0 21 3
A getExternCss() 0 3 1
A getFunctions() 0 6 1
A getAssetsCollector() 0 3 1
A getExternJs() 0 3 1
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 1
        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 1
        ];
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 6
            $type,
63 6
            array_map(function ($item) {
64 6
                if ($this->loader === null) {
65 6
                    return $item;
66
                }
67
                /** @var string[] $path */
68 1
                $path = (array)$item;
69 1
                if ($this->loader->exists($path[0])) {
70 1
                    $path[0] = $this->loader->getSourceContext($path[0])->getPath();
71
                }
72 1
                return $path;
73 6
            }, $paths),
74 6
            $namespace,
75 6
            $method
76 6
        );
77
    }
78
79
    /**
80
     * @param string $namespace
81
     * @return string
82
     * @throws \Exception
83
     */
84 3
    public function getExternCss(string $namespace = Assets::NAMESPACE_COMMON): string
85
    {
86 3
        return $this->assetsCollector->get('css', $namespace);
87
    }
88
89
    /**
90
     * @param string $namespace
91
     * @return string
92
     * @throws \Exception
93
     */
94 1
    public function getExternJs(string $namespace = Assets::NAMESPACE_COMMON): string
95
    {
96 1
        return $this->assetsCollector->get('js', $namespace);
97
    }
98
99
    /**
100
     * @return Assets
101
     */
102
    public function getAssetsCollector(): Assets
103
    {
104
        return $this->assetsCollector;
105
    }
106
}
107