Skip to content

Commit

Permalink
Faster build times by being able to exclude certain paths from variab…
Browse files Browse the repository at this point in the history
…le replacement:

Issue:  box-project/box2#97

Required for the other PR I am making for box2
  • Loading branch information
webdevvie committed Jun 6, 2016
1 parent b55dceb commit d1aba71
Showing 1 changed file with 71 additions and 4 deletions.
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 = [];
}

/**
* 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

0 comments on commit d1aba71

Please sign in to comment.