Completed
Push — master ( 55311e...acc5b2 )
by Craig
06:34
created

AssetExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 5
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\ThemeModule\Twig\Extension;
13
14
use Zikula\Common\Translator\TranslatorInterface;
15
use Zikula\ThemeModule\Engine\AssetBag;
16
use Zikula\ThemeModule\Engine\Engine;
17
18
class AssetExtension extends \Twig_Extension
19
{
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    private $translator;
24
25
    /**
26
     * @var AssetBag
27
     */
28
    private $styleSheets;
29
30
    /**
31
     * @var AssetBag
32
     */
33
    private $scripts;
34
35
    /**
36
     * @var AssetBag
37
     */
38
    private $headers;
39
40
    /**
41
     * @var AssetBag
42
     */
43
    private $footers;
44
45
    /**
46
     * @var Engine
47
     */
48
    private $engine;
49
50
    /**
51
     * AssetExtension constructor.
52
     * @param AssetBag $styleSheets
53
     * @param AssetBag $scripts
54
     * @param AssetBag $headers
55
     * @param AssetBag $footers
56
     */
57
    public function __construct(
58
        AssetBag $styleSheets,
59
        AssetBag $scripts,
60
        AssetBag $headers,
61
        AssetBag $footers,
62
        Engine $engine
63
    ) {
64
        $this->styleSheets = $styleSheets;
65
        $this->scripts = $scripts;
66
        $this->headers = $headers;
67
        $this->footers = $footers;
68
        $this->engine = $engine;
69
    }
70
71
    /**
72
     * Returns a list of functions to add to the existing list.
73
     *
74
     * @return array An array of functions
75
     */
76
    public function getFunctions()
77
    {
78
        return [
79
            new \Twig_SimpleFunction('pageAddAsset', [$this, 'pageAddAsset']),
80
        ];
81
    }
82
83
    /**
84
     * Zikula allows only the following asset types
85
     * <ul>
86
     *  <li>stylesheet</li>
87
     *  <li>javascript</li>
88
     *  <li>header</li>
89
     *  <li>footer</li>
90
     * </ul>
91
     *
92
     * @param string $type
93
     * @param string $value
94
     * @param int $weight
95
     */
96
    public function pageAddAsset($type, $value, $weight = AssetBag::WEIGHT_DEFAULT)
97
    {
98
        if (empty($type) || empty($value)) {
99
            throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
100
        }
101
        if (!in_array($type, ['stylesheet', 'javascript', 'header', 'footer']) || !is_numeric($weight)) {
102
            throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
103
        }
104
105
        // ensure proper variable types
106
        $value = (string) $value;
107
        $type = (string) $type;
108
        $weight = (int) $weight;
109
110
        // @todo remove this code block at Core-2.0 because all themes are twig based
111
        $themeBundle = $this->engine->getTheme();
112
        if (isset($themeBundle) && !$themeBundle->isTwigBased()) {
113
            \PageUtil::addVar($type, $value);
114
115
            return;
116
        }
117
118
        if ('stylesheet' == $type) {
119
            $this->styleSheets->add([$value => $weight]);
120
        } elseif ('javascript' == $type) {
121
            $this->scripts->add([$value => $weight]);
122
        } elseif ('header' == $type) {
123
            $this->headers->add([$value => $weight]);
124
        } elseif ('footer' == $type) {
125
            $this->footers->add([$value => $weight]);
126
        }
127
    }
128
}
129