1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
<?php
namespace Deliverist\Builder\Commands;
use Deliverist\Builder\Builder;
use Deliverist\Builder\FileSystemException;
use Deliverist\Builder\InvalidArgumentException;
use Deliverist\Builder\ICommand;
class ApacheImports implements ICommand
{
private $toRemove;
public function run(Builder $builder, $files = NULL, $removeFiles = TRUE)
{
if (!isset($files)) {
throw new InvalidArgumentException("Missing parameter 'files'.");
}
$this->toRemove = array();
if (!is_array($files)) {
$files = array($files);
}
foreach ($files as $file) {
$path = $builder->getPath($file);
if (!is_file($path)) {
throw new FileSystemException("File '$file' not found.");
}
$this->processFile($path);
}
if ($removeFiles) {
foreach ($this->toRemove as $file) {
\Nette\Utils\FileSystem::delete($file);
}
}
}
private function processFile($path)
{
$content = file_get_contents($path);
file_put_contents($path, $this->expandApacheImports($content, $path));
}
private function expandApacheImports($content, $path)
{
$dir = dirname($path);
return preg_replace_callback('~<!--#include\s+file="(.+)"\s+-->~U', function ($m) use ($dir, $path) {
$file = $dir . '/' . $m[1];
if (is_file($file)) {
$this->toRemove[] = $file;
return $this->expandApacheImports(file_get_contents($file), $file);
} else {
throw new FileSystemException("Required file '" . $m[1] . "' is missing.");
}
}, $content);
}
}