1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Helpers\View; |
4
|
|
|
|
5
|
|
|
use Nip\Utility\Str; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Nip Framework |
9
|
|
|
* |
10
|
|
|
* @category Nip |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
12
|
|
|
*/ |
13
|
|
|
class StyleSheets extends AbstractHelper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $files = []; |
19
|
|
|
|
20
|
|
|
protected $rawStyles = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $file |
24
|
|
|
* @param bool $condition |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
public function add($file, $condition = false) |
28
|
|
|
{ |
29
|
|
|
$this->files[$condition][$file] = $file; |
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $content |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function addRaw($content) |
38
|
|
|
{ |
39
|
|
|
$this->rawStyles[] = $content; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $file |
46
|
|
|
* @param bool $condition |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function prepend($file, $condition = false) |
50
|
|
|
{ |
51
|
|
View Code Duplication |
if (!isset($this->files[$condition]) || !is_array($this->files[$condition])) { |
|
|
|
|
52
|
|
|
$this->files[$condition] = []; |
53
|
|
|
} |
54
|
|
|
array_unshift($this->files[$condition], $file); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $file |
60
|
|
|
* @param bool $condition |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function remove($file, $condition = false) |
64
|
|
|
{ |
65
|
|
|
unset($this->files[$condition][$file]); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function __toString() |
73
|
|
|
{ |
74
|
|
|
$return = ''; |
75
|
|
|
|
76
|
|
|
if (count($this->files)) { |
77
|
|
|
foreach ($this->files as $condition => $files) { |
78
|
|
|
foreach ($files as $file) { |
79
|
|
|
$return .= $this->buildTag($file, $condition); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function renderRaw() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$return = ''; |
93
|
|
|
if (count($this->rawStyles)) { |
94
|
|
|
$return .= '<style type="text/css" media="screen">'; |
95
|
|
|
$return .= implode("\r\n", $this->rawStyles); |
96
|
|
|
$return .= '</style>'; |
97
|
|
|
} |
98
|
|
|
return $return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $path |
103
|
|
|
* @param $condition |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function buildTag($path, $condition = null) |
107
|
|
|
{ |
108
|
|
|
$return = '<link rel="stylesheet" type="text/css" media="screen" href="' . $this->buildURL($path) . '" />'; |
109
|
|
|
if ($condition != null && !empty($condition)) { |
110
|
|
|
$return = '<!--[if ' . $condition . ']>' . $return . '<![endif]-->'; |
111
|
|
|
} |
112
|
|
|
$return .= "\r\n"; |
113
|
|
|
|
114
|
|
|
return $return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $source |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function buildURL($source) |
122
|
|
|
{ |
123
|
|
|
if (Str::startsWith($source, ['http', 'https'])) { |
124
|
|
|
return $source; |
125
|
|
|
} else { |
126
|
|
|
return asset('/stylesheets/' . $source . '.css'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.