get(static::class, 'disable_flush_combined'); if(!$disabled) { self::delete_all_combined_files(); } } /** * Enable combining of css/javascript files. * * @param bool $enable */ public static function set_combined_files_enabled($enable) { self::backend()->setCombinedFilesEnabled($enable); } /** * Checks whether combining of css/javascript files is enabled. * * @return bool */ public static function get_combined_files_enabled() { return self::backend()->getCombinedFilesEnabled(); } /** * Set the relative folder e.g. 'assets' for where to store combined files * * @param string $folder Path to folder */ public static function set_combined_files_folder($folder) { self::backend()->setCombinedFilesFolder($folder); } /** * Set whether to add caching query params to the requests for file-based requirements. * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, * while automatically busting this cache every time the file is changed. * * @param bool */ public static function set_suffix_requirements($var) { self::backend()->setSuffixRequirements($var); } /** * Check whether we want to suffix requirements * * @return bool */ public static function get_suffix_requirements() { return self::backend()->getSuffixRequirements(); } /** * Instance of the requirements for storage. You can create your own backend to change the * default JS and CSS inclusion behaviour. * * @var Requirements_Backend */ private static $backend = null; /** * @return Requirements_Backend */ public static function backend() { if(!self::$backend) { self::$backend = Injector::inst()->create('Requirements_Backend'); } return self::$backend; } /** * Setter method for changing the Requirements backend * * @param Requirements_Backend $backend */ public static function set_backend(Requirements_Backend $backend) { self::$backend = $backend; } /** * Register the given JavaScript file as required. * * @param string $file Relative to docroot * @param array $options List of options. Available options include: * - 'provides' : List of scripts files included in this file */ public static function javascript($file, $options = array()) { self::backend()->javascript($file, $options); } /** * Register the given JavaScript code into the list of requirements * * @param string $script The script content as a string (without enclosing "; } } // Add all inline JavaScript *after* including external files they might rely on foreach($this->getCustomScripts() as $script) { $jsRequirements .= ""; } foreach($this->getCSS() as $file => $params) { $path = Convert::raw2att($this->pathForFile($file)); if($path) { $media = (isset($params['media']) && !empty($params['media'])) ? " media=\"{$params['media']}\"" : ""; $requirements .= "\n"; } } foreach($this->getCustomCSS() as $css) { $requirements .= "\n"; } foreach($this->getCustomHeadTags() as $customHeadTag) { $requirements .= "$customHeadTag\n"; } // Inject CSS into body $content = $this->insertTagsIntoHead($requirements, $content); // Inject scripts if ($this->getForceJSToBottom()) { $content = $this->insertScriptsAtBottom($jsRequirements, $content); } elseif($this->getWriteJavascriptToBody()) { $content = $this->insertScriptsIntoBody($jsRequirements, $content); } else { $content = $this->insertTagsIntoHead($jsRequirements, $content); } return $content; } /** * Given a block of HTML, insert the given scripts at the bottom before * the closing tag * * @param string $jsRequirements String containing one or more javascript