factory.php
100 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Archives helper class. |
| 16 | * |
| 17 | * @since 1.7.1 |
| 18 | */ |
| 19 | abstract class VAPArchiveFactory |
| 20 | { |
| 21 | /** |
| 22 | * Extracts the file from a package. |
| 23 | * |
| 24 | * @param string $source The path of the package. |
| 25 | * @param string $destination The folder in which the files should be extracted. |
| 26 | * |
| 27 | * @return bool True on success, false otherwise. |
| 28 | * |
| 29 | * @throws Exception |
| 30 | */ |
| 31 | public static function extract($source, $destination) |
| 32 | { |
| 33 | return static::getInstance($source)->extract($source, $destination); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Compresses the specified folder into an archive. |
| 38 | * |
| 39 | * @param string $source The path of the folder to compress. |
| 40 | * @param string $destination The path of the resulting archive. |
| 41 | * |
| 42 | * @return bool True on success, false otherwise. |
| 43 | * |
| 44 | * @throws Exception |
| 45 | */ |
| 46 | public static function compress($source, $destination) |
| 47 | { |
| 48 | return static::getInstance($destination)->compress($source, $destination); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Downloads the specified archive. |
| 53 | * |
| 54 | * @param string $source The path of the folder to compress. |
| 55 | * |
| 56 | * @return void |
| 57 | * |
| 58 | * @since 1.7.1 |
| 59 | * |
| 60 | * @throws Exception |
| 61 | */ |
| 62 | public static function download($source) |
| 63 | { |
| 64 | static::getInstance($source)->download($source); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a new instance of the specified archive handler. |
| 69 | * |
| 70 | * @param string $type Either the archive type or a path, from which |
| 71 | * the file type will be extracted. |
| 72 | * |
| 73 | * @return VAPArchiveType |
| 74 | * |
| 75 | * @throws Exception |
| 76 | */ |
| 77 | protected static function getInstance($type) |
| 78 | { |
| 79 | // check if we have a path |
| 80 | if (preg_match("/[\/\\\\]/", $type)) |
| 81 | { |
| 82 | // extract file type from path |
| 83 | $type = pathinfo($type, PATHINFO_EXTENSION); |
| 84 | } |
| 85 | |
| 86 | switch (strtolower($type)) |
| 87 | { |
| 88 | case 'zip': |
| 89 | |
| 90 | // create a new ZIP archive instance |
| 91 | VAPLoader::import('libraries.archive.type.zip'); |
| 92 | return new VAPArchiveTypeZip; |
| 93 | |
| 94 | default: |
| 95 | // invalid archive type |
| 96 | throw new Exception(sprintf('Archive [%s] not supported', $type), 500); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |