diff --git a/src/Generator/Code/Chunks.php b/src/Generator/Code/Chunks.php index 61e910d0..88561e22 100644 --- a/src/Generator/Code/Chunks.php +++ b/src/Generator/Code/Chunks.php @@ -20,6 +20,8 @@ namespace PSX\Schema\Generator\Code; +use ZipArchive; + /** * Chunks * @@ -29,76 +31,66 @@ */ class Chunks { - /** - * @var string|null - */ - private $namespace; - - /** - * @var array - */ - private $parts = []; + private ?string $namespace; + private array $parts = []; - /** - * @param string|null $namespace - */ public function __construct(?string $namespace = null) { $this->namespace = $namespace; } - /** - * @param string $identifier - * @param string $code - */ - public function append(string $identifier, string $code) + public function append(string $identifier, string|Chunks $code): void { $this->parts[$identifier] = $code; } - /** - * @param Chunks $chunks - */ - public function merge(Chunks $chunks) + public function merge(Chunks $chunks): void { foreach ($chunks->getChunks() as $identifier => $code) { $this->append($identifier, $code); } } - /** - * @return string|null - */ public function getNamespace(): ?string { return $this->namespace; } /** - * @return array + * @return array */ - public function getChunks() + public function getChunks(): array { return $this->parts; } /** * Writes this chunk collection as zip archive to the provided file - * - * @param string $file */ - public function writeTo(string $file) + public function writeTo(string $file): void { - $zip = new \ZipArchive(); - $zip->open($file, \ZipArchive::CREATE); + $zip = new ZipArchive(); + $zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE); - foreach ($this->getChunks() as $identifier => $code) { - $zip->addFromString($identifier, $code); - } + $this->recursiveAddToZip($zip, $this); $zip->close(); } + private function recursiveAddToZip(ZipArchive $zip, Chunks $chunks, ?string $basePath = null): void + { + foreach ($chunks->getChunks() as $identifier => $code) { + $prefix = $basePath !== null ? $basePath . '/' : ''; + + if ($code instanceof Chunks) { + $zip->addEmptyDir($prefix . $identifier); + $this->recursiveAddToZip($zip, $code, $prefix . $identifier); + } else { + $zip->addFromString($prefix . $identifier, $code); + } + } + } + /** * @return string */ diff --git a/tests/Generator/ChunksTest.php b/tests/Generator/ChunksTest.php new file mode 100644 index 00000000..da654836 --- /dev/null +++ b/tests/Generator/ChunksTest.php @@ -0,0 +1,49 @@ + + * + * Copyright 2010-2023 Christoph Kappestein + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace PSX\Schema\Tests\Generator; + +use PHPUnit\Framework\TestCase; +use PSX\Schema\Generator\Code\Chunks; +use PSX\Schema\Generator\Config; + +/** + * ChunksTest + * + * @author Christoph Kappestein + * @license http://www.apache.org/licenses/LICENSE-2.0 + * @link https://phpsx.org + */ +class ChunksTest extends TestCase +{ + public function testWriteTo() + { + $subChunks = new Chunks(); + $subChunks->append('file_b', 'foobar'); + + $chunks = new Chunks(); + $chunks->append('folder', $subChunks); + $chunks->append('file_a', 'foobar'); + + $chunks->writeTo(__DIR__ . '/resource/test.zip'); + + $this->assertFileExists(__DIR__ . '/resource/test.zip'); + } +} \ No newline at end of file