From 098fc10db374bdd0ac7d0e1ca2e6576e471674f1 Mon Sep 17 00:00:00 2001 From: Mubashar Ahmad Date: Tue, 17 May 2022 21:00:56 +0500 Subject: [PATCH] no message --- composer.json | 8 +- src/Zip.php | 383 +++++++++++++++---------------------- src/ZipFacade.php | 3 +- src/ZipManager.php | 244 +++++++++++------------ src/ZipServiceProvider.php | 16 +- 5 files changed, 278 insertions(+), 376 deletions(-) diff --git a/composer.json b/composer.json index c0b505b..c2f4445 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "keywords": [ "laravel-zip", "laravel", - "laravel5", + "laravel8", + "laravel9", "zip", "unzip", "extract", @@ -23,8 +24,9 @@ "merge" ], "require": { - "php": ">=7.1", - "illuminate/support": "^6.0|^7.0|^8.0" + "php": ">=8.0", + "illuminate/support": "^6.0|^7.0|^8.0|^9.0", + "ext-zip": "*" }, "autoload": { "psr-4": { diff --git a/src/Zip.php b/src/Zip.php index 6c3deb8..4c17697 100644 --- a/src/Zip.php +++ b/src/Zip.php @@ -1,4 +1,6 @@ - 'No error', - ZipArchive::ER_MULTIDISK => 'Multi-disk zip archives not supported', - ZipArchive::ER_RENAME => 'Renaming temporary file failed', - ZipArchive::ER_CLOSE => 'Closing zip archive failed', - ZipArchive::ER_SEEK => 'Seek error', - ZipArchive::ER_READ => 'Read error', - ZipArchive::ER_WRITE => 'Write error', - ZipArchive::ER_CRC => 'CRC error', - ZipArchive::ER_ZIPCLOSED => 'Containing zip archive was closed', - ZipArchive::ER_NOENT => 'No such file', - ZipArchive::ER_EXISTS => 'File already exists', - ZipArchive::ER_OPEN => 'Can\'t open file', - ZipArchive::ER_TMPOPEN => 'Failure to create temporary file', - ZipArchive::ER_ZLIB => 'Zlib error', - ZipArchive::ER_MEMORY => 'Malloc failure', - ZipArchive::ER_CHANGED => 'Entry has been changed', + private static array $zip_status_codes = [ + ZipArchive::ER_OK => 'No error', + ZipArchive::ER_MULTIDISK => 'Multi-disk zip archives not supported', + ZipArchive::ER_RENAME => 'Renaming temporary file failed', + ZipArchive::ER_CLOSE => 'Closing zip archive failed', + ZipArchive::ER_SEEK => 'Seek error', + ZipArchive::ER_READ => 'Read error', + ZipArchive::ER_WRITE => 'Write error', + ZipArchive::ER_CRC => 'CRC error', + ZipArchive::ER_ZIPCLOSED => 'Containing zip archive was closed', + ZipArchive::ER_NOENT => 'No such file', + ZipArchive::ER_EXISTS => 'File already exists', + ZipArchive::ER_OPEN => 'Can\'t open file', + ZipArchive::ER_TMPOPEN => 'Failure to create temporary file', + ZipArchive::ER_ZLIB => 'Zlib error', + ZipArchive::ER_MEMORY => 'Malloc failure', + ZipArchive::ER_CHANGED => 'Entry has been changed', ZipArchive::ER_COMPNOTSUPP => 'Compression method not supported', - ZipArchive::ER_EOF => 'Premature EOF', - ZipArchive::ER_INVAL => 'Invalid argument', - ZipArchive::ER_NOZIP => 'Not a zip archive', - ZipArchive::ER_INTERNAL => 'Internal error', - ZipArchive::ER_INCONS => 'Zip archive inconsistent', - ZipArchive::ER_REMOVE => 'Can\'t remove file', - ZipArchive::ER_DELETED => 'Entry has been deleted' - ); + ZipArchive::ER_EOF => 'Premature EOF', + ZipArchive::ER_INVAL => 'Invalid argument', + ZipArchive::ER_NOZIP => 'Not a zip archive', + ZipArchive::ER_INTERNAL => 'Internal error', + ZipArchive::ER_INCONS => 'Zip archive inconsistent', + ZipArchive::ER_REMOVE => 'Can\'t remove file', + ZipArchive::ER_DELETED => 'Entry has been deleted' + ]; /** * Class constructor * - * @param string $zip_file ZIP file name + * @param string $zip_file ZIP file name * */ - public function __construct($zip_file) { - + public function __construct($zip_file) + { if (empty($zip_file)) { throw new \Exception(self::getStatus(ZipArchive::ER_NOENT)); } $this->zip_file = $zip_file; - } /** * Open a zip archive * - * @param string $zip_file ZIP file name + * @param string $zip_file ZIP file name * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public static function open($zip_file) { - + public static function open($zip_file) + { try { - $zip = new Zip($zip_file); $zip->setArchive(self::openZipFile($zip_file)); - } catch (\Exception $ze) { - throw $ze; - } return $zip; - } /** * Check a zip archive * - * @param string $zip_file ZIP file name + * @param string $zip_file ZIP file name * * @return bool */ - public static function check($zip_file) { - + public static function check($zip_file) + { try { - $zip = self::openZipFile($zip_file, ZipArchive::CHECKCONS); $zip->close(); - } catch (Exception $ze) { - throw $ze; - } return true; - } /** * Create a new zip archive * - * @param string $zip_file ZIP file name - * @param bool $overwrite overwrite existing file (if any) + * @param string $zip_file ZIP file name + * @param bool $overwrite overwrite existing file (if any) * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public static function create($zip_file, $overwrite = false) { - - $overwrite = filter_var($overwrite, FILTER_VALIDATE_BOOLEAN, array( - "options" => array( - "default" => false - ) - )); + public static function create($zip_file, $overwrite = false) + { + $overwrite = filter_var($overwrite, FILTER_VALIDATE_BOOLEAN, [ + 'options' => [ + 'default' => false + ] + ]); try { - $zip = new Zip($zip_file); if ($overwrite) { @@ -187,36 +178,31 @@ public static function create($zip_file, $overwrite = false) { } else { $zip->setArchive(self::openZipFile($zip_file, ZipArchive::CREATE)); } - } catch (Exception $ze) { - throw $ze; - } return $zip; - } /** * Set files to skip * - * @param string $mode [HIDDEN, ZANYSOFT, ALL, NONE] + * @param string $mode [HIDDEN, ZANYSOFT, ALL, NONE] * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setSkipped($mode) { - + final public function setSkipped($mode) + { $mode = strtoupper($mode); if (!in_array($mode, $this->supported_skip_modes)) { - throw new Exception("Unsupported skip mode"); + throw new Exception('Unsupported skip mode'); } $this->skip_mode = $mode; return $this; - } /** @@ -224,25 +210,23 @@ final public function setSkipped($mode) { * * @return string */ - final public function getSkipped() { - + final public function getSkipped() + { return $this->skip_mode; - } /** * Set extraction password * - * @param string $password + * @param string $password * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setPassword($password) { - + final public function setPassword($password) + { $this->password = $password; return $this; - } /** @@ -250,29 +234,27 @@ final public function setPassword($password) { * * @return string */ - final public function getPassword() { - + final public function getPassword() + { return $this->password; - } /** * Set current base path (just to add relative files to zip archive) * - * @param string $path + * @param string $path * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setPath($path) { - + final public function setPath($path) + { if (!file_exists($path)) { - throw new Exception("Not existent path"); + throw new Exception('Not existent path'); } - $this->path = $path[strlen($path) - 1] == "/" ? $path : $path . "/"; + $this->path = $path[strlen($path) - 1] == '/' ? $path : $path . '/'; return $this; - } /** @@ -280,32 +262,30 @@ final public function setPath($path) { * * @return string */ - final public function getPath() { - + final public function getPath() + { return $this->path; - } /** * Set extraction folder mask * - * @param int $mask + * @param int $mask * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setMask($mask) { - - $mask = filter_var($mask, FILTER_VALIDATE_INT, array( - "options" => array( - "max_range" => 0777, - "default" => 0777 - ), 'flags' => FILTER_FLAG_ALLOW_OCTAL - )); + final public function setMask($mask) + { + $mask = filter_var($mask, FILTER_VALIDATE_INT, [ + 'options' => [ + 'max_range' => 0777, + 'default' => 0777 + ], 'flags' => FILTER_FLAG_ALLOW_OCTAL + ]); $this->mask = $mask; return $this; - } /** @@ -313,25 +293,23 @@ final public function setMask($mask) { * * @return int */ - final public function getMask() { - + final public function getMask() + { return $this->mask; - } /** * Set the current ZipArchive object * - * @param \ZipArchive $zip + * @param \ZipArchive $zip * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setArchive(ZipArchive $zip) { - + final public function setArchive(ZipArchive $zip) + { $this->zip_archive = $zip; return $this; - } /** @@ -339,10 +317,9 @@ final public function setArchive(ZipArchive $zip) { * * @return \ZipArchive */ - final public function getArchive() { - + final public function getArchive() + { return $this->zip_archive; - } /** @@ -350,17 +327,17 @@ final public function getArchive() { * * @return string */ - final public function getZipFile() { - + final public function getZipFile() + { return $this->zip_file; - } /** * Get an SplFileObject for the zip file - * @return SplFileObject + * @return \SplFileObject */ - public function getFileObject() { + public function getFileObject() + { return new \SplFileObject($this->zip_file); } @@ -369,12 +346,11 @@ public function getFileObject() { * * @return array */ - public function listFiles() { - - $list = Array(); + public function listFiles() + { + $list = []; for ($i = 0; $i < $this->zip_archive->numFiles; $i++) { - $name = $this->zip_archive->getNameIndex($i); if ($name === false) { @@ -382,22 +358,21 @@ public function listFiles() { } array_push($list, $name); - } return $list; - } /** * Check if zip archive has a file * - * @param string $file File - * @param int $flags (optional) ZipArchive::FL_NOCASE, ZipArchive::FL_NODIR seperated by bitwise OR + * @param string $file File + * @param int $flags (optional) ZipArchive::FL_NOCASE, ZipArchive::FL_NODIR seperated by bitwise OR * * @return bool */ - public function has($file, $flags = 0) { + public function has($file, $flags = 0) + { if (empty($file)) { throw new Exception('Invalid File'); } @@ -408,19 +383,18 @@ public function has($file, $flags = 0) { /** * Extract files from zip archive * - * @param string $destination Destination path - * @param mixed $files (optional) a filename or an array of filenames + * @param string $destination Destination path + * @param mixed $files (optional) a filename or an array of filenames * * @return bool */ - public function extract($destination, $files = null) { - + public function extract($destination, $files = null) + { if (empty($destination)) { throw new Exception('Invalid destination path'); } if (!file_exists($destination)) { - $omask = umask(0); $action = mkdir($destination, $this->mask, true); @@ -428,23 +402,18 @@ public function extract($destination, $files = null) { umask($omask); if ($action === false) { - throw new Exception("Error creating folder " . $destination); + throw new Exception('Error creating folder ' . $destination); } - } if (!is_writable($destination)) { throw new Exception('Destination path not writable'); } - if (is_array($files) && @sizeof($files) != 0) { - + if (is_array($files) && count($files) != 0) { $file_matrix = $files; - } else { - $file_matrix = $this->getArchiveFiles(); - } if (!empty($this->password)) { @@ -458,84 +427,69 @@ public function extract($destination, $files = null) { } return true; - } /** * Add files to zip archive * - * @param mixed $file_name_or_array filename to add or an array of filenames - * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not + * @param mixed $file_name_or_array filename to add or an array of filenames + * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public function add($file_name_or_array, $flatten_root_folder = false) { - + public function add($file_name_or_array, $flatten_root_folder = false) + { if (empty($file_name_or_array)) { throw new Exception(self::getStatus(ZipArchive::ER_NOENT)); } - $flatten_root_folder = filter_var($flatten_root_folder, FILTER_VALIDATE_BOOLEAN, array( - "options" => array( - "default" => false - ) - )); + $flatten_root_folder = filter_var($flatten_root_folder, FILTER_VALIDATE_BOOLEAN, [ + 'options' => [ + 'default' => false + ] + ]); try { - if (is_array($file_name_or_array)) { - foreach ($file_name_or_array as $file_name) { $this->addItem($file_name, $flatten_root_folder); } - } else { $this->addItem($file_name_or_array, $flatten_root_folder); } - } catch (Exception $ze) { - throw $ze; - } return $this; - } /** * Delete files from zip archive * - * @param mixed $file_name_or_array filename to delete or an array of filenames + * @param mixed $file_name_or_array filename to delete or an array of filenames * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public function delete($file_name_or_array) { - + public function delete($file_name_or_array) + { if (empty($file_name_or_array)) { throw new Exception(self::getStatus(ZipArchive::ER_NOENT)); } try { - if (is_array($file_name_or_array)) { - foreach ($file_name_or_array as $file_name) { $this->deleteItem($file_name); } - } else { $this->deleteItem($file_name_or_array); } - } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -543,14 +497,13 @@ public function delete($file_name_or_array) { * * @return bool */ - public function close() { - + public function close() + { if ($this->zip_archive->close() === false) { throw new Exception(self::getStatus($this->zip_archive->status)); } return true; - } /** @@ -558,12 +511,11 @@ public function close() { * * @return array */ - private function getArchiveFiles() { - - $list = array(); + private function getArchiveFiles() + { + $list = []; for ($i = 0; $i < $this->zip_archive->numFiles; $i++) { - $file = $this->zip_archive->statIndex($i); if ($file === false) { @@ -572,32 +524,31 @@ private function getArchiveFiles() { $name = str_replace('\\', '/', $file['name']); - if ($name[0] == "." AND in_array($this->skip_mode, array("HIDDEN", "ALL"))) { + if ($name[0] == '.' and in_array($this->skip_mode, ['HIDDEN', 'ALL'])) { continue; } - if ($name[0] == "." AND @$name[1] == "_" AND in_array($this->skip_mode, array("ZANYSOFT", "ALL"))) { + if ($name[0] == '.' and @$name[1] == '_' and in_array($this->skip_mode, ['ZANYSOFT', 'ALL'])) { continue; } array_push($list, $name); - } return $list; - } /** * Add item to zip archive * - * @param string $file File to add (realpath) - * @param bool $flatroot (optional) If true, source directory will be not included - * @param string $base (optional) Base to record in zip file + * @param string $file File to add (realpath) + * @param bool $flatroot (optional) If true, source directory will be not included + * @param string|null $base (optional) Base to record in zip file * + * @throws Exception */ - private function addItem($file, $flatroot = false, $base = null) { - + private function addItem(string $file, bool $flatroot = false, string $base = null): void + { $file = is_null($this->path) ? $file : $this->path . $file; $real_file = str_replace('\\', '/', realpath($file)); @@ -605,21 +556,17 @@ private function addItem($file, $flatroot = false, $base = null) { $real_name = basename($real_file); if (!is_null($base)) { - - if ($real_name[0] == "." AND in_array($this->skip_mode, array("HIDDEN", "ALL"))) { + if ($real_name[0] == '.' and in_array($this->skip_mode, ['HIDDEN', 'ALL'])) { return; } - if ($real_name[0] == "." AND @$real_name[1] == "_" AND in_array($this->skip_mode, array("ZANYSOFT", "ALL"))) { + if ($real_name[0] == '.' and @$real_name[1] == '_' and in_array($this->skip_mode, ['ZANYSOFT', 'ALL'])) { return; } - } if (is_dir($real_file)) { - if (!$flatroot) { - $folder_target = is_null($base) ? $real_name : $base . $real_name; $new_folder = $this->zip_archive->addEmptyDir($folder_target); @@ -627,37 +574,26 @@ private function addItem($file, $flatroot = false, $base = null) { if ($new_folder === false) { throw new Exception(self::getStatus($this->zip_archive->status)); } - } else { - $folder_target = null; - } foreach (new \DirectoryIterator($real_file) as $path) { - if ($path->isDot()) { continue; } $file_real = $path->getPathname(); - $base = is_null($folder_target) ? null : ($folder_target . "/"); + $base = is_null($folder_target) ? null : ($folder_target . '/'); try { - $this->addItem($file_real, false, $base); - } catch (Exception $ze) { - throw $ze; - } - } - } else if (is_file($real_file)) { - $file_target = is_null($base) ? $real_name : $base . $real_name; $add_file = $this->zip_archive->addFile($real_file, $file_target); @@ -665,39 +601,36 @@ private function addItem($file, $flatroot = false, $base = null) { if ($add_file === false) { throw new Exception(self::getStatus($this->zip_archive->status)); } - } else { return; } - } /** * Delete item from zip archive * - * @param string $file File to delete (zippath) + * @param string $file File to delete (zippath) * */ - private function deleteItem($file) { - + private function deleteItem($file) + { $deleted = $this->zip_archive->deleteName($file); if ($deleted === false) { throw new \Exception(self::getStatus($this->zip_archive->status)); } - } /** * Open a zip file * - * @param string $zip_file ZIP status code - * @param int $flags ZIP status code + * @param string $zip_file ZIP status code + * @param int $flags ZIP status code * * @return \ZipArchive */ - private static function openZipFile($zip_file, $flags = null) { - + private static function openZipFile($zip_file, $flags = null) + { $zip = new ZipArchive(); $open = $zip->open($zip_file, $flags); @@ -707,23 +640,21 @@ private static function openZipFile($zip_file, $flags = null) { } return $zip; - } /** * Get status from zip status code * - * @param int $code ZIP status code + * @param int $code ZIP status code * * @return string */ - private static function getStatus($code) { - + private static function getStatus(int $code): string + { if (array_key_exists($code, self::$zip_status_codes)) { return self::$zip_status_codes[$code]; } else { return sprintf('Unknown status %s', $code); } - } } diff --git a/src/ZipFacade.php b/src/ZipFacade.php index e856df3..e3f1763 100644 --- a/src/ZipFacade.php +++ b/src/ZipFacade.php @@ -1,4 +1,5 @@ zip_archives[] = $zip; return $this; - } /** * Remove a \Coodojo\Zip\Zip object from manager * - * @param \ZanySoft\Zip\Zip $zip + * @param \ZanySoft\Zip\Zip $zip * * @return \ZanySoft\Zip\ZipManager */ - public function removeZip(\ZanySoft\Zip\Zip $zip) { - + public function removeZip(\ZanySoft\Zip\Zip $zip) + { $archive_key = array_search($zip, $this->zip_archives, true); - if ( $archive_key === false ) throw new Exception("Archive not found"); + if ($archive_key === false) { + throw new Exception('Archive not found'); + } unset($this->zip_archives[$archive_key]); return $this; - } /** @@ -60,53 +62,52 @@ public function removeZip(\ZanySoft\Zip\Zip $zip) { * * @return array */ - public function listZips() { - - $list = array(); + public function listZips(): array + { + $list = []; - foreach ( $this->zip_archives as $key=>$archive ) $list[$key] = $archive->getZipFile(); + foreach ($this->zip_archives as $key => $archive) { + $list[$key] = $archive->getZipFile(); + } return $list; - } /** * Get a a \Coodojo\Zip\Zip object * - * @param int $zipId The zip id from self::listZips() + * @param int $zipId The zip id from self::listZips() * * @return \ZanySoft\Zip\Zip */ - public function getZip($zipId) { - - if ( array_key_exists($zipId, $this->zip_archives) === false ) throw new Exception("Archive not found"); + public function getZip($zipId) + { + if (array_key_exists($zipId, $this->zip_archives) === false) { + throw new Exception('Archive not found'); + } return $this->zip_archives[$zipId]; - } /** * Set current base path (just to add relative files to zip archive) * for all zip files * - * @param string $path + * @param string $path * * @return \ZanySoft\Zip\ZipManager */ - public function setPath($path) { - + public function setPath($path) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->setPath($path); - + foreach ($this->zip_archives as $archive) { + $archive->setPath($path); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -114,37 +115,35 @@ public function setPath($path) { * * @return array */ - public function getPath() { - - $paths = array(); + public function getPath() + { + $paths = []; - foreach ( $this->zip_archives as $key=>$archive ) $paths[$key] = $archive->getPath(); + foreach ($this->zip_archives as $key => $archive) { + $paths[$key] = $archive->getPath(); + } return $paths; - } /** * Set default file mask for all Zips * - * @param int $mask + * @param int $mask * * @return \ZanySoft\Zip\ZipManager */ - public function setMask($mask) { - + public function setMask($mask) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->setMask($mask); - + foreach ($this->zip_archives as $archive) { + $archive->setMask($mask); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -152,14 +151,15 @@ public function setMask($mask) { * * @return array */ - public function getMask() { + public function getMask() + { + $masks = []; - $masks = array(); - - foreach ( $this->zip_archives as $key=>$archive ) $masks[$key] = $archive->getMask(); + foreach ($this->zip_archives as $key => $archive) { + $masks[$key] = $archive->getMask(); + } return $masks; - } /** @@ -167,75 +167,64 @@ public function getMask() { * * @return array */ - public function listFiles() { - - $files = array(); + public function listFiles() + { + $files = []; try { - - foreach ( $this->zip_archives as $key=>$archive ) $files[$key] = $archive->listFiles(); - + foreach ($this->zip_archives as $key => $archive) { + $files[$key] = $archive->listFiles(); + } } catch (Exception $ze) { - throw $ze; - } return $files; - } /** * Extract Zips to common destination * - * @param string $destination Destination path - * @param bool $separate Specify if files should be placed in different directories - * @param array $files Array of files to extract + * @param string $destination Destination path + * @param bool $separate Specify if files should be placed in different directories + * @param array $files Array of files to extract * * @return bool */ - public function extract($destination, $separate = true, $files = null) { - + public function extract($destination, $separate = true, $files = null) + { try { - - foreach ( $this->zip_archives as $archive ) { - - $local_path = substr($destination, -1) == '/' ? $destination : $destination.'/'; + foreach ($this->zip_archives as $archive) { + $local_path = substr($destination, -1) == '/' ? $destination : $destination . '/'; $local_file = pathinfo($archive->getZipFile()); - $local_destination = $separate ? ($local_path.$local_file['filename']) : $destination; + $local_destination = $separate ? ($local_path . $local_file['filename']) : $destination; $archive->extract($local_destination, $files = null); - } - } catch (Exception $ze) { - throw $ze; - } return true; - } /** * Merge multiple Zips into one * - * @param string $output_zip_file Destination zip - * @param bool $separate Specify if files should be placed in different directories + * @param string $output_zip_file Destination zip + * @param bool $separate Specify if files should be placed in different directories * * @return bool */ - public function merge($output_zip_file, $separate = true) { - + public function merge($output_zip_file, $separate = true) + { $pathinfo = pathinfo($output_zip_file); - $temporary_folder = $pathinfo['dirname']."/".self::getTemporaryFolder(); + $temporary_folder = $pathinfo['dirname'] . '/' . self::getTemporaryFolder(); try { - $this->extract($temporary_folder, $separate, null); $zip = Zip::create($output_zip_file); @@ -243,66 +232,52 @@ public function merge($output_zip_file, $separate = true) { $zip->add($temporary_folder, true)->close(); self::recursiveUnlink($temporary_folder); - - } catch (Exception $ze) { - - throw $ze; - } catch (Exception $e) { - throw $e; - } return true; - } /** * Add a file to zip * - * @param mixed $file_name_or_array filename to add or an array of filenames - * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not + * @param mixed $file_name_or_array filename to add or an array of filenames + * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not * * @return \ZanySoft\Zip\ZipManager */ - public function add($file_name_or_array, $flatten_root_folder = false) { - + public function add($file_name_or_array, $flatten_root_folder = false) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->add($file_name_or_array, $flatten_root_folder); - + foreach ($this->zip_archives as $archive) { + $archive->add($file_name_or_array, $flatten_root_folder); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** * Delete a file from Zips * - * @param mixed $file_name_or_array filename to add or an array of filenames + * @param mixed $file_name_or_array filename to add or an array of filenames * * @return \ZanySoft\Zip\ZipManager */ - public function delete($file_name_or_array) { - + public function delete($file_name_or_array) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->delete($file_name_or_array); - + foreach ($this->zip_archives as $archive) { + $archive->delete($file_name_or_array); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -310,63 +285,58 @@ public function delete($file_name_or_array) { * * @return bool */ - public function close() { - + public function close() + { try { - - foreach ( $this->zip_archives as $archive ) $archive->close(); - + foreach ($this->zip_archives as $archive) { + $archive->close(); + } } catch (Exception $ze) { - throw $ze; - } return true; - } - private static function removeExtension($filename) { - + /** + * @param $filename + * @return mixed|string + */ + private static function removeExtension($filename) + { $file_info = pathinfo($filename); return $file_info['filename']; - } - private static function getTemporaryFolder() { - - return "zip-temp-folder-".md5(uniqid(rand(), true), 0); - + private static function getTemporaryFolder() + { + return 'zip-temp-folder-' . md5(uniqid((string)rand(), true), false); } /** * @param string $folder */ - private static function recursiveUnlink($folder) { - - foreach ( new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path ) { - + private static function recursiveUnlink($folder) + { + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) { $pathname = $path->getPathname(); - if ( $path->isDir() ) { - + if ($path->isDir()) { $action = rmdir($pathname); - } else { - $action = unlink($pathname); - } - if ( $action === false ) throw new Exception("Error deleting ".$pathname." during recursive unlink of folder ".$folder); - + if ($action === false) { + throw new Exception('Error deleting ' . $pathname . ' during recursive unlink of folder ' . $folder); + } } $action = rmdir($folder); - if ( $action === false ) throw new Exception("Error deleting folder ".$folder); - + if ($action === false) { + throw new Exception('Error deleting folder ' . $folder); + } } - } diff --git a/src/ZipServiceProvider.php b/src/ZipServiceProvider.php index 9ea8d14..3a7da02 100644 --- a/src/ZipServiceProvider.php +++ b/src/ZipServiceProvider.php @@ -4,15 +4,15 @@ use Illuminate\Support\ServiceProvider; -class ZipServiceProvider extends ServiceProvider { - - +class ZipServiceProvider extends ServiceProvider +{ /** * Register the service provider. * * @return void */ - public function register() { + public function register() + { $this->registerCpanelService(); /*if ($this->app->runningInConsole()) { @@ -25,7 +25,8 @@ public function register() { * * @return void */ - public function registerCpanelService() { + public function registerCpanelService() + { $this->app->singleton('zip', function ($app) { return new Zip($app); }); @@ -54,7 +55,4 @@ protected function isLumen() { return str_contains($this->app->version(), 'Lumen') === true; } - -} - -?> \ No newline at end of file +} \ No newline at end of file