Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
36 / 36 |
| ZipGenerator | |
100.00% |
1 / 1 |
|
100.00% |
7 / 7 |
18 | |
100.00% |
36 / 36 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
| createFile | |
100.00% |
1 / 1 |
4 | |
100.00% |
8 / 8 |
|||
| zipDirectory | |
100.00% |
1 / 1 |
4 | |
100.00% |
8 / 8 |
|||
| collectPaths | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
|||
| addFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| addFiles | |
100.00% |
1 / 1 |
4 | |
100.00% |
6 / 6 |
|||
| generateFileContent | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\FileGenerator\Services\FileGenerator\Generators; |
| 4 | |
| 5 | use Qmp\Laravel\FileGenerator\Config; |
| 6 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Factory; |
| 7 | use RecursiveDirectoryIterator; |
| 8 | use RecursiveIteratorIterator; |
| 9 | use ZipArchive; |
| 10 | |
| 11 | class ZipGenerator extends AbstractGenerator |
| 12 | { |
| 13 | /** |
| 14 | * @var array |
| 15 | */ |
| 16 | protected array $paths = []; |
| 17 | |
| 18 | /** |
| 19 | * @var \ZipArchive |
| 20 | */ |
| 21 | protected ZipArchive $zip; |
| 22 | |
| 23 | /** |
| 24 | * @param \Qmp\Laravel\FileGenerator\Config $config |
| 25 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
| 26 | */ |
| 27 | public function __construct(Config $config) |
| 28 | { |
| 29 | parent::__construct($config); |
| 30 | |
| 31 | $this->zip = new ZipArchive(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @var array|string[] |
| 36 | */ |
| 37 | protected array $rules = [ |
| 38 | 'type' => 'required|in:zip', |
| 39 | 'content' => 'required', |
| 40 | ]; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | */ |
| 45 | protected function createFile(): void |
| 46 | { |
| 47 | $this->collectPaths(); |
| 48 | |
| 49 | if ($this->zip->open($this->config->output_path, ZipArchive::CREATE) === TRUE) { |
| 50 | |
| 51 | foreach ($this->paths as $path) { |
| 52 | if (is_dir($path)) { |
| 53 | $this->zipDirectory($path); |
| 54 | } else { |
| 55 | $this->zip->addFile($this->checkFileExist($path), basename($path)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | $this->zip->close(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param string $path |
| 65 | */ |
| 66 | protected function zipDirectory(string $path) |
| 67 | { |
| 68 | $path = substr($path, -1) === '/' ? $path : $path . '/'; |
| 69 | |
| 70 | $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
| 71 | |
| 72 | foreach ($files as $file) { |
| 73 | if (!$file->isDir()) { |
| 74 | $filePath = $file->getRealPath(); |
| 75 | $relativePath = substr($filePath, strlen($path)); |
| 76 | $this->zip->addFile($filePath, $relativePath); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * |
| 83 | */ |
| 84 | protected function collectPaths() |
| 85 | { |
| 86 | if(is_string($this->config->content)) { |
| 87 | $this->addFile($this->config->content); |
| 88 | } |
| 89 | |
| 90 | if(is_array($this->config->content)) { |
| 91 | $this->addFiles($this->config->content); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param string $path |
| 97 | */ |
| 98 | protected function addFile(string $path) |
| 99 | { |
| 100 | $this->paths[] = $path; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param array $files |
| 105 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 106 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
| 107 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\GeneratorNotExistException |
| 108 | */ |
| 109 | protected function addFiles(array $files) |
| 110 | { |
| 111 | foreach ($files as $file) { |
| 112 | if (is_string($file)) { |
| 113 | $this->addFile($file); |
| 114 | } |
| 115 | |
| 116 | if (is_array($file)) { |
| 117 | $this->generateFileContent($file); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param array $data |
| 124 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 125 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
| 126 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\GeneratorNotExistException |
| 127 | */ |
| 128 | protected function generateFileContent(array $data) |
| 129 | { |
| 130 | $config = app()->make(Config::class, $data); |
| 131 | $generator = Factory::handle($config); |
| 132 | $this->addFile($generator->generate()); |
| 133 | } |
| 134 | } |