1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Helpers\View; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Scripts |
7
|
|
|
* @package Nip\Helpers\View |
8
|
|
|
*/ |
9
|
|
|
class Scripts extends AbstractHelper |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $files = []; |
16
|
|
|
|
17
|
|
|
protected $rawScripts = []; |
18
|
|
|
|
19
|
|
|
protected $defaultPlaceholder = "head"; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $file |
23
|
|
|
* @param bool $placeholder |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function add($file, $placeholder = false) |
27
|
|
|
{ |
28
|
|
|
return $this->addFile($file, 'add', $placeholder); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $file |
33
|
|
|
* @param string $direction |
34
|
|
|
* @param bool $placeholder |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function addFile($file, $direction = 'add', $placeholder = false) |
38
|
|
|
{ |
39
|
|
|
if ($placeholder === false || empty($placeholder)) { |
40
|
|
|
$placeholder = $this->defaultPlaceholder; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
if (!isset($this->files[$placeholder]) || !is_array($this->files[$placeholder])) { |
|
|
|
|
44
|
|
|
$this->files[$placeholder] = []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($direction == 'prepend') { |
48
|
|
|
array_unshift($this->files[$placeholder], $file); |
49
|
|
|
} else { |
50
|
|
|
$this->files[$placeholder][] = $file; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $content |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function addRaw($content) |
61
|
|
|
{ |
62
|
|
|
$this->rawScripts[] = $content; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $file |
69
|
|
|
* @param bool $placeholder |
70
|
|
|
* @return Scripts |
71
|
|
|
*/ |
72
|
|
|
public function prepend($file, $placeholder = false) |
73
|
|
|
{ |
74
|
|
|
return $this->addFile($file, 'prepend', $placeholder); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function __toString() |
81
|
|
|
{ |
82
|
|
|
return $this->render($this->defaultPlaceholder); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param bool $placeholder |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function render($placeholder = false) |
90
|
|
|
{ |
91
|
|
|
if ($placeholder == false && empty($placeholder)) { |
|
|
|
|
92
|
|
|
$placeholder = $this->defaultPlaceholder; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (isset($this->files[$placeholder])) { |
96
|
|
|
return $this->renderHMTL($this->files[$placeholder]); |
97
|
|
|
} |
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $files |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function renderHMTL($files) |
106
|
|
|
{ |
107
|
|
|
$return = ''; |
108
|
|
|
if (is_array($files)) { |
109
|
|
|
$internal = []; |
110
|
|
|
$external = []; |
111
|
|
|
|
112
|
|
|
foreach ($files as $file) { |
113
|
|
|
if (preg_match('/https?:\/\//', $file)) { |
114
|
|
|
$external[] = $file; |
115
|
|
|
} else { |
116
|
|
|
$internal[] = $file; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
if (count($external)) { |
122
|
|
|
foreach ($external as $file) { |
123
|
|
|
$return .= $this->buildTag($file); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
foreach ($internal as $file) { |
128
|
|
|
$return .= $this->buildTag($this->buildURL($file)); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $path |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function buildTag($path) |
140
|
|
|
{ |
141
|
|
|
return "<script type=\"text/javascript\" src=\"$path\"></script>\r\n"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $source |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function buildURL($source) |
149
|
|
|
{ |
150
|
|
|
return asset('/scripts/' . $source . '.js'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function renderRaw() |
157
|
|
|
{ |
158
|
|
|
$return = ''; |
159
|
|
|
if (count($this->rawScripts)) { |
160
|
|
|
$return .= '<script type="text/javascript">'; |
161
|
|
|
$return .= implode("\r\n", $this->rawScripts); |
162
|
|
|
$return .= '</script>'; |
163
|
|
|
} |
164
|
|
|
return $return; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.