Skip to content

Commit

Permalink
Refactor file resolution
Browse files Browse the repository at this point in the history
This fixes a long file resolution error /var/html/path/to/asset.css/var/html/path/to/asset.css when using child themes and shows a nicer error for missing files
  • Loading branch information
octoberapp committed May 23, 2024
1 parent 6b1d99a commit e196770
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/Assetic/Asset/FileAsset.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace October\Rain\Assetic\Asset;

use File;
use October\Rain\Assetic\Filter\FilterInterface;
use October\Rain\Assetic\Util\VarUtils;
use InvalidArgumentException;
Expand Down Expand Up @@ -36,7 +37,7 @@ public function __construct($source, $filters = [], $sourceRoot = null, $sourceP
$sourcePath = basename($source);
}
}
elseif (null === $sourcePath) {
elseif ($sourcePath === null) {
if (strpos($source, $sourceRoot) !== 0) {
throw new InvalidArgumentException(sprintf('The source "%s" is not in the root directory "%s"', $source, $sourceRoot));
}
Expand All @@ -57,7 +58,7 @@ public function load(FilterInterface $additionalFilter = null)
$source = VarUtils::resolve($this->source, $this->getVars(), $this->getValues());

if (!is_file($source)) {
throw new RuntimeException(sprintf('The source file "%s" does not exist.', $source));
throw new RuntimeException(sprintf('The source file "%s" does not exist.', File::nicePath($source)));
}

$this->doLoad(file_get_contents($source), $additionalFilter);
Expand All @@ -71,7 +72,7 @@ public function getLastModified()
$source = VarUtils::resolve($this->source, $this->getVars(), $this->getValues());

if (!is_file($source)) {
throw new RuntimeException(sprintf('The source file "%s" does not exist.', $source));
throw new RuntimeException(sprintf('The source file "%s" does not exist.', File::nicePath($source)));
}

return filemtime($source);
Expand Down
12 changes: 7 additions & 5 deletions src/Assetic/Combiner.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php namespace October\Rain\Assetic;

use File;
use October\Rain\Assetic\Asset\FileAsset;
use October\Rain\Assetic\Asset\AssetCache;
use October\Rain\Assetic\Asset\AssetCollection;
use October\Rain\Assetic\Cache\FilesystemCache;
use File;

/**
* Combiner helper class
Expand Down Expand Up @@ -64,15 +64,17 @@ public function prepareCombiner(array $assets, $options = [])
$filesSalt = null;
foreach ($assets as $asset) {
$filters = $this->getFilters(File::extension($asset), $production);
$path = file_exists($asset)
? $asset
: (File::symbolizePath($asset, null) ?: $this->localPath . $asset);

$path = File::symbolizePath($asset);
if (!file_exists($path) && file_exists($this->localPath . $asset)) {
$path = $this->localPath . $asset;
}

$files[] = new FileAsset($path, $filters, base_path());
$filesSalt .= $this->localPath . $asset;
}
$filesSalt = md5($filesSalt);

$filesSalt = md5($filesSalt);
$collection = new AssetCollection($files, [], $filesSalt);
$collection->setTargetPath($targetPath);

Expand Down
8 changes: 4 additions & 4 deletions src/Assetic/Factory/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function generateAssetName($inputs, $filters, $options = [])
public function getLastModified(AssetInterface $asset)
{
$mtime = 0;
foreach ($asset instanceof AssetCollectionInterface ? $asset : array($asset) as $leaf) {
foreach ($asset instanceof AssetCollectionInterface ? $asset : [$asset] as $leaf) {
$mtime = max($mtime, $leaf->getLastModified());

if (!$filters = $leaf->getFilters()) {
Expand All @@ -272,7 +272,7 @@ public function getLastModified(AssetInterface $asset)
continue;
}

// extract children from leaf after running all preceeding filters
// Extract children from leaf after running all preceding filters
$clone = clone $leaf;
$clone->clearFilters();
foreach (array_slice($prevFilters, 0, -1) as $prevFilter) {
Expand All @@ -298,7 +298,7 @@ public function getLastModified(AssetInterface $asset)
* * A glob: If the string contains a "*" it will be interpreted as a glob
* * A path: Otherwise the string is interpreted as a filesystem path
*
* Both globs and paths will be absolutized using the current root directory.
* Both globs and paths will be absolute using the current root directory.
*
* @param string $input An input string
* @param array $options An array of options
Expand Down Expand Up @@ -384,7 +384,7 @@ private static function isAbsolutePath($path)
}

/**
* Loops through the root directories and returns the first match.
* findRootDir loops through the root directories and returns the first match.
*
* @param string $path An absolute path
* @param array $roots An array of root directories
Expand Down
13 changes: 10 additions & 3 deletions src/Assetic/Traits/HasDeepHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ public function getDeepHashFromAssets($assets)
{
$key = '';

$assetFiles = array_map(function ($file) {
return file_exists($file) ? $file : (File::symbolizePath($file, null) ?: $this->localPath . $file);
}, $assets);
$assetFiles = [];
foreach ($assets as $file) {
$path = File::symbolizePath($file);
if (file_exists($path)) {
$assetFiles[] = $path;
}
elseif (file_exists($this->localPath . $path)) {
$assetFiles[] = $this->localPath . $path;
}
}

foreach ($assetFiles as $file) {
$filters = $this->getFilters(File::extension($file));
Expand Down

0 comments on commit e196770

Please sign in to comment.