* @package tao */ class tao_helpers_Scriptloader { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Short description of attribute CSS * * @access public * @var string */ const CSS = 'css'; /** * Short description of attribute JS * * @access public * @var string */ const JS = 'js'; /** * Short description of attribute jsFiles * * @access private * @var array */ private static $jsFiles = []; /** * Short description of attribute cssFiles * * @access private * @var array */ private static $cssFiles = []; /** * Short description of attribute jsVars * * @access protected * @var array */ protected static $jsVars = []; // --- OPERATIONS --- /** * Short description of method contextInit * * @access public * @author Bertrand Chevrier, * @param string extension * @param string module * @param string action * @return mixed */ public static function contextInit($extension, $module, $action) { $basePath = '/' . $extension . '/views/'; //load module scripts $jsModuleFile = $basePath . self::JS . '/controllers/' . strtolower($module) . '/' . $action . '.' . self::JS; $cssModuleFile = $basePath . self::CSS . '/' . $module . '.' . self::CSS; $cssModuleDir = $basePath . self::CSS . '/' . $module . '/'; if (file_exists($jsModuleFile)) { self::addJsFile($jsModuleFile); } if (file_exists($cssModuleFile)) { self::addCssFile($cssModuleFile); } foreach (glob($cssModuleDir . '*.' . self::CSS) as $file) { self::addCssFile($file); } // //@todo load action scripts // } /** * define the paths to look for the scripts * * @access public * @author Bertrand Chevrier, * @param array paths * @param boolean recursive * @param string filter * @return mixed */ public static function setPaths($paths, $recursive = false, $filter = '') { foreach ($paths as $path) { if (!preg_match("/\/$/", $path)) { $path .= '/'; } if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::CSS) { foreach (glob($path . "*." . tao_helpers_Scriptloader::CSS) as $cssFile) { self::$cssFiles[] = $path . $cssFile; } } if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::JS) { foreach (glob($path . "*." . tao_helpers_Scriptloader::JS) as $jsFile) { self::$jsFiles[] = $path . $jsFile; } } if ($recursive) { $dirs = []; foreach (scandir($path) as $file) { if (is_dir($path . $file) && $file != '.' && $file != '..') { $dirs[] = $path . $file; } } if (count($dirs) > 0) { self::setPaths($dirs, true, $filter); } } } } /** * add a file to load * * @access public * @author Bertrand Chevrier, * @param string file * @param string type * @return mixed * @throws Exception */ public static function addFile($file, $type = '') { if (empty($type)) { if (preg_match("/\." . tao_helpers_Scriptloader::CSS . "$/", $file)) { $type = tao_helpers_Scriptloader::CSS; } if (preg_match("/\." . tao_helpers_Scriptloader::JS . "$/", $file)) { $type = tao_helpers_Scriptloader::JS; } } switch (strtolower($type)) { case tao_helpers_Scriptloader::CSS: self::$cssFiles[] = $file; break; case tao_helpers_Scriptloader::JS: self::$jsFiles[] = $file; break; default: throw new Exception("Unknown script type for file : " . $file); } } /** * add a css file to load * * @access public * @author Bertrand Chevrier, * @param string file * @return mixed */ public static function addCssFile($file) { self::addFile($file, tao_helpers_Scriptloader::CSS); } /** * add a js file to load * * @access public * @author Bertrand Chevrier, * @param string file * @return mixed */ public static function addJsFile($file) { self::addFile($file, tao_helpers_Scriptloader::JS); } /** * add an array of css files to load * * @access public * @author Bertrand Chevrier, * @param array files * @return mixed */ public static function addCssFiles($files = []) { foreach ($files as $file) { self::addFile($file, tao_helpers_Scriptloader::CSS); } } /** * add an array of css files to load * * @access public * @author Bertrand Chevrier, * @param array files * @return mixed */ public static function addJsFiles($files = []) { foreach ($files as $file) { self::addFile($file, tao_helpers_Scriptloader::JS); } } /** * Short description of method addJsVar * * @access public * @author Bertrand Chevrier, * @param string name * @param string value * @return mixed */ public static function addJsVar($name, $value = '') { self::$jsVars[$name] = $value; } /** * Short description of method addJsVars * * @access public * @author Bertrand Chevrier, * @param array vars * @return mixed */ public static function addJsVars($vars) { if (is_array($vars)) { foreach ($vars as $name => $value) { if (is_int($name)) { $name = 'var_' . $name; } self::addJsVar($name, $value); } } } public static function getJsFiles() { return self::$jsFiles; } /** * render the html to load the resources * * @access public * @author Bertrand Chevrier, * @param string filter * @return string */ public static function render($filter = '') { $returnValue = (string) ''; if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::CSS) { foreach (self::$cssFiles as $file) { $returnValue .= "\t\n"; } } if (empty($filter) || strtolower($filter) == tao_helpers_Scriptloader::JS) { if (count(self::$jsVars) > 0) { $returnValue .= "\t\n"; } foreach (self::$jsFiles as $file) { $returnValue .= "\t\n"; } } return (string) $returnValue; } }