diff --git a/src/lib/Herrera/Box/Box.php b/src/lib/Herrera/Box/Box.php index ed0d4d1..7f88b4b 100644 --- a/src/lib/Herrera/Box/Box.php +++ b/src/lib/Herrera/Box/Box.php @@ -52,6 +52,23 @@ class Box */ private $values = array(); + /** + * An array of files to add via the faster queueing method + * + * @var array + */ + private $fileQueue = array(); + + /** + * @var array + */ + private $excludedFromValueReplace = array(); + + /** + * @var string + */ + private $excludedFromValueReplaceBasePath = ''; + /** * Sets the Phar instance. * @@ -75,6 +92,42 @@ public function addCompactor(CompactorInterface $compactor) $this->compactors->attach($compactor); } + + /** + * adds the files in the queue to the phar file + * + * @return void + */ + public function addFilesFromQueue() + { + if (count($this->fileQueue) == 0) { + return; + } + $this->phar->buildFromIterator( + new \ArrayIterator($this->fileQueue), + $this->excludedFromValueReplaceBasePath + ); + $this->fileQueue = array(); + } + + /** + * Checks if the file should be variable replaced or should be queued + * + * @param string $local + * @return boolean + */ + private function shouldValueReplace($local) + { + if (count($this->excludedFromValueReplace) == 0) { + return true; + } + foreach ($this->excludedFromValueReplace as $regexp) { + if (preg_match($regexp, $local) > 0) { + return false; + } + } + return true; + } /** * Adds a file to the Phar, after compacting it and replacing its * placeholders. @@ -90,19 +143,20 @@ public function addFile($file, $local = null) if (null === $local) { $local = $file; } - if (false === is_file($file)) { throw FileException::create( 'The file "%s" does not exist or is not a file.', $file ); } - if (false === ($contents = @file_get_contents($file))) { throw FileException::lastError(); } - - $this->addFromString($local, $contents); + if ($this->shouldValueReplace($local)) { + $this->addFromString($local, $contents); + } else { + $this->fileQueue[$local] = $file; + } } /** @@ -314,6 +368,19 @@ public function setStubUsingFile($file, $replace = false) $this->phar->setStub($contents); } + /** + * Sets the regular expressions for files that don't have to be value replaced + * + * @param array $regexps + * @param string $basePath + * @return void + */ + public function setExcludedFromValueReplace($regexps, $basePath) + { + $this->excludedFromValueReplace = $regexps; + $this->excludedFromValueReplaceBasePath = $basePath; + } + /** * Sets the placeholder values. *