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
@ -25,6 +25,14 @@ class Requirements {
|
||||
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 /
|
||||
* location on to the requirements
|
||||
@ -386,6 +394,8 @@ class Requirements_Backend {
|
||||
*/
|
||||
public $combine_js_with_jsmin = true;
|
||||
|
||||
protected $combinedFilesFolder;
|
||||
|
||||
/**
|
||||
* Put all javascript includes at the bottom of the template
|
||||
* before the closing <body> tag instead of the <head> tag.
|
||||
@ -409,6 +419,10 @@ class Requirements_Backend {
|
||||
return $this->combined_files_enabled;
|
||||
}
|
||||
|
||||
function setCombinedFilesFolder($folder) {
|
||||
$this->combinedFilesFolder = $folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether we want to suffix requirements with the time /
|
||||
* location on to the requirements
|
||||
@ -830,8 +844,9 @@ class Requirements_Backend {
|
||||
*/
|
||||
function delete_combined_files($combinedFileName = null) {
|
||||
$combinedFiles = ($combinedFileName) ? array($combinedFileName => null) : $this->combine_files;
|
||||
$combinedFolder = ($this->combinedFilesFolder) ? (Director::baseFolder() . '/' . $this->combinedFilesFolder) : Director::baseFolder();
|
||||
foreach($combinedFiles as $combinedFile => $sourceItems) {
|
||||
$filePath = Director::baseFolder() . '/' . $combinedFile;
|
||||
$filePath = $combinedFolder . '/' . $combinedFile;
|
||||
if(file_exists($filePath)) {
|
||||
unlink($filePath);
|
||||
}
|
||||
@ -842,7 +857,6 @@ class Requirements_Backend {
|
||||
$this->combine_files = array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* See {@link combine_files()}
|
||||
*
|
||||
@ -869,13 +883,16 @@ 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
|
||||
$combinedFiles = array();
|
||||
$newJSRequirements = array();
|
||||
$newCSSRequirements = array();
|
||||
foreach($this->javascript as $file => $dummy) {
|
||||
if(isset($combinerCheck[$file])) {
|
||||
$newJSRequirements[$combinerCheck[$file]] = true;
|
||||
$newJSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
|
||||
$combinedFiles[$combinerCheck[$file]] = true;
|
||||
} else {
|
||||
$newJSRequirements[$file] = true;
|
||||
@ -884,7 +901,7 @@ class Requirements_Backend {
|
||||
|
||||
foreach($this->css as $file => $params) {
|
||||
if(isset($combinerCheck[$file])) {
|
||||
$newCSSRequirements[$combinerCheck[$file]] = true;
|
||||
$newCSSRequirements[$combinedFilesFolder . $combinerCheck[$file]] = true;
|
||||
$combinedFiles[$combinerCheck[$file]] = true;
|
||||
} else {
|
||||
$newCSSRequirements[$file] = $params;
|
||||
@ -926,12 +943,14 @@ class Requirements_Backend {
|
||||
// also the semicolon between each file is required for jQuery to be combinable properly
|
||||
$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;
|
||||
$fh = fopen($base . $combinedFile, 'w');
|
||||
$fh = fopen($combinedFilePath, 'wb');
|
||||
if($fh) {
|
||||
if(fwrite($fh, $combinedData) == strlen($combinedData)) $successfulWrite = true;
|
||||
fclose($fh);
|
||||
@ -940,7 +959,7 @@ class Requirements_Backend {
|
||||
|
||||
// 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);
|
||||
user_error("Requirements_Backend::process_combined_files(): Couldn't create '$combinedFilePath'", E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ class RequirementsTest extends SapphireTest {
|
||||
|
||||
protected function setupCombinedRequirements($backend) {
|
||||
$backend->clear();
|
||||
$backend->setCombinedFilesFolder('assets');
|
||||
|
||||
// clearing all previously generated requirements (just in case)
|
||||
$backend->clear_combined_files();
|
||||
@ -64,10 +65,11 @@ class RequirementsTest extends SapphireTest {
|
||||
function testCombinedJavascript() {
|
||||
$backend = new Requirements_Backend;
|
||||
$backend->set_combined_files_enabled(true);
|
||||
$backend->setCombinedFilesFolder('assets');
|
||||
|
||||
$this->setupCombinedRequirements($backend);
|
||||
|
||||
$combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js';
|
||||
$combinedFilePath = Director::baseFolder() . '/assets/' . 'RequirementsTest_bc.js';
|
||||
|
||||
$html = $backend->includeInHTML(false, self::$html_template);
|
||||
|
||||
@ -94,8 +96,8 @@ class RequirementsTest extends SapphireTest {
|
||||
function testBlockedCombinedJavascript() {
|
||||
$backend = new Requirements_Backend;
|
||||
$backend->set_combined_files_enabled(true);
|
||||
|
||||
$combinedFilePath = Director::baseFolder() . '/' . 'RequirementsTest_bc.js';
|
||||
$backend->setCombinedFilesFolder('assets');
|
||||
$combinedFilePath = Director::baseFolder() . '/assets/' . 'RequirementsTest_bc.js';
|
||||
|
||||
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
|
||||
$this->setupCombinedRequirements($backend);
|
||||
|
Loading…
Reference in New Issue
Block a user