Skip to content

Commit

Permalink
Replaced system calls for file handling with iterative methods in php
Browse files Browse the repository at this point in the history
  • Loading branch information
woxxy committed Jan 11, 2015
1 parent 9d811d3 commit 276c2f1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 40 deletions.
42 changes: 2 additions & 40 deletions classes/Foolz/Package/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ protected function loadAssets()
mkdir($this->getPublicDir(), 0777, true);
}

// damned copy doesn't work with directories
//copy($this->getPackage()->getDir().'assets', $this->getPublicDir());
system('cp -R '.$this->getPackage()->getDir().'assets/*'.' '.$this->getPublicDir());
Util::copy_recursive($this->getPackage()->getDir().'assets', rtrim($this->getPublicDir(), '/'));
}

/**
Expand All @@ -130,45 +128,9 @@ public function clearAssets()
// get it just right out of the assets folder
if (file_exists($this->public_dir.$this->getPackage()->getConfig('name')))
{
static::flushDir($this->public_dir.$this->getPackage()->getConfig('name'));
Util::delete_recursive($this->public_dir.$this->getPackage()->getConfig('name'));
}

return $this;
}

/**
* Empties a directory
*
* @param string $path The directory to empty
*/
protected static function flushDir($path)
{
$fp = opendir($path);

while (false !== ($file = readdir($fp)))
{
// Remove '.', '..'
if (in_array($file, array('.', '..')))
{
continue;
}

$filepath = $path.'/'.$file;

if (is_dir($filepath))
{
static::flushDir($filepath);

// removing dir here won't remove the root dir, just as we want it
rmdir($filepath);
continue;
}
elseif (is_file($filepath))
{
unlink($filepath);
}
}

closedir($fp);
}
}
67 changes: 67 additions & 0 deletions classes/Foolz/Package/Util.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Foolz\Package;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
* Collection of utilities used in Foolz\Package
Expand Down Expand Up @@ -60,4 +62,69 @@ public static function saveArrayToFile($path, $array)
file_put_contents($path, $content);
}

/**
* Delete a file/recursively delete a directory
*
* NOTE: Be very careful with the path you pass to this!
*
* From: http://davidhancock.co/2012/11/useful-php-functions-for-dealing-with-the-file-system/
*
* @param string $path The path to the file/directory to delete
* @return void
*/
public static function delete_recursive($path)
{
if (is_dir($path)) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($iterator as $file) {
if ($file->isDir()) {
rmdir($file->getPathname());
}
else {
unlink($file->getPathname());
}
}

rmdir($path);
}
else {
unlink($path);
}
}

/**
* Copy a file or recursively copy a directories contents
*
* From: http://davidhancock.co/2012/11/useful-php-functions-for-dealing-with-the-file-system/
*
* @param string $source The path to the source file/directory
* @param string $dest The path to the destination directory
* @return void
*/
public static function copy_recursive($source, $dest)
{
if (is_dir($source)) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $file) {
if ($file->isDir()) {
mkdir($dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
}
else {
copy($file, $dest.DIRECTORY_SEPARATOR.$iterator->getSubPathName());
}
}
}
else {
copy($source, $dest);
}
}

}

0 comments on commit 276c2f1

Please sign in to comment.