Skip to content

Commit

Permalink
Implement and test FileOrganiser asset copy functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 29, 2014
1 parent 3d7e95a commit adafe5b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/ClientSide/FileOrganiser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class FileOrganiser {

private $response;
private $manifest;
private $emptyHash;

public function __construct($response, Manifest $manifest) {
$this->emptyHash = str_pad("", 32, "0");
$this->response = $response;
$this->manifest = $manifest;
}
Expand Down Expand Up @@ -100,7 +102,6 @@ public function checkAssetValid() {
return false;
}


// Recursive fingerprint whole source directory.
$assetWwwFingerprint = file_get_contents($assetWwwFingerprintFile);
$assetSrcFingerprint = $this->recursiveFingerprint($assetSrcDir);
Expand All @@ -115,6 +116,52 @@ public function checkAssetValid() {
*/
public function copyAsset() {
$copyCount = 0;

$wwwDir = Path::get(Path::WWW);
$assetSrcDir = Path::get(Path::ASSET);
$assetWwwDir = $wwwDir . "/" . pathinfo($assetSrcDir, PATHINFO_BASENAME);
$assetWwwFingerprintFile = $wwwDir . "/asset-fingerprint";

if(!is_dir($assetSrcDir)) {
return $copyCount;
}

$hash = "";

// TODO: Refactor into Core/DirectoryWalker
foreach ($iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($assetSrcDir,
\RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item) {
$path = $item->getPathname();
$subPath = $iterator->getSubPathname();
$wwwPath = $assetWwwDir . "/" . $subPath;

// var_dump($path, $subPath, $wwwPath);die();

if(!is_dir(dirname($wwwPath))) {
mkdir(dirname($wwwPath), 0775, true);
}

if(!$item->isDir()) {
$hash .= md5($path) . md5_file($path);

if(copy($path, $wwwPath)) {
$copyCount++;
}
else {
// TODO: Handle exception.
}
}

}

if(strlen($hash) === 0) {
$hash = $this->emptyHash;
}

file_put_contents($assetWwwFingerprintFile, $hash);

return $copyCount;
}

Expand All @@ -128,23 +175,23 @@ public function copyAsset() {
* indicating an empty or non-existant directory
*/
private function recursiveFingerprint($dir) {
$emptyHash = str_pad("", 32, "0");
$hash = "";

if(!is_dir($dir)) {
return $emptyHash;
return $this->emptyHash;
}

// TODO: Refactor into Core/DirectoryWalker
foreach ($iterator = new \RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $item) {
new \RecursiveDirectoryIterator($dir,
\RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item) {
$path = $item->getPathname();
$hash .= md5($path) . md5_file($path);
}

if(strlen($hash) === 0) {
return $emptyHash;
return $this->emptyHash;
}

return md5($hash);
Expand Down
6 changes: 6 additions & 0 deletions test/Unit/ClientSide/FileOrganiser.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public function testAssetInvalidatesAfterCopy() {

}

public function testAssetCopyCreatesCorrectHash() {
// Check hash is 0000000 when no source assets.

// Check hash is correct when source assets are copied.
}

public function testOrganiserMinifies() {
$dir = $this->getPath(Path::SCRIPT);
$publicDir = substr($dir, strlen(Path::get(Path::SRC)));
Expand Down

0 comments on commit adafe5b

Please sign in to comment.