Skip to content

Commit

Permalink
Implement asset copy validity
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 29, 2014
1 parent adafe5b commit 6506f6a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/ClientSide/FileOrganiser.php
Expand Up @@ -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";
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -160,7 +165,7 @@ public function copyAsset() {
$hash = $this->emptyHash;
}

file_put_contents($assetWwwFingerprintFile, $hash);
file_put_contents($this->assetWwwFingerprintFile, md5($hash));

return $copyCount;
}
Expand All @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions test/Unit/ClientSide/FileOrganiser.test.php
Expand Up @@ -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() {
Expand All @@ -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)));
Expand Down

0 comments on commit 6506f6a

Please sign in to comment.