From ea191602da8bcb0491d66e0424fb1c7a73305d91 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 10 Jun 2021 13:16:20 +0300 Subject: [PATCH] Fixed missing styles when CSS/JS Pipeline is used and `assets/` folder is missing --- CHANGELOG.md | 2 ++ system/src/Grav/Common/Assets.php | 2 +- system/src/Grav/Common/Assets/Pipeline.php | 29 +++++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04fdb8b49..8f602a592 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * Allow to unset an asset attribute by specifying null (ie, `'defer': null`) * Support specifying custom attributes to assets in a collection [Read more](https://learn.getgrav.org/17/themes/asset-manager#collections-with-attributes?target=_blank) [#3358](https://github.com/getgrav/grav/issues/3358) * File `frontmatter.yaml` isn't part of media, ignore it +1. [](#bugfix) + * Fixed missing styles when CSS/JS Pipeline is used and `assets/` folder is missing # v1.7.16 ## 06/02/2021 diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index d2b5c890f..4273b140b 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -110,7 +110,7 @@ class Assets extends PropertyObject /** @var UniformResourceLocator $locator */ $locator = $grav['locator']; - $this->assets_dir = $locator->findResource('asset://') . DS; + $this->assets_dir = $locator->findResource('asset://'); $this->assets_url = $locator->findResource('asset://', false); $this->config($asset_config); diff --git a/system/src/Grav/Common/Assets/Pipeline.php b/system/src/Grav/Common/Assets/Pipeline.php index 7aef0e145..2e499f98f 100644 --- a/system/src/Grav/Common/Assets/Pipeline.php +++ b/system/src/Grav/Common/Assets/Pipeline.php @@ -9,9 +9,9 @@ namespace Grav\Common\Assets; -use Grav\Common\Assets\BaseAsset; use Grav\Common\Assets\Traits\AssetUtilsTrait; use Grav\Common\Config\Config; +use Grav\Common\Filesystem\Folder; use Grav\Common\Grav; use Grav\Common\Uri; use Grav\Common\Utils; @@ -88,7 +88,14 @@ class Pipeline extends PropertyObject $uri = Grav::instance()['uri']; $this->base_url = rtrim($uri->rootUrl($config->get('system.absolute_urls')), '/') . '/'; - $this->assets_dir = $locator->findResource('asset://') . DS; + $this->assets_dir = $locator->findResource('asset://'); + if (!$this->assets_dir) { + // Attempt to create assets folder if it doesn't exist yet. + $this->assets_dir = $locator->findResource('asset://', true, true); + Folder::mkdir($this->assets_dir); + $locator->clearCache(); + } + $this->assets_url = $locator->findResource('asset://', false); } @@ -119,10 +126,9 @@ class Pipeline extends PropertyObject $file = $uid . '.css'; $relative_path = "{$this->base_url}{$this->assets_url}/{$file}"; - $buffer = null; - - if (file_exists($this->assets_dir . $file)) { - $buffer = file_get_contents($this->assets_dir . $file) . "\n"; + $filepath = "{$this->assets_dir}/{$file}"; + if (file_exists($filepath)) { + $buffer = file_get_contents($filepath) . "\n"; } else { //if nothing found get out of here! if (empty($assets)) { @@ -141,7 +147,7 @@ class Pipeline extends PropertyObject // Write file if (trim($buffer) !== '') { - file_put_contents($this->assets_dir . $file, $buffer); + file_put_contents($filepath, $buffer); } } @@ -182,10 +188,9 @@ class Pipeline extends PropertyObject $file = $uid . '.js'; $relative_path = "{$this->base_url}{$this->assets_url}/{$file}"; - $buffer = null; - - if (file_exists($this->assets_dir . $file)) { - $buffer = file_get_contents($this->assets_dir . $file) . "\n"; + $filepath = "{$this->assets_dir}/{$file}"; + if (file_exists($filepath)) { + $buffer = file_get_contents($filepath) . "\n"; } else { //if nothing found get out of here! if (empty($assets)) { @@ -204,7 +209,7 @@ class Pipeline extends PropertyObject // Write file if (trim($buffer) !== '') { - file_put_contents($this->assets_dir . $file, $buffer); + file_put_contents($filepath, $buffer); } }