From 276c2f157ebefac498dc3288e261c31c2566c365 Mon Sep 17 00:00:00 2001 From: Woxxy Date: Sun, 11 Jan 2015 14:10:25 +0100 Subject: [PATCH] Replaced system calls for file handling with iterative methods in php --- classes/Foolz/Package/AssetManager.php | 42 +--------------- classes/Foolz/Package/Util.php | 67 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 40 deletions(-) diff --git a/classes/Foolz/Package/AssetManager.php b/classes/Foolz/Package/AssetManager.php index 3c85e42..347dca8 100644 --- a/classes/Foolz/Package/AssetManager.php +++ b/classes/Foolz/Package/AssetManager.php @@ -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(), '/')); } /** @@ -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); - } } \ No newline at end of file diff --git a/classes/Foolz/Package/Util.php b/classes/Foolz/Package/Util.php index 5e70021..4d24e03 100644 --- a/classes/Foolz/Package/Util.php +++ b/classes/Foolz/Package/Util.php @@ -1,6 +1,8 @@ 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); + } + } + } \ No newline at end of file