mirror of
https://github.com/getgrav/grav.git
synced 2026-07-07 13:03:06 +02:00
Fixed pipeline + remote url rewrites #2216
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
* Fixed asset manager to not add empty assets when they don't exist in the filesystem
|
||||
* Regression: Fixed asset manager methods with default legacy attributes
|
||||
* Update `script` and `style` Twig tags to use the new `Assets` classes
|
||||
* Fixed asset pipeline to rewrite remote URLs as well as local [#2216](https://github.com/getgrav/grav/issues/2216)
|
||||
|
||||
# v1.6.0-beta.1
|
||||
## 10/01/2018
|
||||
|
||||
@@ -130,7 +130,7 @@ class Pipeline extends PropertyObject
|
||||
}
|
||||
|
||||
// Concatenate files
|
||||
$buffer = $this->gatherLinks($assets, self::CSS_ASSET);
|
||||
$buffer = $this->gatherLinks($assets, self::CSS_ASSET, $no_pipeline);
|
||||
|
||||
// Minify if required
|
||||
if ($this->shouldMinify('css')) {
|
||||
@@ -160,7 +160,8 @@ class Pipeline extends PropertyObject
|
||||
*
|
||||
* @param array $assets
|
||||
* @param string $group
|
||||
* @param array $attributes
|
||||
* @param array $attributes
|
||||
* @param array $no_pipeline
|
||||
*
|
||||
* @return bool|string URL or generated content if available, else false
|
||||
*/
|
||||
@@ -202,7 +203,7 @@ class Pipeline extends PropertyObject
|
||||
}
|
||||
|
||||
// Concatenate files
|
||||
$buffer = $this->gatherLinks($assets, self::JS_ASSET);
|
||||
$buffer = $this->gatherLinks($assets, self::JS_ASSET, $no_pipeline);
|
||||
|
||||
// Minify if required
|
||||
if ($this->shouldMinify('js')) {
|
||||
@@ -228,23 +229,23 @@ class Pipeline extends PropertyObject
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Finds relative CSS urls() and rewrites the URL with an absolute one
|
||||
*
|
||||
* @param string $file the css source file
|
||||
* @param string $relative_path relative path to the css file
|
||||
* @param string $file the css source file
|
||||
* @param string $dir , $local relative path to the css file
|
||||
* @param boolean $local is this a local or remote asset
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function cssRewrite($file, $relative_path)
|
||||
protected function cssRewrite($file, $dir, $local)
|
||||
{
|
||||
// Strip any sourcemap comments
|
||||
$file = preg_replace(self::CSS_SOURCEMAP_REGEX, '', $file);
|
||||
|
||||
// Find any css url() elements, grab the URLs and calculate an absolute path
|
||||
// Then replace the old url with the new one
|
||||
$file = (string)preg_replace_callback(self::CSS_URL_REGEX, function ($matches) use ($relative_path) {
|
||||
$file = (string)preg_replace_callback(self::CSS_URL_REGEX, function ($matches) use ($dir, $local) {
|
||||
|
||||
$old_url = $matches[2];
|
||||
|
||||
@@ -253,7 +254,7 @@ class Pipeline extends PropertyObject
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
$new_url = $this->base_url . ltrim(Utils::normalizePath($relative_path . '/' . $old_url), '/');
|
||||
$new_url = ($local ? $this->base_url: '') . ltrim(Utils::normalizePath($dir . '/' . $old_url), '/');
|
||||
|
||||
return str_replace($old_url, $new_url, $matches[0]);
|
||||
}, $file);
|
||||
|
||||
@@ -35,18 +35,18 @@ trait AssetUtilsTrait
|
||||
/**
|
||||
* Download and concatenate the content of several links.
|
||||
*
|
||||
* @param array $links
|
||||
* @param bool $css
|
||||
* @param array $assets
|
||||
* @param bool $css
|
||||
* @param array $no_pipeline
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function gatherLinks(array $links, $css = true)
|
||||
protected function gatherLinks(array $assets, $css = true, &$no_pipeline = [])
|
||||
{
|
||||
$buffer = '';
|
||||
|
||||
|
||||
foreach ($links as $asset) {
|
||||
$relative_dir = '';
|
||||
foreach ($assets as $id => $asset) {
|
||||
$local = true;
|
||||
|
||||
$link = $asset->getAsset();
|
||||
@@ -57,9 +57,10 @@ trait AssetUtilsTrait
|
||||
if (0 === strpos($link, '//')) {
|
||||
$link = 'http:' . $link;
|
||||
}
|
||||
$relative_dir = dirname($relative_path);
|
||||
} else {
|
||||
// Fix to remove relative dir if grav is in one
|
||||
if (($this->base_url !== '/') && strpos($this->base_url, $link) === 0) {
|
||||
if (($this->base_url !== '/') && Utils::startsWith($relative_path, $this->base_url)) {
|
||||
$base_url = '#' . preg_quote($this->base_url, '#') . '#';
|
||||
$relative_path = ltrim(preg_replace($base_url, '/', $link, 1), '/');
|
||||
}
|
||||
@@ -72,6 +73,9 @@ trait AssetUtilsTrait
|
||||
|
||||
// No file found, skip it...
|
||||
if ($file === false) {
|
||||
if (!$local) { // Assume we coudln't download this file for some reason assume it's not pipeline compatible
|
||||
$no_pipeline[$id] = $asset;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -81,8 +85,8 @@ trait AssetUtilsTrait
|
||||
}
|
||||
|
||||
// If this is CSS + the file is local + rewrite enabled
|
||||
if ($css && $local && $this->css_rewrite) {
|
||||
$file = $this->cssRewrite($file, $relative_dir);
|
||||
if ($css && $this->css_rewrite) {
|
||||
$file = $this->cssRewrite($file, $relative_dir, $local);
|
||||
}
|
||||
|
||||
$file = rtrim($file) . PHP_EOL;
|
||||
|
||||
Reference in New Issue
Block a user