diff --git a/src/ClientSide/FileOrganiser.php b/src/ClientSide/FileOrganiser.php index 3c5dd329..4e585f9a 100644 --- a/src/ClientSide/FileOrganiser.php +++ b/src/ClientSide/FileOrganiser.php @@ -15,11 +15,15 @@ class FileOrganiser { private $response; private $manifest; private $emptyHash; +private $assetWwwFingerprintFile; public function __construct($response, Manifest $manifest) { - $this->emptyHash = str_pad("", 32, "0"); $this->response = $response; $this->manifest = $manifest; + + $wwwDir = Path::get(Path::WWW); + $this->emptyHash = str_pad("", 32, "0"); + $this->assetWwwFingerprintFile = $wwwDir . "/asset-fingerprint"; } /** @@ -95,15 +99,18 @@ public function checkAssetValid() { $wwwDir = Path::get(Path::WWW); $assetSrcDir = Path::get(Path::ASSET); $assetWwwDir = $wwwDir . "/" . substr($assetSrcDir, -strlen("asset")); - $assetWwwFingerprintFile = $wwwDir . "/asset-fingerprint"; + + if(!is_dir($assetSrcDir)) { + return true; + } if(!is_dir($assetWwwDir) - || !file_exists($assetWwwFingerprintFile)) { + || !file_exists($this->assetWwwFingerprintFile)) { return false; } // Recursive fingerprint whole source directory. - $assetWwwFingerprint = file_get_contents($assetWwwFingerprintFile); + $assetWwwFingerprint = file_get_contents($this->assetWwwFingerprintFile); $assetSrcFingerprint = $this->recursiveFingerprint($assetSrcDir); return ($assetWwwFingerprint === $assetSrcFingerprint); @@ -120,7 +127,7 @@ public function copyAsset() { $wwwDir = Path::get(Path::WWW); $assetSrcDir = Path::get(Path::ASSET); $assetWwwDir = $wwwDir . "/" . pathinfo($assetSrcDir, PATHINFO_BASENAME); - $assetWwwFingerprintFile = $wwwDir . "/asset-fingerprint"; + $this->assetWwwFingerprintFile = $wwwDir . "/asset-fingerprint"; if(!is_dir($assetSrcDir)) { return $copyCount; @@ -137,8 +144,6 @@ public function copyAsset() { $subPath = $iterator->getSubPathname(); $wwwPath = $assetWwwDir . "/" . $subPath; -// var_dump($path, $subPath, $wwwPath);die(); - if(!is_dir(dirname($wwwPath))) { mkdir(dirname($wwwPath), 0775, true); } @@ -160,7 +165,7 @@ public function copyAsset() { $hash = $this->emptyHash; } - file_put_contents($assetWwwFingerprintFile, $hash); + file_put_contents($this->assetWwwFingerprintFile, md5($hash)); return $copyCount; } @@ -187,7 +192,10 @@ private function recursiveFingerprint($dir) { \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { $path = $item->getPathname(); - $hash .= md5($path) . md5_file($path); + + if(!$item->isDir()) { + $hash .= md5($path) . md5_file($path); + } } if(strlen($hash) === 0) { diff --git a/test/Unit/ClientSide/FileOrganiser.test.php b/test/Unit/ClientSide/FileOrganiser.test.php index a88b9cc7..62812f6a 100644 --- a/test/Unit/ClientSide/FileOrganiser.test.php +++ b/test/Unit/ClientSide/FileOrganiser.test.php @@ -98,7 +98,29 @@ public function testAssetCopies() { } public function testAssetValidAfterCopy() { + $dir = $this->getPath(Path::ASSET); + $fileArray = [ + "text-file.txt", + "picture.jpg", + "directory/markdown-file.md", + "directory/another-text-file.txt", + ]; + foreach ($fileArray as $file) { + $filePath = "$dir/$file"; + if(!is_dir(dirname($filePath))) { + mkdir(dirname($filePath), 0775, true); + } + + file_put_contents($filePath, uniqid() . "\n$file\n" . uniqid() ); + } + $this->assertFalse($this->fileOrganiser->checkAssetValid(), + "assets should be invalid before copy"); + + $copyCount = $this->fileOrganiser->copyAsset(); + + $this->assertTrue($this->fileOrganiser->checkAssetValid(), + "assets should be valid after copy"); } public function testAssetInvalidatesAfterCopy() { @@ -111,6 +133,10 @@ public function testAssetCopyCreatesCorrectHash() { // Check hash is correct when source assets are copied. } +public function testAssetValidIfNoSourceDirectory() { + $this->assertTrue($this->fileOrganiser->checkAssetValid()); +} + public function testOrganiserMinifies() { $dir = $this->getPath(Path::SCRIPT); $publicDir = substr($dir, strlen(Path::get(Path::SRC)));