2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-11-07 03:06:28 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\View;
|
|
|
|
|
2017-05-10 06:24:30 +02:00
|
|
|
use Exception;
|
2017-06-22 12:50:45 +02:00
|
|
|
use InvalidArgumentException;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Storage\GeneratedAssetHandler;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2017-06-27 04:30:48 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-10-03 01:20:49 +02:00
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader;
|
2017-06-27 04:30:48 +02:00
|
|
|
use SilverStripe\Core\Manifest\ResourceURLGenerator;
|
2018-01-12 04:25:02 +01:00
|
|
|
use SilverStripe\Core\Path;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\i18n\i18n;
|
2019-01-15 03:04:39 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2015-10-12 06:34:34 +02:00
|
|
|
|
2015-11-30 03:03:46 +01:00
|
|
|
class Requirements_Backend
|
|
|
|
{
|
2016-11-29 00:31:16 +01:00
|
|
|
use Injectable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $suffixRequirements = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether to combine CSS and JavaScript files
|
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @var bool|null
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
protected $combinedFilesEnabled = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if files should be combined automatically on dev mode.
|
|
|
|
*
|
|
|
|
* By default combined files will not be combined except in test or
|
|
|
|
* live environments. Turning this on will allow for pre-combining of files in development mode.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $combine_in_dev = false;
|
|
|
|
|
2021-11-09 02:15:12 +01:00
|
|
|
/**
|
|
|
|
* Determine if relative urls in the combined files should be converted to absolute.
|
|
|
|
*
|
|
|
|
* By default combined files will be parsed for relative urls to image/font assets and those
|
|
|
|
* utls will be changed to absolute to accomodate to the fact that the combined css is placed
|
|
|
|
* in a totally different folder than the source css files.
|
|
|
|
*
|
|
|
|
* Turn this off if you see some unexpected results.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $resolve_relative_css_refs = true;
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Paths to all required JavaScript files relative to docroot
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $javascript = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of included scripts to array of contained files.
|
|
|
|
* To be used alongside front-end combination mechanisms.
|
|
|
|
*
|
|
|
|
* @var array Map of providing filepath => array(provided filepaths)
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $providedJavascript = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Paths to all required CSS files relative to the docroot.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $css = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* All custom javascript code that is inserted into the page's HTML
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $customScript = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2019-06-15 03:07:49 +02:00
|
|
|
* All custom CSS rules which are inserted directly at the bottom of the HTML `<head>` tag
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $customCSS = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2019-06-15 03:07:49 +02:00
|
|
|
* All custom HTML markup which is added before the closing `<head>` tag, e.g. additional
|
2016-11-29 00:31:16 +01:00
|
|
|
* metatags.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $customHeadTags = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remembers the file paths or uniquenessIDs of all Requirements cleared through
|
|
|
|
* {@link clear()}, so that they can be restored later.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $disabled = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The file paths (relative to docroot) or uniquenessIDs of any included requirements which
|
|
|
|
* should be blocked when executing {@link inlcudeInHTML()}. This is useful, for example,
|
|
|
|
* to block scripts included by a superclass without having to override entire functions and
|
|
|
|
* duplicate a lot of code.
|
|
|
|
*
|
|
|
|
* Use {@link unblock()} or {@link unblock_all()} to revert changes.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $blocked = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of combined files registered via {@link combine_files()}. Keys are the output file
|
|
|
|
* names, values are lists of input files.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected $combinedFiles = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-05-10 06:24:30 +02:00
|
|
|
* Use the injected minification service to minify any javascript file passed to {@link combine_files()}.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2017-05-10 06:24:30 +02:00
|
|
|
protected $minifyCombinedFiles = false;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not file headers should be written when combining files
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $writeHeaderComment = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Where to save combined files. By default they're placed in assets/_combinedfiles, however
|
|
|
|
* this may be an issue depending on your setup, especially for CSS files which often contain
|
|
|
|
* relative paths.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $combinedFilesFolder = null;
|
|
|
|
|
|
|
|
/**
|
2019-06-15 03:07:49 +02:00
|
|
|
* Put all JavaScript includes at the bottom of the template before the closing `<body>` tag,
|
|
|
|
* rather than the default behaviour of placing them at the end of the `<head>` tag. This means
|
2016-11-29 00:31:16 +01:00
|
|
|
* script downloads won't block other HTTP requests, which can be a performance improvement.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $writeJavascriptToBody = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force the JavaScript to the bottom of the page, even if there's a script tag in the body already
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $forceJSToBottom = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures the default prefix for combined files.
|
|
|
|
*
|
|
|
|
* This defaults to `_combinedfiles`, and is the folder within the configured asset backend that
|
|
|
|
* combined files will be stored in. If using a backend shared with other systems, it is usually
|
|
|
|
* necessary to distinguish combined files from other assets.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_combined_files_folder = '_combinedfiles';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to include the hash in the querystring instead of the filename for combined files.
|
|
|
|
*
|
|
|
|
* By default the `<hash>` of the source files is appended to the end of the combined file
|
|
|
|
* (prior to the file extension). If combined files are versioned in source control or running
|
|
|
|
* in a distributed environment (such as one where the newest version of a file may not always be
|
|
|
|
* immediately available) then it may sometimes be necessary to disable this. When this is set to true,
|
|
|
|
* the hash will instead be appended via a querystring parameter to enable cache busting, but not in
|
|
|
|
* the filename itself. I.e. `assets/_combinedfiles/name.js?m=<hash>`
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $combine_hash_querystring = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GeneratedAssetHandler
|
|
|
|
*/
|
|
|
|
protected $assetHandler = null;
|
|
|
|
|
2017-05-10 06:24:30 +02:00
|
|
|
/**
|
|
|
|
* @var Requirements_Minifier
|
|
|
|
*/
|
|
|
|
protected $minifier = null;
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Gets the backend storage for generated files
|
|
|
|
*
|
|
|
|
* @return GeneratedAssetHandler
|
|
|
|
*/
|
|
|
|
public function getAssetHandler()
|
|
|
|
{
|
|
|
|
return $this->assetHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a new asset handler for this backend
|
|
|
|
*
|
|
|
|
* @param GeneratedAssetHandler $handler
|
|
|
|
*/
|
|
|
|
public function setAssetHandler(GeneratedAssetHandler $handler)
|
|
|
|
{
|
|
|
|
$this->assetHandler = $handler;
|
|
|
|
}
|
|
|
|
|
2017-05-10 06:24:30 +02:00
|
|
|
/**
|
|
|
|
* Gets the minification service for this backend
|
|
|
|
*
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0
|
2017-05-10 06:24:30 +02:00
|
|
|
* @return Requirements_Minifier
|
|
|
|
*/
|
|
|
|
public function getMinifier()
|
|
|
|
{
|
|
|
|
return $this->minifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a new minification service for this backend
|
|
|
|
*
|
|
|
|
* @param Requirements_Minifier $minifier
|
|
|
|
*/
|
|
|
|
public function setMinifier(Requirements_Minifier $minifier = null)
|
|
|
|
{
|
|
|
|
$this->minifier = $minifier;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Enable or disable the combination of CSS and JavaScript files
|
|
|
|
*
|
|
|
|
* @param bool $enable
|
|
|
|
*/
|
|
|
|
public function setCombinedFilesEnabled($enable)
|
|
|
|
{
|
|
|
|
$this->combinedFilesEnabled = (bool)$enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if header comments are written
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getWriteHeaderComment()
|
|
|
|
{
|
|
|
|
return $this->writeHeaderComment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag whether header comments should be written for each combined file
|
|
|
|
*
|
|
|
|
* @param bool $write
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setWriteHeaderComment($write)
|
|
|
|
{
|
|
|
|
$this->writeHeaderComment = $write;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the folder to save combined files in. By default they're placed in _combinedfiles,
|
|
|
|
* however this may be an issue depending on your setup, especially for CSS files which often
|
|
|
|
* contain relative paths.
|
|
|
|
*
|
|
|
|
* This must not include any 'assets' prefix
|
|
|
|
*
|
|
|
|
* @param string $folder
|
|
|
|
*/
|
|
|
|
public function setCombinedFilesFolder($folder)
|
|
|
|
{
|
|
|
|
$this->combinedFilesFolder = $folder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the combined files folder prefix
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCombinedFilesFolder()
|
|
|
|
{
|
|
|
|
if ($this->combinedFilesFolder) {
|
|
|
|
return $this->combinedFilesFolder;
|
|
|
|
}
|
|
|
|
return Config::inst()->get(__CLASS__, 'default_combined_files_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.
|
|
|
|
*
|
2020-12-21 22:23:23 +01:00
|
|
|
* @param bool $var
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function setSuffixRequirements($var)
|
|
|
|
{
|
|
|
|
$this->suffixRequirements = $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether we want to suffix requirements
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getSuffixRequirements()
|
|
|
|
{
|
|
|
|
return $this->suffixRequirements;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether you want to write the JS to the body of the page rather than at the end of the
|
|
|
|
* head tag.
|
|
|
|
*
|
2020-12-21 22:23:23 +01:00
|
|
|
* @param bool $var
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setWriteJavascriptToBody($var)
|
|
|
|
{
|
|
|
|
$this->writeJavascriptToBody = $var;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether you want to write the JS to the body of the page rather than at the end of the
|
|
|
|
* head tag.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getWriteJavascriptToBody()
|
|
|
|
{
|
|
|
|
return $this->writeJavascriptToBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces the JavaScript requirements to the end of the body, right before the closing tag
|
|
|
|
*
|
2020-12-21 22:23:23 +01:00
|
|
|
* @param bool $var
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setForceJSToBottom($var)
|
|
|
|
{
|
|
|
|
$this->forceJSToBottom = $var;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the JavaScript requirements are written to the end of the body, right before the closing tag
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getForceJSToBottom()
|
|
|
|
{
|
|
|
|
return $this->forceJSToBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-10 06:24:30 +02:00
|
|
|
* Check if minify files should be combined
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-05-10 06:24:30 +02:00
|
|
|
public function getMinifyCombinedFiles()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-10 06:24:30 +02:00
|
|
|
return $this->minifyCombinedFiles;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-10 06:24:30 +02:00
|
|
|
* Set if combined files should be minified
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @param bool $minify
|
|
|
|
* @return $this
|
|
|
|
*/
|
2017-05-10 06:24:30 +02:00
|
|
|
public function setMinifyCombinedFiles($minify)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-10 06:24:30 +02:00
|
|
|
$this->minifyCombinedFiles = $minify;
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given JavaScript file as required.
|
|
|
|
*
|
2017-06-27 04:30:48 +02:00
|
|
|
* @param string $file Either relative to docroot or in the form "vendor/package:resource"
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param array $options List of options. Available options include:
|
|
|
|
* - 'provides' : List of scripts files included in this file
|
|
|
|
* - 'async' : Boolean value to set async attribute to script tag
|
|
|
|
* - 'defer' : Boolean value to set defer attribute to script tag
|
|
|
|
* - 'type' : Override script type= value.
|
2019-07-21 09:51:22 +02:00
|
|
|
* - 'integrity' : SubResource Integrity hash
|
|
|
|
* - 'crossorigin' : Cross-origin policy for the resource
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
public function javascript($file, $options = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-10-03 01:20:49 +02:00
|
|
|
$file = ModuleResourceLoader::singleton()->resolvePath($file);
|
2017-06-27 04:30:48 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
// Get type
|
|
|
|
$type = null;
|
|
|
|
if (isset($this->javascript[$file]['type'])) {
|
|
|
|
$type = $this->javascript[$file]['type'];
|
|
|
|
}
|
|
|
|
if (isset($options['type'])) {
|
|
|
|
$type = $options['type'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that async/defer is set if it is set once even if file is included multiple times
|
|
|
|
$async = (
|
2019-10-29 21:47:24 +01:00
|
|
|
isset($options['async']) && $options['async']
|
2016-11-29 00:31:16 +01:00
|
|
|
|| (
|
|
|
|
isset($this->javascript[$file])
|
|
|
|
&& isset($this->javascript[$file]['async'])
|
2019-10-29 21:47:24 +01:00
|
|
|
&& $this->javascript[$file]['async']
|
2016-11-29 00:31:16 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$defer = (
|
2019-10-29 21:47:24 +01:00
|
|
|
isset($options['defer']) && $options['defer']
|
2016-11-29 00:31:16 +01:00
|
|
|
|| (
|
|
|
|
isset($this->javascript[$file])
|
|
|
|
&& isset($this->javascript[$file]['defer'])
|
2019-10-29 21:47:24 +01:00
|
|
|
&& $this->javascript[$file]['defer']
|
2016-11-29 00:31:16 +01:00
|
|
|
)
|
|
|
|
);
|
2019-07-21 09:51:22 +02:00
|
|
|
$integrity = $options['integrity'] ?? null;
|
|
|
|
$crossorigin = $options['crossorigin'] ?? null;
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->javascript[$file] = [
|
2016-11-29 00:31:16 +01:00
|
|
|
'async' => $async,
|
|
|
|
'defer' => $defer,
|
|
|
|
'type' => $type,
|
2019-07-21 09:51:22 +02:00
|
|
|
'integrity' => $integrity,
|
|
|
|
'crossorigin' => $crossorigin,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// Record scripts included in this file
|
|
|
|
if (isset($options['provides'])) {
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->providedJavascript[$file] = array_values($options['provides'] ?? []);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a javascript requirement
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
protected function unsetJavascript($file)
|
|
|
|
{
|
|
|
|
unset($this->javascript[$file]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all scripts that are already provided by prior scripts.
|
|
|
|
* This follows these rules:
|
|
|
|
* - Files will not be considered provided if they are separately
|
|
|
|
* included prior to the providing file.
|
|
|
|
* - Providing files can be blocked, and don't provide anything
|
|
|
|
* - Provided files can't be blocked (you need to block the provider)
|
|
|
|
* - If a combined file includes files that are provided by prior
|
|
|
|
* scripts, then these should be excluded from the combined file.
|
|
|
|
* - If a combined file includes files that are provided by later
|
|
|
|
* scripts, then these files should be included in the combined
|
|
|
|
* file, but we can't block the later script either (possible double
|
|
|
|
* up of file).
|
|
|
|
*
|
|
|
|
* @return array Array of provided files (map of $path => $path)
|
|
|
|
*/
|
|
|
|
public function getProvidedScripts()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$providedScripts = [];
|
|
|
|
$includedScripts = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->javascript as $script => $options) {
|
|
|
|
// Ignore scripts that are explicitly blocked
|
|
|
|
if (isset($this->blocked[$script])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// At this point, the file is included.
|
|
|
|
// This might also be combined at this point, potentially.
|
|
|
|
$includedScripts[$script] = true;
|
|
|
|
|
|
|
|
// Record any files this provides, EXCEPT those already included by now
|
|
|
|
if (isset($this->providedJavascript[$script])) {
|
|
|
|
foreach ($this->providedJavascript[$script] as $provided) {
|
|
|
|
if (!isset($includedScripts[$provided])) {
|
|
|
|
$providedScripts[$provided] = $provided;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $providedScripts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of required JavaScript, excluding blocked
|
|
|
|
* and duplicates of provided files.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getJavascript()
|
|
|
|
{
|
|
|
|
return array_diff_key(
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->javascript ?? [],
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->getBlocked(),
|
|
|
|
$this->getProvidedScripts()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all javascript, including blocked files. Unwraps the array into a non-associative list
|
|
|
|
*
|
|
|
|
* @return array Indexed array of javascript files
|
|
|
|
*/
|
|
|
|
protected function getAllJavascript()
|
|
|
|
{
|
|
|
|
return $this->javascript;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given JavaScript code into the list of requirements
|
|
|
|
*
|
2019-06-15 03:07:49 +02:00
|
|
|
* @param string $script The script content as a string (without enclosing `<script>` tag)
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
|
|
|
*/
|
|
|
|
public function customScript($script, $uniquenessID = null)
|
|
|
|
{
|
|
|
|
if ($uniquenessID) {
|
|
|
|
$this->customScript[$uniquenessID] = $script;
|
|
|
|
} else {
|
|
|
|
$this->customScript[] = $script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all registered custom scripts
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCustomScripts()
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return array_diff_key($this->customScript ?? [], $this->blocked);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given CSS styles into the list of requirements
|
|
|
|
*
|
2019-06-15 03:07:49 +02:00
|
|
|
* @param string $script CSS selectors as a string (without enclosing `<style>` tag)
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
|
|
|
*/
|
|
|
|
public function customCSS($script, $uniquenessID = null)
|
|
|
|
{
|
|
|
|
if ($uniquenessID) {
|
|
|
|
$this->customCSS[$uniquenessID] = $script;
|
|
|
|
} else {
|
|
|
|
$this->customCSS[] = $script;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all registered custom CSS
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCustomCSS()
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return array_diff_key($this->customCSS ?? [], $this->blocked);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-15 03:07:49 +02:00
|
|
|
* Add the following custom HTML code to the `<head>` section of the page
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @param string $html Custom HTML code
|
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
|
|
|
*/
|
|
|
|
public function insertHeadTags($html, $uniquenessID = null)
|
|
|
|
{
|
|
|
|
if ($uniquenessID) {
|
|
|
|
$this->customHeadTags[$uniquenessID] = $html;
|
|
|
|
} else {
|
|
|
|
$this->customHeadTags[] = $html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all custom head tags
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCustomHeadTags()
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return array_diff_key($this->customHeadTags ?? [], $this->blocked);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include the content of the given JavaScript file in the list of requirements. Dollar-sign
|
|
|
|
* variables will be interpolated with values from $vars similar to a .ss template.
|
|
|
|
*
|
|
|
|
* @param string $file The template file to load, relative to docroot
|
|
|
|
* @param string[] $vars The array of variables to interpolate.
|
|
|
|
* @param string $uniquenessID A unique ID that ensures a piece of code is only added once
|
|
|
|
*/
|
|
|
|
public function javascriptTemplate($file, $vars, $uniquenessID = null)
|
|
|
|
{
|
2017-12-14 23:20:20 +01:00
|
|
|
$file = ModuleResourceLoader::singleton()->resolvePath($file);
|
2018-01-12 04:25:02 +01:00
|
|
|
$absolutePath = Director::getAbsFile($file);
|
2022-04-14 03:12:59 +02:00
|
|
|
if (!file_exists($absolutePath ?? '')) {
|
2018-01-12 04:25:02 +01:00
|
|
|
throw new InvalidArgumentException("Javascript template file {$file} does not exist");
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$script = file_get_contents($absolutePath ?? '');
|
2020-04-20 19:58:09 +02:00
|
|
|
$search = [];
|
|
|
|
$replace = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
if ($vars) {
|
|
|
|
foreach ($vars as $k => $v) {
|
|
|
|
$search[] = '$' . $k;
|
2022-04-14 03:12:59 +02:00
|
|
|
$replace[] = str_replace("\\'", "'", Convert::raw2js($v) ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$script = str_replace($search ?? '', $replace ?? '', $script ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->customScript($script, $uniquenessID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the given stylesheet into the list of requirements.
|
|
|
|
*
|
|
|
|
* @param string $file The CSS file to load, relative to site root
|
|
|
|
* @param string $media Comma-separated list of media types to use in the link tag
|
|
|
|
* (e.g. 'screen,projector')
|
2019-07-21 09:51:22 +02:00
|
|
|
* @param array $options List of options. Available options include:
|
|
|
|
* - 'integrity' : SubResource Integrity hash
|
|
|
|
* - 'crossorigin' : Cross-origin policy for the resource
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2019-07-21 09:51:22 +02:00
|
|
|
public function css($file, $media = null, $options = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-10-03 01:20:49 +02:00
|
|
|
$file = ModuleResourceLoader::singleton()->resolvePath($file);
|
2017-06-27 04:30:48 +02:00
|
|
|
|
2019-07-21 09:51:22 +02:00
|
|
|
$integrity = $options['integrity'] ?? null;
|
|
|
|
$crossorigin = $options['crossorigin'] ?? null;
|
|
|
|
|
2018-01-12 04:25:02 +01:00
|
|
|
$this->css[$file] = [
|
2019-07-21 09:51:22 +02:00
|
|
|
"media" => $media,
|
|
|
|
"integrity" => $integrity,
|
|
|
|
"crossorigin" => $crossorigin,
|
2018-01-12 04:25:02 +01:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a css requirement
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
protected function unsetCSS($file)
|
|
|
|
{
|
|
|
|
unset($this->css[$file]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of registered CSS file requirements, excluding blocked files
|
|
|
|
*
|
|
|
|
* @return array Associative array of file to spec
|
|
|
|
*/
|
|
|
|
public function getCSS()
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return array_diff_key($this->css ?? [], $this->blocked);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all CSS files requirements, including blocked
|
|
|
|
*
|
|
|
|
* @return array Associative array of file to spec
|
|
|
|
*/
|
|
|
|
protected function getAllCSS()
|
|
|
|
{
|
|
|
|
return $this->css;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of all blocked files
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBlocked()
|
|
|
|
{
|
|
|
|
return $this->blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear either a single or all requirements
|
|
|
|
*
|
|
|
|
* Caution: Clearing single rules added via customCSS and customScript only works if you
|
|
|
|
* originally specified a $uniquenessID.
|
|
|
|
*
|
|
|
|
* @param string|int $fileOrID
|
|
|
|
*/
|
|
|
|
public function clear($fileOrID = null)
|
|
|
|
{
|
2017-08-29 00:24:41 +02:00
|
|
|
$types = [
|
|
|
|
'javascript',
|
|
|
|
'css',
|
|
|
|
'customScript',
|
|
|
|
'customCSS',
|
|
|
|
'customHeadTags',
|
|
|
|
'combinedFiles',
|
|
|
|
];
|
|
|
|
foreach ($types as $type) {
|
|
|
|
if ($fileOrID) {
|
2016-11-29 00:31:16 +01:00
|
|
|
if (isset($this->{$type}[$fileOrID])) {
|
|
|
|
$this->disabled[$type][$fileOrID] = $this->{$type}[$fileOrID];
|
|
|
|
unset($this->{$type}[$fileOrID]);
|
|
|
|
}
|
2017-08-29 00:24:41 +02:00
|
|
|
} else {
|
|
|
|
$this->disabled[$type] = $this->{$type};
|
|
|
|
$this->{$type} = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore requirements cleared by call to Requirements::clear
|
|
|
|
*/
|
|
|
|
public function restore()
|
|
|
|
{
|
2017-08-29 00:24:41 +02:00
|
|
|
$types = [
|
|
|
|
'javascript',
|
|
|
|
'css',
|
|
|
|
'customScript',
|
|
|
|
'customCSS',
|
|
|
|
'customHeadTags',
|
|
|
|
'combinedFiles',
|
|
|
|
];
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$this->{$type} = $this->disabled[$type];
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block inclusion of a specific file
|
|
|
|
*
|
|
|
|
* The difference between this and {@link clear} is that the calling order does not matter;
|
|
|
|
* {@link clear} must be called after the initial registration, whereas {@link block} can be
|
|
|
|
* used in advance. This is useful, for example, to block scripts included by a superclass
|
|
|
|
* without having to override entire functions and duplicate a lot of code.
|
|
|
|
*
|
|
|
|
* Note that blocking should be used sparingly because it's hard to trace where an file is
|
|
|
|
* being blocked from.
|
|
|
|
*
|
2017-12-17 04:09:22 +01:00
|
|
|
* @param string|int $fileOrID Relative path from webroot, module resource reference or
|
|
|
|
* requirement API ID
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function block($fileOrID)
|
|
|
|
{
|
2017-12-17 04:09:22 +01:00
|
|
|
if (is_string($fileOrID)) {
|
|
|
|
$fileOrID = ModuleResourceLoader::singleton()->resolvePath($fileOrID);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->blocked[$fileOrID] = $fileOrID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the block list
|
|
|
|
*
|
|
|
|
* @param string|int $fileOrID
|
|
|
|
*/
|
|
|
|
public function unblock($fileOrID)
|
|
|
|
{
|
|
|
|
unset($this->blocked[$fileOrID]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all items from the block list
|
|
|
|
*/
|
|
|
|
public function unblockAll()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->blocked = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the given HTML content with the appropriate include tags for the registered
|
|
|
|
* requirements. Needs to receive a valid HTML/XHTML template in the $content parameter,
|
|
|
|
* including a head and body tag.
|
|
|
|
*
|
|
|
|
* @param string $content HTML content that has already been parsed from the $templateFile
|
|
|
|
* through {@link SSViewer}
|
|
|
|
* @return string HTML content augmented with the requirements tags
|
|
|
|
*/
|
|
|
|
public function includeInHTML($content)
|
|
|
|
{
|
|
|
|
if (func_num_args() > 1) {
|
|
|
|
Deprecation::notice(
|
|
|
|
'5.0',
|
|
|
|
'$templateFile argument is deprecated. includeInHTML takes a sole $content parameter now.'
|
|
|
|
);
|
|
|
|
$content = func_get_arg(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if content isn't injectable, or there is nothing to inject
|
2022-04-14 03:12:59 +02:00
|
|
|
$tagsAvailable = preg_match('#</head\b#', $content ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
$hasFiles = $this->css || $this->javascript || $this->customCSS || $this->customScript || $this->customHeadTags;
|
|
|
|
if (!$tagsAvailable || !$hasFiles) {
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
$requirements = '';
|
|
|
|
$jsRequirements = '';
|
|
|
|
|
|
|
|
// Combine files - updates $this->javascript and $this->css
|
|
|
|
$this->processCombinedFiles();
|
|
|
|
|
2017-04-18 00:34:18 +02:00
|
|
|
// Script tags for js links
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->getJavascript() as $file => $attributes) {
|
2017-04-18 00:34:18 +02:00
|
|
|
// Build html attributes
|
|
|
|
$htmlAttributes = [
|
|
|
|
'type' => isset($attributes['type']) ? $attributes['type'] : "application/javascript",
|
|
|
|
'src' => $this->pathForFile($file),
|
|
|
|
];
|
|
|
|
if (!empty($attributes['async'])) {
|
|
|
|
$htmlAttributes['async'] = 'async';
|
|
|
|
}
|
|
|
|
if (!empty($attributes['defer'])) {
|
|
|
|
$htmlAttributes['defer'] = 'defer';
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2019-07-21 09:51:22 +02:00
|
|
|
if (!empty($attributes['integrity'])) {
|
|
|
|
$htmlAttributes['integrity'] = $attributes['integrity'];
|
|
|
|
}
|
|
|
|
if (!empty($attributes['crossorigin'])) {
|
|
|
|
$htmlAttributes['crossorigin'] = $attributes['crossorigin'];
|
|
|
|
}
|
2017-04-18 00:34:18 +02:00
|
|
|
$jsRequirements .= HTML::createTag('script', $htmlAttributes);
|
|
|
|
$jsRequirements .= "\n";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add all inline JavaScript *after* including external files they might rely on
|
|
|
|
foreach ($this->getCustomScripts() as $script) {
|
2017-04-18 00:34:18 +02:00
|
|
|
$jsRequirements .= HTML::createTag(
|
|
|
|
'script',
|
|
|
|
[ 'type' => 'application/javascript' ],
|
|
|
|
"//<![CDATA[\n{$script}\n//]]>"
|
|
|
|
);
|
|
|
|
$jsRequirements .= "\n";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 00:34:18 +02:00
|
|
|
// CSS file links
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->getCSS() as $file => $params) {
|
2017-04-18 00:34:18 +02:00
|
|
|
$htmlAttributes = [
|
|
|
|
'rel' => 'stylesheet',
|
|
|
|
'type' => 'text/css',
|
|
|
|
'href' => $this->pathForFile($file),
|
|
|
|
];
|
|
|
|
if (!empty($params['media'])) {
|
|
|
|
$htmlAttributes['media'] = $params['media'];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2019-07-21 09:51:22 +02:00
|
|
|
if (!empty($params['integrity'])) {
|
|
|
|
$htmlAttributes['integrity'] = $params['integrity'];
|
|
|
|
}
|
|
|
|
if (!empty($params['crossorigin'])) {
|
|
|
|
$htmlAttributes['crossorigin'] = $params['crossorigin'];
|
|
|
|
}
|
2017-04-18 00:34:18 +02:00
|
|
|
$requirements .= HTML::createTag('link', $htmlAttributes);
|
|
|
|
$requirements .= "\n";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-04-18 00:34:18 +02:00
|
|
|
// Literal custom CSS content
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->getCustomCSS() as $css) {
|
2017-04-18 00:34:18 +02:00
|
|
|
$requirements .= HTML::createTag('style', ['type' => 'text/css'], "\n{$css}\n");
|
|
|
|
$requirements .= "\n";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->getCustomHeadTags() as $customHeadTag) {
|
2017-04-18 00:34:18 +02:00
|
|
|
$requirements .= "{$customHeadTag}\n";
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-06-15 03:07:49 +02:00
|
|
|
* the closing `</body>` tag
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2019-06-15 03:07:49 +02:00
|
|
|
* @param string $jsRequirements String containing one or more javascript `<script />` tags
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $content HTML body
|
|
|
|
* @return string Merged HTML
|
|
|
|
*/
|
|
|
|
protected function insertScriptsAtBottom($jsRequirements, $content)
|
|
|
|
{
|
|
|
|
// Forcefully put the scripts at the bottom of the body instead of before the first
|
|
|
|
// script tag.
|
|
|
|
$content = preg_replace(
|
|
|
|
'/(<\/body[^>]*>)/i',
|
|
|
|
$this->escapeReplacement($jsRequirements) . '\\1',
|
2022-04-14 03:12:59 +02:00
|
|
|
$content ?? ''
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-15 03:07:49 +02:00
|
|
|
* Given a block of HTML, insert the given scripts inside the `<body></body>`
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2019-06-15 03:07:49 +02:00
|
|
|
* @param string $jsRequirements String containing one or more javascript `<script />` tags
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $content HTML body
|
|
|
|
* @return string Merged HTML
|
|
|
|
*/
|
|
|
|
protected function insertScriptsIntoBody($jsRequirements, $content)
|
|
|
|
{
|
|
|
|
// If your template already has script tags in the body, then we try to put our script
|
|
|
|
// tags just before those. Otherwise, we put it at the bottom.
|
2022-04-14 03:12:59 +02:00
|
|
|
$bodyTagPosition = stripos($content ?? '', '<body');
|
|
|
|
$scriptTagPosition = stripos($content ?? '', '<script', $bodyTagPosition ?? 0);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$commentTags = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
$canWriteToBody = ($scriptTagPosition !== false)
|
|
|
|
&&
|
|
|
|
// Check that the script tag is not inside a html comment tag
|
|
|
|
!(
|
2022-04-14 03:12:59 +02:00
|
|
|
preg_match('/.*(?|(<!--)|(-->))/U', $content ?? '', $commentTags, 0, $scriptTagPosition ?? 0)
|
2016-11-29 00:31:16 +01:00
|
|
|
&&
|
|
|
|
$commentTags[1] == '-->'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($canWriteToBody) {
|
|
|
|
// Insert content before existing script tags
|
2022-04-14 03:12:59 +02:00
|
|
|
$content = substr($content ?? '', 0, $scriptTagPosition)
|
2016-11-29 00:31:16 +01:00
|
|
|
. $jsRequirements
|
2022-04-14 03:12:59 +02:00
|
|
|
. substr($content ?? '', $scriptTagPosition ?? 0);
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
|
|
|
// Insert content at bottom of page otherwise
|
|
|
|
$content = $this->insertScriptsAtBottom($jsRequirements, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-15 03:07:49 +02:00
|
|
|
* Given a block of HTML, insert the given code inside the `<head></head>` block
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @param string $jsRequirements String containing one or more html tags
|
|
|
|
* @param string $content HTML body
|
|
|
|
* @return string Merged HTML
|
|
|
|
*/
|
|
|
|
protected function insertTagsIntoHead($jsRequirements, $content)
|
|
|
|
{
|
|
|
|
$content = preg_replace(
|
|
|
|
'/(<\/head>)/i',
|
|
|
|
$this->escapeReplacement($jsRequirements) . '\\1',
|
2022-04-14 03:12:59 +02:00
|
|
|
$content ?? ''
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safely escape a literal string for use in preg_replace replacement
|
|
|
|
*
|
|
|
|
* @param string $replacement
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function escapeReplacement($replacement)
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return addcslashes($replacement ?? '', '\\$');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given
|
|
|
|
* HTTP Response
|
|
|
|
*
|
|
|
|
* @param HTTPResponse $response
|
|
|
|
*/
|
|
|
|
public function includeInResponse(HTTPResponse $response)
|
|
|
|
{
|
|
|
|
$this->processCombinedFiles();
|
2020-04-20 19:58:09 +02:00
|
|
|
$jsRequirements = [];
|
|
|
|
$cssRequirements = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
foreach ($this->getJavascript() as $file => $attributes) {
|
|
|
|
$path = $this->pathForFile($file);
|
|
|
|
if ($path) {
|
2022-04-14 03:12:59 +02:00
|
|
|
$jsRequirements[] = str_replace(',', '%2C', $path ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
if (count($jsRequirements ?? [])) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$response->addHeader('X-Include-JS', implode(',', $jsRequirements));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->getCSS() as $file => $params) {
|
|
|
|
$path = $this->pathForFile($file);
|
|
|
|
if ($path) {
|
2022-04-14 03:12:59 +02:00
|
|
|
$path = str_replace(',', '%2C', $path ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
$cssRequirements[] = isset($params['media']) ? "$path:##:$params[media]" : $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
if (count($cssRequirements ?? [])) {
|
2016-11-29 00:31:16 +01:00
|
|
|
$response->addHeader('X-Include-CSS', implode(',', $cssRequirements));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add i18n files from the given javascript directory. SilverStripe expects that the given
|
|
|
|
* directory will contain a number of JavaScript files named by language: en_US.js, de_DE.js,
|
|
|
|
* etc.
|
|
|
|
*
|
|
|
|
* @param string $langDir The JavaScript lang directory, relative to the site root, e.g.,
|
|
|
|
* 'framework/javascript/lang'
|
|
|
|
* @param bool $return Return all relative file paths rather than including them in
|
|
|
|
* requirements
|
|
|
|
*
|
|
|
|
* @return array|null All relative files if $return is true, or null otherwise
|
|
|
|
*/
|
|
|
|
public function add_i18n_javascript($langDir, $return = false)
|
|
|
|
{
|
2017-10-10 05:51:47 +02:00
|
|
|
$langDir = ModuleResourceLoader::singleton()->resolvePath($langDir);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$files = [];
|
|
|
|
$candidates = [
|
2019-01-15 03:04:39 +01:00
|
|
|
'en',
|
|
|
|
'en_US',
|
|
|
|
i18n::getData()->langFromLocale(i18n::config()->get('default_locale')),
|
|
|
|
i18n::config()->get('default_locale'),
|
|
|
|
i18n::getData()->langFromLocale(i18n::get_locale()),
|
|
|
|
i18n::get_locale(),
|
2022-04-14 03:12:59 +02:00
|
|
|
strtolower(DBField::create_field('Locale', i18n::get_locale())->RFC1766() ?? ''),
|
|
|
|
strtolower(DBField::create_field('Locale', i18n::config()->get('default_locale'))->RFC1766() ?? '')
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2019-01-15 03:04:39 +01:00
|
|
|
|
|
|
|
$candidates = array_map(
|
2019-01-15 22:36:53 +01:00
|
|
|
function ($candidate) {
|
|
|
|
return $candidate . '.js';
|
|
|
|
},
|
2022-04-14 03:12:59 +02:00
|
|
|
$candidates ?? []
|
2019-01-15 03:04:39 +01:00
|
|
|
);
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($candidates as $candidate) {
|
2018-01-12 04:25:02 +01:00
|
|
|
$relativePath = Path::join($langDir, $candidate);
|
|
|
|
$absolutePath = Director::getAbsFile($relativePath);
|
2022-04-14 03:12:59 +02:00
|
|
|
if (file_exists($absolutePath ?? '')) {
|
2018-01-12 04:25:02 +01:00
|
|
|
$files[] = $relativePath;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($return) {
|
|
|
|
return $files;
|
|
|
|
}
|
2018-01-12 04:25:02 +01:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$this->javascript($file);
|
|
|
|
}
|
|
|
|
return null;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the path for specified file
|
|
|
|
*
|
|
|
|
* @param string $fileOrUrl
|
|
|
|
* @return string|bool
|
|
|
|
*/
|
|
|
|
protected function pathForFile($fileOrUrl)
|
|
|
|
{
|
|
|
|
// Since combined urls could be root relative, treat them as urls here.
|
2022-04-14 03:12:59 +02:00
|
|
|
if (preg_match('{^(//)|(http[s]?:)}', $fileOrUrl ?? '') || Director::is_root_relative_url($fileOrUrl)) {
|
2016-11-29 00:31:16 +01:00
|
|
|
return $fileOrUrl;
|
|
|
|
} else {
|
2017-06-27 04:30:48 +02:00
|
|
|
return Injector::inst()->get(ResourceURLGenerator::class)->urlForResource($fileOrUrl);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenate several css or javascript files into a single dynamically generated file. 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.
|
|
|
|
*
|
|
|
|
* All combined files will have a comment on the start of each concatenated file denoting their
|
|
|
|
* original position.
|
|
|
|
*
|
|
|
|
* 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 incorrect inheritance. You can also
|
|
|
|
* only include each file once across all includes and combinations in a single page load.
|
|
|
|
*
|
|
|
|
* CAUTION: Combining CSS Files discards any "media" information.
|
|
|
|
*
|
|
|
|
* Example for combined JavaScript:
|
|
|
|
* <code>
|
|
|
|
* Requirements::combine_files(
|
|
|
|
* 'foobar.js',
|
|
|
|
* array(
|
|
|
|
* 'mysite/javascript/foo.js',
|
|
|
|
* 'mysite/javascript/bar.js',
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* 'async' => true,
|
|
|
|
* 'defer' => true,
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example for combined CSS:
|
|
|
|
* <code>
|
|
|
|
* Requirements::combine_files(
|
|
|
|
* 'foobar.css',
|
|
|
|
* array(
|
|
|
|
* 'mysite/javascript/foo.css',
|
|
|
|
* 'mysite/javascript/bar.css',
|
|
|
|
* ),
|
|
|
|
* array(
|
|
|
|
* 'media' => 'print',
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param string $combinedFileName Filename of the combined file relative to docroot
|
|
|
|
* @param array $files Array of filenames relative to docroot
|
|
|
|
* @param array $options Array of options for combining files. Available options are:
|
|
|
|
* - 'media' : If including CSS Files, you can specify a media type
|
|
|
|
* - 'async' : If including JavaScript Files, boolean value to set async attribute to script tag
|
|
|
|
* - 'defer' : If including JavaScript Files, boolean value to set defer attribute to script tag
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
public function combineFiles($combinedFileName, $files, $options = [])
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
if (is_string($options)) {
|
|
|
|
Deprecation::notice('4.0', 'Parameter media is deprecated. Use options array instead.');
|
2020-04-20 19:58:09 +02:00
|
|
|
$options = ['media' => $options];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
// Skip this combined files if already included
|
|
|
|
if (isset($this->combinedFiles[$combinedFileName])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all files to necessary type list
|
2020-04-20 19:58:09 +02:00
|
|
|
$paths = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
$combinedType = null;
|
|
|
|
foreach ($files as $file) {
|
|
|
|
// Get file details
|
|
|
|
list($path, $type) = $this->parseCombinedFile($file);
|
|
|
|
if ($type === 'javascript') {
|
|
|
|
$type = 'js';
|
|
|
|
}
|
|
|
|
if ($combinedType && $type && $combinedType !== $type) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"Cannot mix js and css files in same combined file {$combinedFileName}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
switch ($type) {
|
|
|
|
case 'css':
|
2019-07-21 09:51:22 +02:00
|
|
|
$this->css($path, (isset($options['media']) ? $options['media'] : null), $options);
|
2016-11-29 00:31:16 +01:00
|
|
|
break;
|
|
|
|
case 'js':
|
|
|
|
$this->javascript($path, $options);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException("Invalid combined file type: {$type}");
|
|
|
|
}
|
|
|
|
$combinedType = $type;
|
|
|
|
$paths[] = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate check
|
|
|
|
foreach ($this->combinedFiles as $existingCombinedFilename => $combinedItem) {
|
|
|
|
$existingFiles = $combinedItem['files'];
|
2022-04-14 03:12:59 +02:00
|
|
|
$duplicates = array_intersect($existingFiles ?? [], $paths);
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($duplicates) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
"Requirements_Backend::combine_files(): Already included file(s) %s in combined file '%s'",
|
|
|
|
implode(',', $duplicates),
|
|
|
|
$existingCombinedFilename
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->combinedFiles[$combinedFileName] = [
|
2016-11-29 00:31:16 +01:00
|
|
|
'files' => $paths,
|
|
|
|
'type' => $combinedType,
|
|
|
|
'options' => $options,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return path and type of given combined file
|
|
|
|
*
|
|
|
|
* @param string|array $file Either a file path, or an array spec
|
|
|
|
* @return array array with two elements, path and type of file
|
|
|
|
*/
|
|
|
|
protected function parseCombinedFile($file)
|
|
|
|
{
|
|
|
|
// Array with path and type keys
|
|
|
|
if (is_array($file) && isset($file['path']) && isset($file['type'])) {
|
2020-04-20 19:58:09 +02:00
|
|
|
return [$file['path'], $file['type']];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract value from indexed array
|
|
|
|
if (is_array($file)) {
|
|
|
|
$path = array_shift($file);
|
|
|
|
|
|
|
|
// See if there's a type specifier
|
|
|
|
if ($file) {
|
|
|
|
$type = array_shift($file);
|
2020-04-20 19:58:09 +02:00
|
|
|
return [$path, $type];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise convent to string
|
|
|
|
$file = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = File::get_file_extension($file);
|
2020-04-20 19:58:09 +02:00
|
|
|
return [$file, $type];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all combined files; keys are the combined file names, values are lists of
|
|
|
|
* associative arrays with 'files', 'type', and 'media' keys for details about this
|
|
|
|
* combined file.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCombinedFiles()
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
return array_diff_key($this->combinedFiles ?? [], $this->blocked);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes all combined files, including blocked ones
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getAllCombinedFiles()
|
|
|
|
{
|
|
|
|
return $this->combinedFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all combined files
|
|
|
|
*/
|
|
|
|
public function deleteAllCombinedFiles()
|
|
|
|
{
|
|
|
|
$combinedFolder = $this->getCombinedFilesFolder();
|
|
|
|
if ($combinedFolder) {
|
|
|
|
$this->getAssetHandler()->removeContent($combinedFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all registered CSS and JavaScript file combinations
|
|
|
|
*/
|
|
|
|
public function clearCombinedFiles()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->combinedFiles = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the heavy lifting involved in combining the combined files.
|
|
|
|
*/
|
|
|
|
public function processCombinedFiles()
|
|
|
|
{
|
|
|
|
// Check if combining is enabled
|
|
|
|
if (!$this->getCombinedFilesEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before scripts are modified, detect files that are provided by preceding ones
|
|
|
|
$providedScripts = $this->getProvidedScripts();
|
|
|
|
|
|
|
|
// Process each combined files
|
|
|
|
foreach ($this->getAllCombinedFiles() as $combinedFile => $combinedItem) {
|
|
|
|
$fileList = $combinedItem['files'];
|
|
|
|
$type = $combinedItem['type'];
|
|
|
|
$options = $combinedItem['options'];
|
|
|
|
|
|
|
|
// Generate this file, unless blocked
|
|
|
|
$combinedURL = null;
|
|
|
|
if (!isset($this->blocked[$combinedFile])) {
|
|
|
|
// Filter files for blocked / provided
|
|
|
|
$filteredFileList = array_diff(
|
2022-04-14 03:12:59 +02:00
|
|
|
$fileList ?? [],
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->getBlocked(),
|
|
|
|
$providedScripts
|
|
|
|
);
|
|
|
|
$combinedURL = $this->getCombinedFileURL($combinedFile, $filteredFileList, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all existing files, injecting the combined file at the position of the first item
|
|
|
|
// in order to preserve inclusion order.
|
|
|
|
// Note that we iterate across blocked files in order to get the correct order, and validate
|
|
|
|
// that the file is included in the correct location (regardless of which files are blocked).
|
|
|
|
$included = false;
|
|
|
|
switch ($type) {
|
|
|
|
case 'css': {
|
2020-04-20 19:58:09 +02:00
|
|
|
$newCSS = []; // Assoc array of css file => spec
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->getAllCSS() as $css => $spec) {
|
|
|
|
if (!in_array($css, $fileList)) {
|
|
|
|
$newCSS[$css] = $spec;
|
|
|
|
} elseif (!$included && $combinedURL) {
|
2020-04-20 19:58:09 +02:00
|
|
|
$newCSS[$combinedURL] = [
|
2019-07-21 09:51:22 +02:00
|
|
|
'media' => $options['media'] ?? null,
|
|
|
|
'integrity' => $options['integrity'] ?? null,
|
|
|
|
'crossorigin' => $options['crossorigin'] ?? null,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
$included = true;
|
|
|
|
}
|
|
|
|
// If already included, or otherwise blocked, then don't add into CSS
|
|
|
|
}
|
|
|
|
$this->css = $newCSS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'js': {
|
|
|
|
// Assoc array of file => attributes
|
2020-04-20 19:58:09 +02:00
|
|
|
$newJS = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
foreach ($this->getAllJavascript() as $script => $attributes) {
|
|
|
|
if (!in_array($script, $fileList)) {
|
|
|
|
$newJS[$script] = $attributes;
|
|
|
|
} elseif (!$included && $combinedURL) {
|
|
|
|
$newJS[$combinedURL] = $options;
|
|
|
|
$included = true;
|
|
|
|
}
|
|
|
|
// If already included, or otherwise blocked, then don't add into scripts
|
|
|
|
}
|
|
|
|
$this->javascript = $newJS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a set of files, combine them (as necessary) and return the url
|
|
|
|
*
|
|
|
|
* @param string $combinedFile Filename for this combined file
|
|
|
|
* @param array $fileList List of files to combine
|
|
|
|
* @param string $type Either 'js' or 'css'
|
|
|
|
* @return string|null URL to this resource, if there are files to combine
|
2017-05-11 07:38:29 +02:00
|
|
|
* @throws Exception
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected function getCombinedFileURL($combinedFile, $fileList, $type)
|
|
|
|
{
|
|
|
|
// Skip empty lists
|
|
|
|
if (empty($fileList)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate path (Filename)
|
|
|
|
$hashQuerystring = Config::inst()->get(static::class, 'combine_hash_querystring');
|
|
|
|
if (!$hashQuerystring) {
|
|
|
|
$combinedFile = $this->hashedCombinedFilename($combinedFile, $fileList);
|
|
|
|
}
|
|
|
|
$combinedFileID = File::join_paths($this->getCombinedFilesFolder(), $combinedFile);
|
|
|
|
|
|
|
|
// Send file combination request to the backend, with an optional callback to perform regeneration
|
2017-05-10 06:24:30 +02:00
|
|
|
$minify = $this->getMinifyCombinedFiles();
|
|
|
|
if ($minify && !$this->minifier) {
|
|
|
|
throw new Exception(
|
|
|
|
sprintf(
|
2017-06-22 12:50:45 +02:00
|
|
|
<<<MESSAGE
|
|
|
|
Cannot minify files without a minification service defined.
|
|
|
|
Set %s::minifyCombinedFiles to false, or inject a %s service on
|
|
|
|
%s.properties.minifier
|
|
|
|
MESSAGE
|
|
|
|
,
|
2017-05-10 06:24:30 +02:00
|
|
|
__CLASS__,
|
|
|
|
Requirements_Minifier::class,
|
|
|
|
__CLASS__
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
$combinedURL = $this
|
|
|
|
->getAssetHandler()
|
|
|
|
->getContentURL(
|
|
|
|
$combinedFileID,
|
|
|
|
function () use ($fileList, $minify, $type) {
|
|
|
|
// Physically combine all file content
|
|
|
|
$combinedData = '';
|
|
|
|
foreach ($fileList as $file) {
|
2018-01-12 04:25:02 +01:00
|
|
|
$filePath = Director::getAbsFile($file);
|
2022-04-14 03:12:59 +02:00
|
|
|
if (!file_exists($filePath ?? '')) {
|
2018-01-12 04:25:02 +01:00
|
|
|
throw new InvalidArgumentException("Combined file {$file} does not exist");
|
|
|
|
}
|
2022-04-14 03:12:59 +02:00
|
|
|
$fileContent = file_get_contents($filePath ?? '');
|
2021-11-01 02:16:00 +01:00
|
|
|
if ($type == 'css') {
|
|
|
|
// resolve relative paths for css files
|
|
|
|
$fileContent = $this->resolveCSSReferences($fileContent, $file);
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
// Use configured minifier
|
|
|
|
if ($minify) {
|
2017-05-10 06:24:30 +02:00
|
|
|
$fileContent = $this->minifier->minify($fileContent, $type, $file);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->writeHeaderComment) {
|
|
|
|
// Write a header comment for each file for easier identification and debugging.
|
|
|
|
$combinedData .= "/****** FILE: $file *****/\n";
|
|
|
|
}
|
|
|
|
$combinedData .= $fileContent . "\n";
|
|
|
|
}
|
|
|
|
return $combinedData;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the name isn't hashed, we will need to append the querystring m= parameter instead
|
|
|
|
// Since url won't be automatically suffixed, add it in here
|
|
|
|
if ($hashQuerystring && $this->getSuffixRequirements()) {
|
|
|
|
$hash = $this->hashOfFiles($fileList);
|
2022-04-14 03:12:59 +02:00
|
|
|
$q = stripos($combinedURL ?? '', '?') === false ? '?' : '&';
|
2016-11-29 00:31:16 +01:00
|
|
|
$combinedURL .= "{$q}m={$hash}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $combinedURL;
|
|
|
|
}
|
|
|
|
|
2021-11-01 02:16:00 +01:00
|
|
|
/**
|
|
|
|
* Resolves relative paths in CSS files which are lost when combining them
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @param string $filePath
|
|
|
|
* @return string New content with paths resolved
|
|
|
|
*/
|
|
|
|
protected function resolveCSSReferences($content, $filePath)
|
|
|
|
{
|
2021-11-09 02:15:12 +01:00
|
|
|
$doResolving = Config::inst()->get(__CLASS__, 'resolve_relative_css_refs');
|
|
|
|
if (!$doResolving) {
|
|
|
|
return $content;
|
|
|
|
}
|
2021-11-01 02:16:00 +01:00
|
|
|
$fileUrl = Injector::inst()->get(ResourceURLGenerator::class)->urlForResource($filePath);
|
|
|
|
$fileUrlDir = dirname($fileUrl);
|
2021-11-09 02:15:12 +01:00
|
|
|
$content = preg_replace_callback('#([\s\'"\(])((:?[\.]{1,2}/)+)#', function ($a) use ($fileUrlDir) {
|
|
|
|
[ $_, $prefix, $match ] = $a;
|
|
|
|
$full = $fileUrlDir . '/' . $match;
|
2021-11-01 02:16:00 +01:00
|
|
|
$full = preg_replace('#/{2,}#', '/', $full); // ensure there's no repeated slashes
|
|
|
|
while (strpos($full, './') > 0) {
|
|
|
|
$post = $full;
|
|
|
|
$post = preg_replace('#([^/\.]+)/\.\./#', '', $post); // erase 'something/../' with the predecessor
|
|
|
|
$post = preg_replace('#([^/\.]+)/\./#', '\\1/', $post); // erase './'
|
|
|
|
if ($post == $full) {
|
|
|
|
break; // nothing changed
|
|
|
|
}
|
|
|
|
$full = $post;
|
|
|
|
}
|
2021-11-09 02:15:12 +01:00
|
|
|
return $prefix . $full;
|
2021-11-01 02:16:00 +01:00
|
|
|
}, $content);
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Given a filename and list of files, generate a new filename unique to these files
|
|
|
|
*
|
|
|
|
* @param string $combinedFile
|
|
|
|
* @param array $fileList
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function hashedCombinedFilename($combinedFile, $fileList)
|
|
|
|
{
|
2022-04-14 03:12:59 +02:00
|
|
|
$name = pathinfo($combinedFile ?? '', PATHINFO_FILENAME);
|
2016-11-29 00:31:16 +01:00
|
|
|
$hash = $this->hashOfFiles($fileList);
|
|
|
|
$extension = File::get_file_extension($combinedFile);
|
2022-04-14 03:12:59 +02:00
|
|
|
return $name . '-' . substr($hash ?? '', 0, 7) . '.' . $extension;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if combined files are enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getCombinedFilesEnabled()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
if (isset($this->combinedFilesEnabled)) {
|
|
|
|
return $this->combinedFilesEnabled;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Non-dev sites are always combined
|
|
|
|
if (!Director::isDev()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to default
|
|
|
|
return Config::inst()->get(__CLASS__, 'combine_in_dev');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For a given filelist, determine some discriminating value to determine if
|
|
|
|
* any of these files have changed.
|
|
|
|
*
|
|
|
|
* @param array $fileList List of files
|
|
|
|
* @return string SHA1 bashed file hash
|
|
|
|
*/
|
|
|
|
protected function hashOfFiles($fileList)
|
|
|
|
{
|
|
|
|
// Get hash based on hash of each file
|
|
|
|
$hash = '';
|
|
|
|
foreach ($fileList as $file) {
|
2018-01-12 04:25:02 +01:00
|
|
|
$absolutePath = Director::getAbsFile($file);
|
2022-04-14 03:12:59 +02:00
|
|
|
if (file_exists($absolutePath ?? '')) {
|
|
|
|
$hash .= sha1_file($absolutePath ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Combined file {$file} does not exist");
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 03:12:59 +02:00
|
|
|
return sha1($hash ?? '');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the given themeable stylesheet as required.
|
|
|
|
*
|
|
|
|
* A CSS file in the current theme path name 'themename/css/$name.css' is first searched for,
|
|
|
|
* and it that doesn't exist and the module parameter is set then a CSS file with that name in
|
|
|
|
* the module is used.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the file - eg '/css/File.css' would have the name 'File'
|
|
|
|
* @param string $media Comma-separated list of media types to use in the link tag
|
|
|
|
* (e.g. 'screen,projector')
|
|
|
|
*/
|
|
|
|
public function themedCSS($name, $media = null)
|
|
|
|
{
|
2017-05-19 04:38:06 +02:00
|
|
|
$path = ThemeResourceLoader::inst()->findThemedCSS($name, SSViewer::get_themes());
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($path) {
|
|
|
|
$this->css($path, $media);
|
|
|
|
} else {
|
2017-10-03 01:20:49 +02:00
|
|
|
throw new InvalidArgumentException(
|
2017-05-13 21:37:14 +02:00
|
|
|
"The css file doesn't exist. Please check if the file $name.css exists in any context or search for "
|
2016-11-29 00:31:16 +01:00
|
|
|
. "themedCSS references calling this file in your templates."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the given themeable javascript as required.
|
|
|
|
*
|
|
|
|
* A javascript file in the current theme path name 'themename/javascript/$name.js' is first searched for,
|
|
|
|
* and it that doesn't exist and the module parameter is set then a javascript file with that name in
|
|
|
|
* the module is used.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the file - eg '/js/File.js' would have the name 'File'
|
|
|
|
* @param string $type Comma-separated list of types to use in the script tag
|
|
|
|
* (e.g. 'text/javascript,text/ecmascript')
|
|
|
|
*/
|
|
|
|
public function themedJavascript($name, $type = null)
|
|
|
|
{
|
2017-05-19 04:38:06 +02:00
|
|
|
$path = ThemeResourceLoader::inst()->findThemedJavascript($name, SSViewer::get_themes());
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($path) {
|
|
|
|
$opts = [];
|
|
|
|
if ($type) {
|
|
|
|
$opts['type'] = $type;
|
|
|
|
}
|
|
|
|
$this->javascript($path, $opts);
|
|
|
|
} else {
|
2017-10-03 01:20:49 +02:00
|
|
|
throw new InvalidArgumentException(
|
2017-05-13 21:37:14 +02:00
|
|
|
"The javascript file doesn't exist. Please check if the file $name.js exists in any "
|
2016-11-29 00:31:16 +01:00
|
|
|
. "context or search for themedJavascript references calling this file in your templates."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output debugging information.
|
|
|
|
*/
|
|
|
|
public 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->combinedFiles);
|
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|