Passed
Push — master ( 0ed261...e52b37 )
by Jean-Christophe
17:09
created

AssetsManager::js_()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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.3
18
 * @since Ubiquity 2.1.0
19
 *
20
 */
21
class AssetsManager {
22
	private static $assetsFolder = '/assets/';
23
	private static $siteURL;
24
25 9
	private static function gString($template, $variable, $attributes = []) {
26 9
		$implode = UArray::implodeAsso ( $attributes, ' ','=','' );
27 9
		return \sprintf ( $template, $variable, $implode );
28
	}
29
30 1
	private static function script($src, $attributes = []) {
31 1
		return self::gString ( '<script src="%s" %s></script>', $src, $attributes );
32
	}
33
34 9
	private static function stylesheet($link, $attributes = []) {
35 9
		$attributes['type']??='text/css';
36 9
		return self::gString ( '<link href="%s" rel="stylesheet" %s>', $link, $attributes );
37
	}
38
39
	private static function image($src, $attributes = []) {
40
		$attributes['alt']??='Alternate text required';
41
		return self::gString ( '<img src="%s" %s>', $src, $attributes );
42
	}
43
44
	/**
45
	 * Starts the assets manager.
46
	 * Essential to define the siteURL part
47
	 *
48
	 * @param array $config
49
	 */
50 41
	public static function start(&$config) {
51 41
		$siteURL = $config ['siteUrl'] ?? '';
52 41
		self::$siteURL = \rtrim ( $siteURL, '/' );
53 41
	}
54
55
	public static function setAssetsFolder($assetsFolder='/assets/'){
56
		self::$assetsFolder=$assetsFolder;
57
	}
58
59
	/**
60
	 * Returns the absolute or relative url to the resource.
61
	 *
62
	 * @param string $resource
63
	 * @param boolean $absolute
64
	 * @return string
65
	 */
66
	public static function getUrl($resource, $absolute = false) {
67
		if (\strpos ( $resource, '//' ) !== false) {
68
			return $resource;
69
		}
70
		if ($absolute) {
71
			return self::$siteURL . self::$assetsFolder . $resource;
72
		}
73
		return \ltrim ( self::$assetsFolder, '/' ) . $resource;
74
	}
75
76
	/**
77
	 * Returns the absolute or relative url for a resource in the **activeTheme**.
78
	 *
79
	 * @param string $resource
80
	 * @param boolean $absolute
81
	 * @return string
82
	 */
83 9
	public static function getActiveThemeUrl($resource, $absolute = false) {
84 9
		$activeTheme = ThemesManager::getActiveTheme ();
85 9
		return self::getThemeUrl ( $activeTheme, $resource, $absolute );
86
	}
87
88
	/**
89
	 * Returns the absolute or relative url for a resource in a theme.
90
	 *
91
	 * @param string $theme
92
	 * @param string $resource
93
	 * @param boolean $absolute
94
	 * @return string
95
	 */
96 9
	public static function getThemeUrl($theme, $resource, $absolute = false) {
97 9
		if ($absolute) {
98
			return self::$siteURL . self::$assetsFolder . $theme . '/' . $resource;
99
		}
100 9
		return \ltrim ( self::$assetsFolder, '/' ) . $theme . '/' . $resource;
101
	}
102
103
	/**
104
	 * Returns the script inclusion for a javascript resource.
105
	 *
106
	 * @param string $resource The javascript resource to include
107
	 * @param array $attributes The other html attributes of the script element
108
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
109
	 * @return string
110
	 */
111
	public static function js($resource, $attributes = [], $absolute = false) {
112
		return self::script ( self::getUrl ( $resource, $absolute ), $attributes );
113
	}
114
115
	/**
116
	 * Returns the css inclusion for a stylesheet resource.
117
	 *
118
	 * @param string $resource The css resource to include
119
	 * @param array $attributes The other html attributes of the script element
120
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
121
	 * @return string
122
	 */
123
	public static function css($resource, $attributes = [], $absolute = false) {
124
		return self::stylesheet ( self::getUrl ( $resource, $absolute ), $attributes );
125
	}
126
127
	/**
128
	 * Returns the image tag for inclusion.
129
	 *
130
	 * @param string $src The base path to the image
131
	 * @param array $attributes The other html attributes of the image element
132
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
133
	 * @return string
134
	 */
135
	public static function img($src, $attributes = [], $absolute = false) {
136
		return self::image ( self::getUrl ( $src, $absolute ), $attributes );
137
	}
138
139
	/**
140
	 * Returns the script inclusion for a javascript resource in **activeTheme**.
141
	 *
142
	 * @param string $resource The javascript resource to include
143
	 * @param array $attributes The other html attributes of the script element
144
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
145
	 * @return string
146
	 */
147 1
	public static function js_($resource, $attributes = [], $absolute = false) {
148 1
		return self::script ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes );
149
	}
150
151
	/**
152
	 * Returns the css inclusion for a stylesheet resource in **activeTheme**.
153
	 *
154
	 * @param string $resource The css resource to include
155
	 * @param array $attributes The other html attributes of the script element
156
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
157
	 * @return string
158
	 */
159 9
	public static function css_($resource, $attributes = [], $absolute = false) {
160 9
		return self::stylesheet ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes );
161
	}
162
163
	/**
164
	 * Returns the image tag for inclusion in **activeTheme**.
165
	 *
166
	 * @param string $src The base path to the image
167
	 * @param array $attributes The other html attributes of the image element
168
	 * @param boolean $absolute True if url must be absolute (containing siteUrl)
169
	 * @return string
170
	 */
171
	public static function img_($src, $attributes = [], $absolute = false) {
172
		return self::image ( self::getActiveThemeUrl ( $src, $absolute ), $attributes );
173
	}
174
}
175