Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Output buffering during buildFrom* functions #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions src/lib/Herrera/Box/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FilesystemIterator;
use Herrera\Box\Compactor\CompactorInterface;
use Herrera\Box\Exception\Exception;
use Herrera\Box\Exception\FileException;
use Herrera\Box\Exception\InvalidArgumentException;
use Herrera\Box\Exception\OpenSslException;
Expand Down Expand Up @@ -161,54 +162,62 @@ public function buildFromIterator(Traversable $iterator, $base = null)
$base = Path::canonical($base . DIRECTORY_SEPARATOR);
}

foreach ($iterator as $key => $value) {
if (is_string($value)) {
if (false === is_string($key)) {
throw UnexpectedValueException::create(
'The key returned by the iterator (%s) is not a string.',
gettype($key)
);
}

$key = Path::canonical($key);
$value = Path::canonical($value);

if (is_dir($value)) {
$this->phar->addEmptyDir($key);
$this->phar->startBuffering();
try {
foreach ($iterator as $key => $value) {
if (is_string($value)) {
if (false === is_string($key)) {
throw UnexpectedValueException::create(
'The key returned by the iterator (%s) is not a string.',
gettype($key)
);
}

$key = Path::canonical($key);
$value = Path::canonical($value);

if (is_dir($value)) {
$this->phar->addEmptyDir($key);
} else {
$this->addFile($value, $key);
}
} elseif ($value instanceof SplFileInfo) {
if (null === $base) {
throw InvalidArgumentException::create(
'The $base argument is required for SplFileInfo values.'
);
}

/** @var $value SplFileInfo */
$real = $value->getRealPath();

if (0 !== strpos($real, $base)) {
throw UnexpectedValueException::create(
'The file "%s" is not in the base directory.',
$real
);
}

$local = str_replace($base, '', $real);

if ($value->isDir()) {
$this->phar->addEmptyDir($local);
} else {
$this->addFile($real, $local);
}
} else {
$this->addFile($value, $key);
}
} elseif ($value instanceof SplFileInfo) {
if (null === $base) {
throw InvalidArgumentException::create(
'The $base argument is required for SplFileInfo values.'
);
}

/** @var $value SplFileInfo */
$real = $value->getRealPath();

if (0 !== strpos($real, $base)) {
throw UnexpectedValueException::create(
'The file "%s" is not in the base directory.',
$real
'The iterator value "%s" was not expected.',
gettype($value)
);
}

$local = str_replace($base, '', $real);

if ($value->isDir()) {
$this->phar->addEmptyDir($local);
} else {
$this->addFile($real, $local);
}
} else {
throw UnexpectedValueException::create(
'The iterator value "%s" was not expected.',
gettype($value)
);
}
} catch (Exception $e) {
$this->phar->stopBuffering();
throw $e;
}

$this->phar->stopBuffering();
}

/**
Expand Down