Skip to content

Commit

Permalink
allow nested chunks and add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 17, 2024
1 parent 445a86f commit c261c59
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 34 deletions.
60 changes: 26 additions & 34 deletions src/Generator/Code/Chunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace PSX\Schema\Generator\Code;

use ZipArchive;

/**
* Chunks
*
Expand All @@ -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<string|Chunks>
*/
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
*/
Expand Down
49 changes: 49 additions & 0 deletions tests/Generator/ChunksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <[email protected]>
*
* 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 <[email protected]>
* @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');
}
}

0 comments on commit c261c59

Please sign in to comment.