|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Assets managment |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Ubiquity\assets; |
|
7
|
|
|
|
|
8
|
|
|
use Ubiquity\themes\ThemesManager; |
|
9
|
|
|
use Ubiquity\utils\base\UArray; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Assets manager for css and js inclusions in templates. |
|
13
|
|
|
* Ubiquity\assets$AssetsManager |
|
14
|
|
|
* This class is part of Ubiquity |
|
15
|
|
|
* |
|
16
|
|
|
* @author jcheron <[email protected]> |
|
17
|
|
|
* @version 1.0.1 |
|
18
|
|
|
* @since Ubiquity 2.1.0 |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
class AssetsManager { |
|
22
|
|
|
const ASSETS_FOLDER = '/public/assets/'; |
|
23
|
|
|
private static $siteURL; |
|
24
|
|
|
|
|
25
|
7 |
|
private static function gString($template, $variable, $attributes = []) { |
|
26
|
7 |
|
$implode = UArray::implodeAsso ( $attributes, ' ' ); |
|
27
|
7 |
|
return sprintf ( $template, $variable, $implode ); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
private static function script($src, $attributes = []) { |
|
31
|
1 |
|
return self::gString ( '<script type="text/javascript" src="%s" %s></script>', $src, $attributes ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
7 |
|
private static function stylesheet($link, $attributes = []) { |
|
35
|
7 |
|
return self::gString ( '<link href="%s" type="text/css" rel="stylesheet" %s>', $link, $attributes ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Starts the assets manager. |
|
40
|
|
|
* Essential to define the siteURL part |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $config |
|
43
|
|
|
*/ |
|
44
|
37 |
|
public static function start(&$config) { |
|
45
|
37 |
|
$siteURL = $config ['siteUrl'] ?? ''; |
|
46
|
37 |
|
self::$siteURL = rtrim ( $siteURL, '/' ); |
|
47
|
37 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the absolute or relative url to the resource. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $resource |
|
53
|
|
|
* @param boolean $absolute |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public static function getUrl($resource, $absolute = false) { |
|
57
|
1 |
|
if (strpos ( $resource, '//' ) !== false) { |
|
58
|
1 |
|
return $resource; |
|
59
|
|
|
} |
|
60
|
|
|
if ($absolute) { |
|
61
|
|
|
return self::$siteURL . self::ASSETS_FOLDER . $resource; |
|
62
|
|
|
} |
|
63
|
|
|
return ltrim ( self::ASSETS_FOLDER, '/' ) . $resource; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the absolute or relative url for a resource in the **activeTheme**. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $resource |
|
70
|
|
|
* @param boolean $absolute |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
7 |
|
public static function getActiveThemeUrl($resource, $absolute = false) { |
|
74
|
7 |
|
$activeTheme = ThemesManager::getActiveTheme (); |
|
75
|
7 |
|
return self::getThemeUrl ( $activeTheme, $resource, $absolute ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the absolute or relative url for a resource in a theme. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $theme |
|
82
|
|
|
* @param string $resource |
|
83
|
|
|
* @param boolean $absolute |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
7 |
|
public static function getThemeUrl($theme, $resource, $absolute = false) { |
|
87
|
7 |
|
if ($absolute) { |
|
88
|
|
|
return self::$siteURL . self::ASSETS_FOLDER . $theme . '/' . $resource; |
|
89
|
|
|
} |
|
90
|
7 |
|
return ltrim ( self::ASSETS_FOLDER, '/' ) . $theme . '/' . $resource; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the script inclusion for a javascript resource. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $resource The javascript resource to include |
|
97
|
|
|
* @param array $attributes The other html attributes of the script element |
|
98
|
|
|
* @param boolean $absolute True if url must be absolute (containing siteUrl) |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public static function js($resource, $attributes = [], $absolute = false) { |
|
102
|
1 |
|
return self::script ( self::getUrl ( $resource, $absolute ), $attributes ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the css inclusion for a stylesheet resource. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $resource The css resource to include |
|
109
|
|
|
* @param array $attributes The other html attributes of the script element |
|
110
|
|
|
* @param boolean $absolute True if url must be absolute (containing siteUrl) |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function css($resource, $attributes = [], $absolute = false) { |
|
114
|
1 |
|
return self::stylesheet ( self::getUrl ( $resource, $absolute ), $attributes ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the script inclusion for a javascript resource in **activeTheme**. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $resource The javascript resource to include |
|
121
|
|
|
* @param array $attributes The other html attributes of the script element |
|
122
|
|
|
* @param boolean $absolute True if url must be absolute (containing siteUrl) |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function js_($resource, $attributes = [], $absolute = false) { |
|
126
|
|
|
return self::script ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns the css inclusion for a stylesheet resource in **activeTheme**. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $resource The css resource to include |
|
133
|
|
|
* @param array $attributes The other html attributes of the script element |
|
134
|
|
|
* @param boolean $absolute True if url must be absolute (containing siteUrl) |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
7 |
|
public static function css_($resource, $attributes = [], $absolute = false) { |
|
138
|
7 |
|
return self::stylesheet ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes ); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|