Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AssetsManager.php
Go to the documentation of this file.
1<?php
2
7
9use Ubiquity\utils\base\UArray;
10
22 private static $assetsFolder = '/assets/';
23 private static $siteURL;
24
25 private static function gString($template, $variable, $attributes = []) {
26 $implode = UArray::implodeAsso ( $attributes, ' ','=','' );
27 return \sprintf ( $template, $variable, $implode );
28 }
29
30 private static function script($src, $attributes = []) {
31 return self::gString ( '<script src="%s" %s></script>', $src, $attributes );
32 }
33
34 private static function stylesheet($link, $attributes = []) {
35 $attributes['type']??='text/css';
36 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
50 public static function start(&$config) {
51 $siteURL = $config ['siteUrl'] ?? '';
52 self::$siteURL = \rtrim ( $siteURL, '/' );
53 }
54
55 public static function setAssetsFolder($assetsFolder='/assets/'){
56 self::$assetsFolder=$assetsFolder;
57 }
58
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
83 public static function getActiveThemeUrl($resource, $absolute = false) {
84 $activeTheme = ThemesManager::getActiveTheme ();
85 return self::getThemeUrl ( $activeTheme, $resource, $absolute );
86 }
87
96 public static function getThemeUrl($theme, $resource, $absolute = false) {
97 if ($absolute) {
98 return self::$siteURL . self::$assetsFolder . $theme . '/' . $resource;
99 }
100 return \ltrim ( self::$assetsFolder, '/' ) . $theme . '/' . $resource;
101 }
102
111 public static function js($resource, $attributes = [], $absolute = false) {
112 return self::script ( self::getUrl ( $resource, $absolute ), $attributes );
113 }
114
123 public static function css($resource, $attributes = [], $absolute = false) {
124 return self::stylesheet ( self::getUrl ( $resource, $absolute ), $attributes );
125 }
126
135 public static function img($src, $attributes = [], $absolute = false) {
136 return self::image ( self::getUrl ( $src, $absolute ), $attributes );
137 }
138
147 public static function js_($resource, $attributes = [], $absolute = false) {
148 return self::script ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes );
149 }
150
159 public static function css_($resource, $attributes = [], $absolute = false) {
160 return self::stylesheet ( self::getActiveThemeUrl ( $resource, $absolute ), $attributes );
161 }
162
171 public static function img_($src, $attributes = [], $absolute = false) {
172 return self::image ( self::getActiveThemeUrl ( $src, $absolute ), $attributes );
173 }
174}
Assets manager for css and js inclusions in templates.
static js_($resource, $attributes=[], $absolute=false)
Returns the script inclusion for a javascript resource in activeTheme.
static image($src, $attributes=[])
static img($src, $attributes=[], $absolute=false)
Returns the image tag for inclusion.
static css($resource, $attributes=[], $absolute=false)
Returns the css inclusion for a stylesheet resource.
static img_($src, $attributes=[], $absolute=false)
Returns the image tag for inclusion in activeTheme.
static gString($template, $variable, $attributes=[])
static start(&$config)
Starts the assets manager.
static js($resource, $attributes=[], $absolute=false)
Returns the script inclusion for a javascript resource.
static css_($resource, $attributes=[], $absolute=false)
Returns the css inclusion for a stylesheet resource in activeTheme.
static script($src, $attributes=[])
static getActiveThemeUrl($resource, $absolute=false)
Returns the absolute or relative url for a resource in the activeTheme.
static getThemeUrl($theme, $resource, $absolute=false)
Returns the absolute or relative url for a resource in a theme.
static getUrl($resource, $absolute=false)
Returns the absolute or relative url to the resource.
static setAssetsFolder($assetsFolder='/assets/')
static stylesheet($link, $attributes=[])
Assets managment.