PHPCRAssetsExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace NVBooster\PHPCRAssetsBundle\Twig;
3
4
/**
5
 * @author nvb
6
 *
7
 */
8
class PHPCRAssetsExtension extends \Twig_Extension
9
{
10
    /**
11
     * @var string
12
     */
13
    private $jsPath;
14
    /**
15
     * @var string
16
     */
17
    private $cssPath;
18
    /**
19
     * @var string
20
     */
21
    private $modesDir;
22
    /**
23
     * @var string
24
     */
25
    private $themesDir;
26
27
    /**
28
     * @var array
29
     */
30
    private $rendered;
31
32
    /**
33
     * @param array $paths
34
     */
35
    public function __construct($paths = array())
36
    {
37
        $this->jsPath = key_exists('js', $paths) ? $paths['js'] : false;
38
        $this->cssPath = key_exists('css', $paths) ? $paths['css'] : false;
39
        $this->modesDir = key_exists('modes_dir', $paths) ? $paths['modes_dir'] : false;
40
        $this->themesDir = key_exists('themes_dir', $paths) ? $paths['themes_dir'] : false;
41
42
        $this->rendered = array();
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     * @see Twig_Extension::getFunctions()
48
     */
49
    public function getFunctions()
50
    {
51
        return array(
52
            new \Twig_SimpleFunction('phpcrassets_codemirror_params', array($this, 'parametersRender'), array('is_safe' => array('html'))),
53
            new \Twig_SimpleFunction('phpcrassets_codemirror_css', array($this, 'getCss')),
54
            new \Twig_SimpleFunction('phpcrassets_codemirror_js', array($this, 'getJs')),
55
        );
56
    }
57
58
    /**
59
     * @param array $parameters
60
     */
61
    public function parametersRender($parameters)
62
    {
63
        return json_encode($parameters);
64
    }
65
66
    /**
67
     * @param array  $parameters
68
     * @param string $force
69
     *
70
     * @return array
71
     */
72
    public function getJs($parameters, $force = false)
73
    {
74
        $urls = array();
75
76
        if ($this->jsPath) {
77
            $urls[] = $this->jsPath;
78
        }
79
80
        if ($this->modesDir) {
81
            $urls[] = preg_replace('@\/$@', '', $this->modesDir) . '/' . $parameters['mode'] . '/' . $parameters['mode'] . '.js';
82
        }
83
84
85
        if (!$force) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $force of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86
            $urls = array_diff($urls, $this->rendered);
87
        }
88
89
        $this->rendered = array_unique(array_merge($this->rendered, $urls));
90
91
        return $urls;
92
    }
93
94
    /**
95
     * @param array  $parameters
96
     * @param string $force
97
     *
98
     * @return array
99
     */
100
    public function getCss($parameters, $force = false)
101
    {
102
        $urls = array();
103
104
        if ($this->cssPath) {
105
            $urls[] = $this->cssPath;
106
        }
107
108
        if ($this->modesDir) {
109
            $urls[] = preg_replace('@\/$@', '', $this->themesDir) . '/' . $parameters['theme'] . '.css';
110
        }
111
112
113
        if (!$force) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $force of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
114
            $urls = array_diff($urls, $this->rendered);
115
        }
116
117
        $this->rendered = array_unique(array_merge($this->rendered, $urls));
118
119
        return $urls;
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     * @see Twig_ExtensionInterface::getName()
125
     */
126
    public function getName()
127
    {
128
        return 'phpcrassets_extension';
129
    }
130
}
131