Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Scriptloader | |
0.00% |
0 / 67 |
|
0.00% |
0 / 11 |
2256 | |
0.00% |
0 / 1 |
| contextInit | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| setPaths | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
240 | |||
| addFile | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| addCssFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addJsFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addCssFiles | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| addJsFiles | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| addJsVar | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addJsVars | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| getJsFiles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
90 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * The scriptloader helper enables you to load web resources dynamically. It |
| 27 | * now CSS and JS resources. |
| 28 | * |
| 29 | * @access public |
| 30 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 31 | * @package tao |
| 32 | |
| 33 | */ |
| 34 | class tao_helpers_Scriptloader |
| 35 | { |
| 36 | // --- ASSOCIATIONS --- |
| 37 | |
| 38 | |
| 39 | // --- ATTRIBUTES --- |
| 40 | |
| 41 | /** |
| 42 | * Short description of attribute CSS |
| 43 | * |
| 44 | * @access public |
| 45 | * @var string |
| 46 | */ |
| 47 | public const CSS = 'css'; |
| 48 | |
| 49 | /** |
| 50 | * Short description of attribute JS |
| 51 | * |
| 52 | * @access public |
| 53 | * @var string |
| 54 | */ |
| 55 | public const JS = 'js'; |
| 56 | |
| 57 | /** |
| 58 | * Short description of attribute jsFiles |
| 59 | * |
| 60 | * @access private |
| 61 | * @var array |
| 62 | */ |
| 63 | private static $jsFiles = []; |
| 64 | |
| 65 | /** |
| 66 | * Short description of attribute cssFiles |
| 67 | * |
| 68 | * @access private |
| 69 | * @var array |
| 70 | */ |
| 71 | private static $cssFiles = []; |
| 72 | |
| 73 | /** |
| 74 | * Short description of attribute jsVars |
| 75 | * |
| 76 | * @access protected |
| 77 | * @var array |
| 78 | */ |
| 79 | protected static $jsVars = []; |
| 80 | |
| 81 | // --- OPERATIONS --- |
| 82 | |
| 83 | /** |
| 84 | * Short description of method contextInit |
| 85 | * |
| 86 | * @access public |
| 87 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 88 | * @param string extension |
| 89 | * @param string module |
| 90 | * @param string action |
| 91 | * @return mixed |
| 92 | */ |
| 93 | public static function contextInit($extension, $module, $action) |
| 94 | { |
| 95 | |
| 96 | |
| 97 | $basePath = '/' . $extension . '/views/'; |
| 98 | |
| 99 | //load module scripts |
| 100 | $jsModuleFile = $basePath . self::JS . '/controllers/' . strtolower($module) . '/' . $action . '.' . self::JS; |
| 101 | |
| 102 | $cssModuleFile = $basePath . self::CSS . '/' . $module . '.' . self::CSS; |
| 103 | $cssModuleDir = $basePath . self::CSS . '/' . $module . '/'; |
| 104 | |
| 105 | if (file_exists($jsModuleFile)) { |
| 106 | self::addJsFile($jsModuleFile); |
| 107 | } |
| 108 | if (file_exists($cssModuleFile)) { |
| 109 | self::addCssFile($cssModuleFile); |
| 110 | } |
| 111 | foreach (glob($cssModuleDir . '*.' . self::CSS) as $file) { |
| 112 | self::addCssFile($file); |
| 113 | } |
| 114 | |
| 115 | // |
| 116 | //@todo load action scripts |
| 117 | // |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * define the paths to look for the scripts |
| 122 | * |
| 123 | * @access public |
| 124 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 125 | * @param array paths |
| 126 | * @param boolean recursive |
| 127 | * @param string filter |
| 128 | * @return mixed |
| 129 | */ |
| 130 | public static function setPaths($paths, $recursive = false, $filter = '') |
| 131 | { |
| 132 | |
| 133 | foreach ($paths as $path) { |
| 134 | if (!preg_match("/\/$/", $path)) { |
| 135 | $path .= '/'; |
| 136 | } |
| 137 | if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::CSS) { |
| 138 | foreach (glob($path . "*." . tao_helpers_Scriptloader::CSS) as $cssFile) { |
| 139 | self::$cssFiles[] = $path . $cssFile; |
| 140 | } |
| 141 | } |
| 142 | if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::JS) { |
| 143 | foreach (glob($path . "*." . tao_helpers_Scriptloader::JS) as $jsFile) { |
| 144 | self::$jsFiles[] = $path . $jsFile; |
| 145 | } |
| 146 | } |
| 147 | if ($recursive) { |
| 148 | $dirs = []; |
| 149 | foreach (scandir($path) as $file) { |
| 150 | if (is_dir($path . $file) && $file != '.' && $file != '..') { |
| 151 | $dirs[] = $path . $file; |
| 152 | } |
| 153 | } |
| 154 | if (count($dirs) > 0) { |
| 155 | self::setPaths($dirs, true, $filter); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * add a file to load |
| 163 | * |
| 164 | * @access public |
| 165 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 166 | * @param string file |
| 167 | * @param string type |
| 168 | * @return mixed |
| 169 | * @throws Exception |
| 170 | */ |
| 171 | public static function addFile($file, $type = '') |
| 172 | { |
| 173 | |
| 174 | if (empty($type)) { |
| 175 | if (preg_match("/\." . tao_helpers_Scriptloader::CSS . "$/", $file)) { |
| 176 | $type = tao_helpers_Scriptloader::CSS; |
| 177 | } |
| 178 | if (preg_match("/\." . tao_helpers_Scriptloader::JS . "$/", $file)) { |
| 179 | $type = tao_helpers_Scriptloader::JS; |
| 180 | } |
| 181 | } |
| 182 | switch (strtolower($type)) { |
| 183 | case tao_helpers_Scriptloader::CSS: |
| 184 | self::$cssFiles[] = $file; |
| 185 | break; |
| 186 | case tao_helpers_Scriptloader::JS: |
| 187 | self::$jsFiles[] = $file; |
| 188 | break; |
| 189 | default: |
| 190 | throw new Exception("Unknown script type for file : " . $file); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * add a css file to load |
| 196 | * |
| 197 | * @access public |
| 198 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 199 | * @param string file |
| 200 | * @return mixed |
| 201 | */ |
| 202 | public static function addCssFile($file) |
| 203 | { |
| 204 | |
| 205 | self::addFile($file, tao_helpers_Scriptloader::CSS); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * add a js file to load |
| 210 | * |
| 211 | * @access public |
| 212 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 213 | * @param string file |
| 214 | * @return mixed |
| 215 | */ |
| 216 | public static function addJsFile($file) |
| 217 | { |
| 218 | |
| 219 | self::addFile($file, tao_helpers_Scriptloader::JS); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * add an array of css files to load |
| 224 | * |
| 225 | * @access public |
| 226 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 227 | * @param array files |
| 228 | * @return mixed |
| 229 | */ |
| 230 | public static function addCssFiles($files = []) |
| 231 | { |
| 232 | |
| 233 | foreach ($files as $file) { |
| 234 | self::addFile($file, tao_helpers_Scriptloader::CSS); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * add an array of css files to load |
| 240 | * |
| 241 | * @access public |
| 242 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 243 | * @param array files |
| 244 | * @return mixed |
| 245 | */ |
| 246 | public static function addJsFiles($files = []) |
| 247 | { |
| 248 | |
| 249 | foreach ($files as $file) { |
| 250 | self::addFile($file, tao_helpers_Scriptloader::JS); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Short description of method addJsVar |
| 256 | * |
| 257 | * @access public |
| 258 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 259 | * @param string name |
| 260 | * @param string value |
| 261 | * @return mixed |
| 262 | */ |
| 263 | public static function addJsVar($name, $value = '') |
| 264 | { |
| 265 | |
| 266 | |
| 267 | self::$jsVars[$name] = $value; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Short description of method addJsVars |
| 272 | * |
| 273 | * @access public |
| 274 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 275 | * @param array vars |
| 276 | * @return mixed |
| 277 | */ |
| 278 | public static function addJsVars($vars) |
| 279 | { |
| 280 | |
| 281 | |
| 282 | if (is_array($vars)) { |
| 283 | foreach ($vars as $name => $value) { |
| 284 | if (is_int($name)) { |
| 285 | $name = 'var_' . $name; |
| 286 | } |
| 287 | self::addJsVar($name, $value); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | public static function getJsFiles() |
| 293 | { |
| 294 | return self::$jsFiles; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * render the html to load the resources |
| 299 | * |
| 300 | * @access public |
| 301 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 302 | * @param string filter |
| 303 | * @return string |
| 304 | */ |
| 305 | public static function render($filter = '') |
| 306 | { |
| 307 | $returnValue = (string) ''; |
| 308 | |
| 309 | |
| 310 | if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::CSS) { |
| 311 | foreach (self::$cssFiles as $file) { |
| 312 | $returnValue .= "\t<link rel='stylesheet' type='text/css' href='{$file}' />\n"; |
| 313 | } |
| 314 | } |
| 315 | if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::JS) { |
| 316 | if (count(self::$jsVars) > 0) { |
| 317 | $returnValue .= "\t<script type='text/javascript'>\n"; |
| 318 | foreach (self::$jsVars as $name => $value) { |
| 319 | $returnValue .= "\tvar {$name} = '{$value}';\n"; |
| 320 | } |
| 321 | $returnValue .= "\t</script>\n"; |
| 322 | } |
| 323 | foreach (self::$jsFiles as $file) { |
| 324 | $returnValue .= "\t<script type='text/javascript' src='{$file}' ></script>\n"; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | |
| 329 | return (string) $returnValue; |
| 330 | } |
| 331 | } |