mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT #3387 Requirements now has a new static function called Requirements::set_combined_files_folder() for setting where the combined files should belong (from r100528)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@105596 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
b9e75f9cef
commit
9a438d26b0
@ -24,7 +24,15 @@ class Requirements {
|
|||||||
public static function get_combined_files_enabled() {
|
public static function get_combined_files_enabled() {
|
||||||
return self::backend()->get_combined_files_enabled();
|
return self::backend()->get_combined_files_enabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 we want to suffix requirements with the time /
|
* Set whether we want to suffix requirements with the time /
|
||||||
* location on to the requirements
|
* location on to the requirements
|
||||||
@ -386,6 +394,8 @@ class Requirements_Backend {
|
|||||||
*/
|
*/
|
||||||
public $combine_js_with_jsmin = true;
|
public $combine_js_with_jsmin = true;
|
||||||
|
|
||||||
|
protected $combinedFilesFolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put all javascript includes at the bottom of the template
|
* Put all javascript includes at the bottom of the template
|
||||||
* before the closing <body> tag instead of the <head> tag.
|
* before the closing <body> tag instead of the <head> tag.
|
||||||
@ -402,11 +412,15 @@ class Requirements_Backend {
|
|||||||
public $write_js_to_body = true;
|
public $write_js_to_body = true;
|
||||||
|
|
||||||
function set_combined_files_enabled($enable) {
|
function set_combined_files_enabled($enable) {
|
||||||
$this->combined_files_enabled = (bool) $enable;
|
$this->combined_files_enabled = (bool) $enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_combined_files_enabled() {
|
function get_combined_files_enabled() {
|
||||||
return $this->combined_files_enabled;
|
return $this->combined_files_enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setCombinedFilesFolder($folder) {
|
||||||
|
$this->combinedFilesFolder = $folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -615,7 +629,7 @@ class Requirements_Backend {
|
|||||||
// Combine files - updates $this->javascript and $this->css
|
// Combine files - updates $this->javascript and $this->css
|
||||||
$this->process_combined_files();
|
$this->process_combined_files();
|
||||||
|
|
||||||
foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
|
foreach(array_diff_key($this->javascript,$this->blocked) as $file => $dummy) {
|
||||||
$path = $this->path_for_file($file);
|
$path = $this->path_for_file($file);
|
||||||
if($path) {
|
if($path) {
|
||||||
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
|
$jsRequirements .= "<script type=\"text/javascript\" src=\"$path\"></script>\n";
|
||||||
@ -830,8 +844,9 @@ class Requirements_Backend {
|
|||||||
*/
|
*/
|
||||||
function delete_combined_files($combinedFileName = null) {
|
function delete_combined_files($combinedFileName = null) {
|
||||||
$combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files;
|
$combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files;
|
||||||
|
$combinedFolder = ($this->combinedFilesFolder) ? (Director::baseFolder() . '/' . $this->combinedFilesFolder) : Director::baseFolder();
|
||||||
foreach($combinedFiles as $combinedFile => $sourceItems) {
|
foreach($combinedFiles as $combinedFile => $sourceItems) {
|
||||||
$filePath = Director::baseFolder() . '/' . $combinedFile;
|
$filePath = $combinedFolder . '/' . $combinedFile;
|
||||||
if(file_exists($filePath)) {
|
if(file_exists($filePath)) {
|
||||||
unlink($filePath);
|
unlink($filePath);
|
||||||
}
|
}
|
||||||
@ -841,7 +856,6 @@ class Requirements_Backend {
|
|||||||
function clear_combined_files() {
|
function clear_combined_files() {
|
||||||
$this->combine_files = array();
|
$this->combine_files = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See {@link combine_files()}
|
* See {@link combine_files()}
|
||||||
@ -868,14 +882,17 @@ class Requirements_Backend {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Work out the relative URL for the combined files from the base folder
|
||||||
|
$combinedFilesFolder = ($this->combinedFilesFolder) ? ($this->combinedFilesFolder . '/') : '';
|
||||||
|
|
||||||
// Figure out which ones apply to this pageview
|
// Figure out which ones apply to this pageview
|
||||||
$combinedFiles = array();
|
$combinedFiles = array();
|
||||||
$newJSRequirements = array();
|
$newJSRequirements = array();
|
||||||
$newCSSRequirements = array();
|
$newCSSRequirements = array();
|
||||||
foreach($this->javascript as $file => $dummy) {
|
foreach($this->javascript as $file => $dummy) {
|
||||||
if(isset($combinerCheck[$file])) {
|
if(isset($combinerCheck[$file])) {
|
||||||
$newJSRequirements[$combinerCheck[$file]] = true;
|
$newJSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
|
||||||
$combinedFiles[$combinerCheck[$file]] = true;
|
$combinedFiles[$combinerCheck[$file]] = true;
|
||||||
} else {
|
} else {
|
||||||
$newJSRequirements[$file] = true;
|
$newJSRequirements[$file] = true;
|
||||||
@ -884,7 +901,7 @@ class Requirements_Backend {
|
|||||||
|
|
||||||
foreach($this->css as $file => $params) {
|
foreach($this->css as $file => $params) {
|
||||||
if(isset($combinerCheck[$file])) {
|
if(isset($combinerCheck[$file])) {
|
||||||
$newCSSRequirements[$combinerCheck[$file]] = true;
|
$newCSSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
|
||||||
$combinedFiles[$combinerCheck[$file]] = true;
|
$combinedFiles[$combinerCheck[$file]] = true;
|
||||||
} else {
|
} else {
|
||||||
$newCSSRequirements[$file] = $params;
|
$newCSSRequirements[$file] = $params;
|
||||||
@ -893,7 +910,7 @@ class Requirements_Backend {
|
|||||||
|
|
||||||
// Process the combined files
|
// Process the combined files
|
||||||
$base = Director::baseFolder() . '/';
|
$base = Director::baseFolder() . '/';
|
||||||
foreach(array_diff_key($combinedFiles,$this->blocked) as $combinedFile => $dummy) {
|
foreach(array_diff_key($combinedFiles, $this->blocked) as $combinedFile => $dummy) {
|
||||||
$fileList = $this->combine_files[$combinedFile];
|
$fileList = $this->combine_files[$combinedFile];
|
||||||
|
|
||||||
// Determine if we need to build the combined include
|
// Determine if we need to build the combined include
|
||||||
@ -926,12 +943,14 @@ class Requirements_Backend {
|
|||||||
// also the semicolon between each file is required for jQuery to be combinable properly
|
// also the semicolon between each file is required for jQuery to be combinable properly
|
||||||
$combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n".($isJS ? ';' : '')."\n";
|
$combinedData .= "/****** FILE: $file *****/\n" . $fileContent . "\n".($isJS ? ';' : '')."\n";
|
||||||
}
|
}
|
||||||
if(!file_exists(dirname($base . $combinedFile))) {
|
|
||||||
Filesystem::makeFolder(dirname($base . $combinedFile));
|
$combinedFilePath = Director::baseFolder() . '/' . $this->combinedFilesFolder . '/' . $combinedFile;
|
||||||
|
if(!file_exists(dirname($combinedFilePath))) {
|
||||||
|
Filesystem::makeFolder(dirname($combinedFilePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
$successfulWrite = false;
|
$successfulWrite = false;
|
||||||
$fh = fopen($base . $combinedFile, 'w');
|
$fh = fopen($combinedFilePath, 'wb');
|
||||||
if($fh) {
|
if($fh) {
|
||||||
if(fwrite($fh, $combinedData) == strlen($combinedData)) $successfulWrite = true;
|
if(fwrite($fh, $combinedData) == strlen($combinedData)) $successfulWrite = true;
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
@ -940,7 +959,7 @@ class Requirements_Backend {
|
|||||||
|
|
||||||
// Unsuccessful write - just include the regular JS files, rather than the combined one
|
// Unsuccessful write - just include the regular JS files, rather than the combined one
|
||||||
if(!$successfulWrite) {
|
if(!$successfulWrite) {
|
||||||
user_error("Requirements_Backend::process_combined_files(): Couldn't create '$base$combinedFile'", E_USER_WARNING);
|
user_error("Requirements_Backend::process_combined_files(): Couldn't create '$combinedFilePath'", E_USER_WARNING);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,7 @@ class RequirementsTest extends SapphireTest {
|
|||||||
|
|
||||||
protected function setupCombinedRequirements($backend) {
|
protected function setupCombinedRequirements($backend) {
|
||||||
$backend->clear();
|
$backend->clear();
|
||||||
|
$backend->setCombinedFilesFolder('assets');
|
||||||
|
|
||||||
// clearing all previously generated requirements (just in case)
|
// clearing all previously generated requirements (just in case)
|
||||||
$backend->clear_combined_files();
|
$backend->clear_combined_files();
|
||||||
@ -64,11 +65,12 @@ class RequirementsTest extends SapphireTest {
|
|||||||
function testCombinedJavascript() {
|
function testCombinedJavascript() {
|
||||||
$backend = new Requirements_Backend;
|
$backend = new Requirements_Backend;
|
||||||
$backend->set_combined_files_enabled(true);
|
$backend->set_combined_files_enabled(true);
|
||||||
|
$backend->setCombinedFilesFolder('assets');
|
||||||
|
|
||||||
$this->setupCombinedRequirements($backend);
|
$this->setupCombinedRequirements($backend);
|
||||||
|
|
||||||
$combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js';
|
$combinedFilePath = Director::baseFolder() . '/assets/' . 'RequirementsTest_bc.js';
|
||||||
|
|
||||||
$html = $backend->includeInHTML(false, self::$html_template);
|
$html = $backend->includeInHTML(false, self::$html_template);
|
||||||
|
|
||||||
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
|
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
|
||||||
@ -94,8 +96,8 @@ class RequirementsTest extends SapphireTest {
|
|||||||
function testBlockedCombinedJavascript() {
|
function testBlockedCombinedJavascript() {
|
||||||
$backend = new Requirements_Backend;
|
$backend = new Requirements_Backend;
|
||||||
$backend->set_combined_files_enabled(true);
|
$backend->set_combined_files_enabled(true);
|
||||||
|
$backend->setCombinedFilesFolder('assets');
|
||||||
$combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js';
|
$combinedFilePath = Director::baseFolder() . '/assets/' . 'RequirementsTest_bc.js';
|
||||||
|
|
||||||
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
|
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
|
||||||
$this->setupCombinedRequirements($backend);
|
$this->setupCombinedRequirements($backend);
|
||||||
|
Loading…
Reference in New Issue
Block a user