Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible bug when remove asset files/dirs #94

Merged
merged 10 commits into from
Jul 6, 2022
Merged
5 changes: 4 additions & 1 deletion src/AssetPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Assets;

use Exception;
use RecursiveDirectoryIterator;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Assets\Exception\InvalidConfigException;
use Yiisoft\Files\FileHelper;
Expand Down Expand Up @@ -244,7 +245,9 @@ private function hash(string $path): string
return ($this->hashCallback)($path);
}

$path = (is_file($path) ? dirname($path) : $path) . FileHelper::lastModifiedTime($path);
$dirname = is_file($path) ? dirname($path) : $path;
vjik marked this conversation as resolved.
Show resolved Hide resolved
$iterator = new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS);
$path = $dirname . FileHelper::lastModifiedTime($path) . iterator_count($iterator);
Gerych1984 marked this conversation as resolved.
Show resolved Hide resolved

return sprintf('%x', crc32($path . '|' . $this->linkAssets));
}
Expand Down
20 changes: 17 additions & 3 deletions tests/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Assets\Tests;

use RecursiveDirectoryIterator;
use RuntimeException;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Assets\AssetBundle;
Expand Down Expand Up @@ -37,6 +38,7 @@
use Yiisoft\Assets\Tests\stubs\UnicodeAsset;
use Yiisoft\Files\FileHelper;

use function array_key_first;
use function dirname;

final class AssetManagerTest extends TestCase
Expand Down Expand Up @@ -160,7 +162,11 @@ public function testGetPublishedPathLinkAssetsFalse(): void
$bundle = new SourceAsset();

$sourcePath = $this->aliases->get($bundle->sourcePath);
$hash = $this->getPublishedHash($sourcePath . FileHelper::lastModifiedTime($sourcePath), $this->publisher);
$iterator = new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS);
$hash = $this->getPublishedHash(
$sourcePath . FileHelper::lastModifiedTime($sourcePath) . iterator_count($iterator),
$this->publisher
);

$this->assertEmpty($this->getRegisteredBundles($this->manager));
$this->manager->register(SourceAsset::class);
Expand All @@ -185,7 +191,11 @@ public function testGetPublishedUrl(): void
$bundle = new SourceAsset();

$sourcePath = $this->aliases->get($bundle->sourcePath);
$hash = $this->getPublishedHash($sourcePath . FileHelper::lastModifiedTime($sourcePath), $this->publisher);
$iterator = new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS);
$hash = $this->getPublishedHash(
$sourcePath . FileHelper::lastModifiedTime($sourcePath) . iterator_count($iterator),
$this->publisher
);

$this->assertEmpty($this->getRegisteredBundles($this->manager));

Expand Down Expand Up @@ -516,7 +526,11 @@ public function testGetAssetUrl(): void
{
$bundle = new ExportAsset();
$sourcePath = $this->aliases->get($bundle->sourcePath);
$hash = $this->getPublishedHash($sourcePath . FileHelper::lastModifiedTime($sourcePath), $this->publisher);
$iterator = new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS);
$hash = $this->getPublishedHash(
$sourcePath . FileHelper::lastModifiedTime($sourcePath) . iterator_count($iterator),
$this->publisher
);

$this->assertSame(
$this->aliases->get("@assetUrl/{$hash}/css/stub.css"),
Expand Down
7 changes: 5 additions & 2 deletions tests/AssetPublisherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Assets\Tests;

use RecursiveDirectoryIterator;
use Yiisoft\Assets\AssetPublisher;
use Yiisoft\Assets\AssetPublisherInterface;
use Yiisoft\Assets\Exception\InvalidConfigException;
Expand All @@ -12,8 +13,8 @@
use Yiisoft\Files\FileHelper;
use Yiisoft\Files\PathMatcher\PathMatcher;

use function dirname;
use function crc32;
use function dirname;
use function is_file;
use function is_link;
use function sprintf;
Expand All @@ -32,8 +33,10 @@ public function testDefaultHashAndSourcesCssJsDefaultOptions(): void
$bundle = new SourceAsset();

$sourcePath = $this->aliases->get($bundle->sourcePath);
$dirname = is_file($sourcePath) ? dirname($sourcePath) : $sourcePath;
$iterator = new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS);
$hash = $this->getPublishedHash(
(is_file($sourcePath) ? dirname($sourcePath) : $sourcePath) . FileHelper::lastModifiedTime($sourcePath),
$dirname . FileHelper::lastModifiedTime($sourcePath) . iterator_count($iterator),
$this->publisher,
);

Expand Down