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

Faster-builds #23

Open
wants to merge 2 commits 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
75 changes: 71 additions & 4 deletions src/lib/Herrera/Box/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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;
}
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down