javascript($file); } /** * Add the javascript code to the header of the page * * See {@link Requirements_Backend::customScript()} for more info * @param script The script content * @param uniquenessID Use this to ensure that pieces of code only get added once. */ static function customScript($script, $uniquenessID = null) { self::backend()->customScript($script, $uniquenessID); } /** * Include custom CSS styling to the header of the page. * * See {@link Requirements_Backend::customCSS()} * * @param string $script CSS selectors as a string (without \n"; } foreach(array_diff_key($this->customHeadTags,$this->blocked) as $customHeadTag) { $requirements .= "$customHeadTag\n"; } if($this->write_js_to_body) { // Remove all newlines from code to preserve layout $jsRequirements = preg_replace('/>\n*/', '>', $jsRequirements); // We put script tags into the body, for performance. // If your template already has script tags in the body, then we put our script tags at the top of the body. // Otherwise, we put it at the bottom. $p1 = strripos($content, ' $p2) { user_error("You have a script tag in the body, moving requirements to top of for compatibilty. I recommend removing the script tag from your template's body.", E_USER_NOTICE); $content = eregi_replace("(]*>)", "\\1" . $jsRequirements, $content); } else { $content = eregi_replace("(]*>)", $jsRequirements . "\\1", $content); } // Put CSS at the bottom of the head $content = eregi_replace("(]*>)", $requirements . "\\1", $content); } else { $content = eregi_replace("(]*>)", $requirements . "\\1", $content); $content = eregi_replace("(]*>)", $jsRequirements . "\\1", $content); } } if(isset($_GET['debug_profile'])) Profiler::unmark("Requirements::includeInHTML"); return $content; } /** * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the HTTP response */ function include_in_response(HTTPResponse $response) { $this->process_combined_files(); $jsRequirements = array(); $cssRequirements = array(); foreach(array_diff_key($this->javascript, $this->blocked) as $file => $dummy) { $path = $this->path_for_file($file); if($path) $jsRequirements[] = $path; } $response->addHeader('X-Include-JS', implode(',', $jsRequirements)); foreach(array_diff_key($this->css,$this->blocked) as $file => $params) { $path = $this->path_for_file($file); if($path) $cssRequirements[] = isset($params['media']) ? "$path:##:$params[media]" : $path; } $response->addHeader('X-Include-CSS', implode(',', $cssRequirements)); } /** * Automatically includes the necessary lang-files from the module * according to the locale set in {@link i18n::$current_locale}. * Assumes that a subfolder /javascript exists relative to the included * javascript file, with a file named after the locale - * so usually /javascript/lang/en_US.js. */ protected function process_i18n_javascript() { // ensure to include the i18n base library if( count(array_diff_key($this->javascript,$this->blocked)) && !isset($this->javascript[SAPPHIRE_DIR . '/javascript/i18n.js']) ) { $this->javascript[THIRDPARTY_DIR . '/prototype.js'] = true; $this->javascript[SAPPHIRE_DIR . '/javascript/i18n.js'] = true; } // include the specific locale and the master locale for each module foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) { if(preg_match('/^http[s]?/', $file)) continue; $absolutePath = Director::baseFolder() . '/' . $file; $absoluteLangPath = dirname($absolutePath) . '/lang/' . i18n::get_locale() . '.js'; $absoluteDefaultLangPath = dirname($absolutePath) . '/lang/' . i18n::default_locale() . '.js'; foreach(array($absoluteDefaultLangPath, $absoluteLangPath) as $path) { if(Director::fileExists($path)) { $langFile = Director::makeRelative($path); // Remove rogue leading slashes from Director::makeRelative() $langFile = preg_replace('/^\//', '', $langFile); $this->javascript[$langFile] = true; } } } } /** * Finds the path for specified file. * * @param string $fileOrUrl * @return string|boolean */ protected static function path_for_file($fileOrUrl) { if(preg_match('/^http[s]?/', $fileOrUrl)) { return $fileOrUrl; } elseif(Director::fileExists($fileOrUrl)) { $prefix = Director::absoluteBaseURL(); $mtimesuffix = "?m=" . filemtime(Director::baseFolder() . '/' . $fileOrUrl); return "{$prefix}{$fileOrUrl}{$mtimesuffix}"; } else { return false; } } /** * Concatenate several css or javascript files into a single dynamically generated * file (stored in {@link Director::baseFolder()}). This increases performance * by fewer HTTP requests. * * The combined file is regenerated * based on every file modification time. Optionally a rebuild can be triggered * by appending ?flush=1 to the URL. * If all files to be combined are javascript, we use the external JSMin library * to minify the javascript. This can be controlled by {@link $combine_js_with_jsmin}. * * All combined files will have a comment on the start of each concatenated file * denoting their original position. For easier debugging, we recommend to only * minify javascript if not in development mode ({@link Director::isDev()}). * * CAUTION: You're responsible for ensuring that the load order for combined files * is retained - otherwise combining javascript files can lead to functional errors * in the javascript logic, and combining css can lead to wrong styling inheritance. * Depending on the javascript logic, you also have to ensure that files are not included * in more than one combine_files() call. * Best practice is to include every javascript file in exactly *one* combine_files() * directive to avoid the issues mentioned above - this is enforced by this function. * * CAUTION: Combining CSS Files discards any "media" information. * * Example for combined JavaScript: * * Requirements::combine_files( * 'foobar.js', * array( * 'mysite/javascript/foo.js', * 'mysite/javascript/bar.js', * ) * ); * * * Example for combined CSS: * * Requirements::combine_files( * 'foobar.css', * array( * 'mysite/javascript/foo.css', * 'mysite/javascript/bar.css', * ) * ); * * * @see http://code.google.com/p/jsmin-php/ * * @todo Should we enforce unique inclusion of files, or leave it to the developer? Can auto-detection cause breaks? * * @param string $combinedFileName Filename of the combined file (will be stored in {@link Director::baseFolder()} by default) * @param array $files Array of filenames relative to the webroot */ function combine_files($combinedFileName, $files) { // duplicate check foreach($this->combine_files as $_combinedFileName => $_files) { $duplicates = array_intersect($_files, $files); if($duplicates) { user_error("Requirements::combine_files(): Already included files " . implode(',', $duplicates) . " in combined file '{$_combinedFileName}'", E_USER_NOTICE); return false; } } $this->combine_files[$combinedFileName] = $files; } /** * Returns all combined files. * @return array */ function get_combine_files() { return $this->combine_files; } /** * Deletes all dynamically generated combined files from the filesystem. * * @param string $combinedFileName If left blank, all combined files are deleted. */ function delete_combined_files($combinedFileName = null) { $combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files; foreach($combinedFiles as $combinedFile => $sourceItems) { $filePath = Director::baseFolder() . '/' . $combinedFile; if(file_exists($filePath)) { unlink($filePath); } } } function clear_combined_files() { $this->combine_files = array(); } /** * See {@link combine_files()} * */ function process_combined_files() { if((Director::isDev() && !SapphireTest::is_running_test()) || !Requirements::get_combined_files_enabled()) { return; } // Make a map of files that could be potentially combined $combinerCheck = array(); foreach($this->combine_files as $combinedFile => $sourceItems) { foreach($sourceItems as $sourceItem) { if(isset($combinerCheck[$sourceItem]) && $combinerCheck[$sourceItem] != $combinedFile){ user_error("Requirements::process_combined_files - file '$sourceItem' appears in two combined files:" . " '{$combinerCheck[$sourceItem]}' and '$combinedFile'", E_USER_WARNING); } $combinerCheck[$sourceItem] = $combinedFile; } } // Figure out which ones apply to this pageview $combinedFiles = array(); $newJSRequirements = array(); $newCSSRequirements = array(); foreach($this->javascript as $file => $dummy) { if(isset($combinerCheck[$file])) { $newJSRequirements[$combinerCheck[$file]] = true; $combinedFiles[$combinerCheck[$file]] = true; } else { $newJSRequirements[$file] = true; } } foreach($this->css as $file => $params) { if(isset($combinerCheck[$file])) { $newCSSRequirements[$combinerCheck[$file]] = true; $combinedFiles[$combinerCheck[$file]] = true; } else { $newCSSRequirements[$file] = $params; } } // Process the combined files $base = Director::baseFolder() . '/'; foreach(array_diff_key($combinedFiles,$this->blocked) as $combinedFile => $dummy) { $fileList = $this->combine_files[$combinedFile]; // Determine if we need to build the combined include if(file_exists($base . $combinedFile) && !isset($_GET['flush'])) { // file exists, check modification date of every contained file $srcLastMod = 0; foreach($fileList as $file) { $srcLastMod = max(filemtime($base . $file), $srcLastMod); } $refresh = $srcLastMod > filemtime($base . $combinedFile); } else { // file doesn't exist, or refresh was explicitly required $refresh = true; } if(!$refresh) continue; $combinedData = ""; foreach(array_diff($fileList,$this->blocked) as $file) { $fileContent = file_get_contents($base . $file); // if we have a javascript file and jsmin is enabled, minify the content if(stripos($file, '.js') && $this->combine_js_with_jsmin) { require_once('thirdparty/jsmin/JSMin.php'); set_time_limit(0); $fileContent = JSMin::minify($fileContent); } // write a header comment for each file for easier identification and debugging $combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n;\n"; } if(!file_exists(dirname($base . $combinedFile))) { Filesystem::makeFolder(dirname($base . $combinedFile)); } $successfulWrite = false; $fh = fopen($base . $combinedFile, 'w'); if($fh) { if(fwrite($fh, $combinedData) == strlen($combinedData)) $successfulWrite = true; fclose($fh); unset($fh); } // Unsuccessful write - just include the regular JS files, rather than the combined one if(!$successfulWrite) { user_error("Requirements_Backend::process_combined_files(): Couldn't create '$base$combinedFile'", E_USER_WARNING); return; } } // @todo Alters the original information, which means you can't call this // method repeatedly - it will behave different on the second call! $this->javascript = $newJSRequirements; $this->css = $newCSSRequirements; } function get_custom_scripts() { $requirements = ""; if($this->customScript) { foreach($this->customScript as $script) { $requirements .= "$script\n"; } } return $requirements; } /** * Register the given "themeable stylesheet" as required. * Themeable stylesheets have globally unique names, just like templates and PHP files. * Because of this, they can be replaced by similarly named CSS files in the theme directory. * * @param $name String The identifier of the file. For example, css/MyFile.css would have the identifier "MyFile" * @param $media String Comma-separated list of media-types (e.g. "screen,projector") */ function themedCSS($name, $media = null) { global $_CSS_MANIFEST; $theme = SSViewer::current_theme(); if($theme && isset($_CSS_MANIFEST[$name]) && isset($_CSS_MANIFEST[$name]['themes']) && isset($_CSS_MANIFEST[$name]['themes'][$theme])) Requirements::css($_CSS_MANIFEST[$name]['themes'][$theme], $media); else if(isset($_CSS_MANIFEST[$name]) && isset($_CSS_MANIFEST[$name]['unthemed'])) $this->css($_CSS_MANIFEST[$name]['unthemed'], $media); // Normal requirements fails quietly when there is no css - we should do the same // else user_error("themedCSS - No CSS file '$name.css' found.", E_USER_WARNING); } function debug() { Debug::show($this->javascript); Debug::show($this->css); Debug::show($this->customCSS); Debug::show($this->customScript); Debug::show($this->customHeadTags); Debug::show($this->combine_files); } } ?>