JS   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 157
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A file() 0 6 1
A script() 0 6 1
A ready() 0 6 1
A setting() 0 6 1
A renderScript() 0 4 1
A renderReady() 0 4 1
A renderSettings() 0 6 1
A resolveSettings() 0 4 1
A render() 0 20 3
A renderScriptTag() 0 19 4
A renderFiles() 0 9 2
1
<?php
2
3
/**
4
 * Javascript class
5
 */
6
namespace Rocket\UI\Script;
7
8
/**
9
 * Javascript class
10
 *
11
 * Store all javascript needed for the page to output it in a single block at the end of the page
12
 */
13
class JS
14
{
15
    /**
16
     * Holds all javascript content
17
     *
18
     * @var array {
19
     *     @var array $file All files that need to be included
20
     *     @var array $script All raw scripts
21
     *     @var array $ready All scripts that need to be wrapped in "document.ready()"
22
     *     @var array $settings The settings for the javascript page
23
     * }
24
     */
25
    protected $queue;
26
27
    /**
28
     * Prepare the queue
29
     */
30 30
    public function __construct($queue)
31
    {
32 30
        $this->queue = $queue + ['file' => [], 'script' => [], 'ready' => [], 'setting' => []];
33 30
    }
34
35
    /**
36
     * Add a file
37
     *
38
     * @param string $content
39
     * @return $this
40
     */
41 3
    public function file($content)
42
    {
43 3
        $this->queue['file'][] = $content;
44
45 3
        return $this;
46
    }
47
48
    /**
49
     * Add a raw script
50
     *
51
     * @param string $content
52
     * @return $this
53
     */
54 3
    public function script($content)
55
    {
56 3
        $this->queue['script'][] = $content;
57
58 3
        return $this;
59
    }
60
61
    /**
62
     * Add a script to wrap inside `document.ready()`
63
     *
64
     * @param string $content
65
     * @return $this
66
     */
67 3
    public function ready($content)
68
    {
69 3
        $this->queue['ready'][] = $content;
70
71 3
        return $this;
72
    }
73
74
    /**
75
     * Add settings to the page
76
     *
77
     * @param array $content
78
     * @return $this
79
     */
80 21
    public function setting(array $content)
81
    {
82 21
        $this->queue['setting'] = array_replace_recursive($this->queue['setting'], $content);
83
84 21
        return $this;
85
    }
86
87
    /**
88
     * Outputs the scripts
89
     *
90
     * @return string
91
     */
92 30
    public function render()
93
    {
94 30
        $output = '';
95
96
        //files
97 30
        if (count($this->queue['file'])) {
98 3
            $output .= $this->renderFiles();
99
        }
100
101 30
        $script_tag = $this->renderScriptTag();
102
103 30
        if (!empty($script_tag)) {
104 24
            $output .= '<script type="text/javascript">';
105 24
            $output .= "var APP = APP || {'settings': {}, 'behaviors':{}, 'locale':{}, 'utilities':{}};";
106 24
            $output .= $this->renderScriptTag();
107 24
            $output .= '</script>';
108
        }
109
110 30
        return $output;
111
    }
112
113 30
    protected function renderScriptTag()
114
    {
115 30
        $output = '';
116 30
        if (count($this->queue['setting'])) {
117 18
            $output .= $this->renderSettings();
118
        }
119
120
        //standard script
121 30
        if (count($this->queue['script'])) {
122 3
            $output .= $this->renderScript();
123
        }
124
125
        //jquery document.ready
126 30
        if (count($this->queue['ready'])) {
127 3
            $output .= $this->renderReady();
128
        }
129
130 30
        return $output;
131
    }
132
133 3
    protected function renderFiles()
134
    {
135 3
        $output = '';
136 3
        foreach ($this->queue['file'] as $item) {
137 3
            $output .= '<script type="text/javascript" src="' . $item . '"></script>';
138
        }
139
140 3
        return $output;
141
    }
142
143 3
    protected function renderScript()
144
    {
145 3
        return implode("\n", $this->queue['script']);
146
    }
147
148 3
    protected function renderReady()
149
    {
150 3
        return "\n" . 'jQuery(document).ready(function($) {' . implode("\n//---\n", $this->queue['ready']) . '});';
151
    }
152
153 18
    protected function renderSettings()
154
    {
155 18
        $settings = $this->resolveSettings();
156
157 18
        return 'jQuery.extend(APP.settings, ' . json_encode($settings) . ');';
158
    }
159
160
    /**
161
     * Walk the settings array to output them as a pure array
162
     *
163
     * @return string
164
     */
165 6
    protected function resolveSettings()
166
    {
167 6
        return $this->queue['setting'];
168
    }
169
}
170