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: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386:
<?php
class Burgomaster
{
public $stageDir;
public $projectRoot;
private $sections = array();
public function __construct($stageDir, $projectRoot = null)
{
$this->startSection('setting_up');
$this->stageDir = $stageDir;
$this->projectRoot = $projectRoot;
if (!$this->stageDir || $this->stageDir == '/') {
throw new \InvalidArgumentException('Invalid base directory');
}
if (is_dir($this->stageDir)) {
$this->debug("Removing existing directory: $this->stageDir");
echo $this->exec("rm -rf $this->stageDir");
}
$this->debug("Creating staging directory: $this->stageDir");
if (!mkdir($this->stageDir, 0777, true)) {
throw new \RuntimeException("Could not create {$this->stageDir}");
}
$this->stageDir = realpath($this->stageDir);
$this->debug("Creating staging directory at: {$this->stageDir}");
if (!is_dir($this->projectRoot)) {
throw new \InvalidArgumentException(
"Project root not found: $this->projectRoot"
);
}
$this->endSection();
$this->startSection('staging');
chdir($this->projectRoot);
}
public function __destruct()
{
if ($this->sections) {
$this->endSection();
}
}
public function startSection($section)
{
$this->sections[] = $section;
$this->debug('Starting');
}
public function endSection()
{
if ($this->sections) {
$this->debug('Completed');
array_pop($this->sections);
}
}
public function debug($message)
{
$prefix = date('c') . ': ';
if ($this->sections) {
$prefix .= '[' . end($this->sections) . '] ';
}
fwrite(STDERR, $prefix . $message . "\n");
}
public function deepCopy($from, $to)
{
if (!is_file($from)) {
throw new \InvalidArgumentException("File not found: {$from}");
}
$to = str_replace('//', '/', $this->stageDir . '/' . $to);
$dir = dirname($to);
if (!is_dir($dir)) {
if (!mkdir($dir, 0777, true)) {
throw new \RuntimeException("Unable to create directory: $dir");
}
}
if (!copy($from, $to)) {
throw new \RuntimeException("Unable to copy $from to $to");
}
}
public function recursiveCopy(
$sourceDir,
$destDir,
$extensions = array('php')
) {
if (!realpath($sourceDir)) {
throw new \InvalidArgumentException("$sourceDir not found");
}
if (!$extensions) {
throw new \InvalidArgumentException('$extensions is empty!');
}
$sourceDir = realpath($sourceDir);
$exts = array_fill_keys($extensions, true);
$iter = new \RecursiveDirectoryIterator($sourceDir);
$iter = new \RecursiveIteratorIterator($iter);
$total = 0;
$this->startSection('copy');
$this->debug("Starting to copy files from $sourceDir");
foreach ($iter as $file) {
if (isset($exts[$file->getExtension()])
|| $file->getBaseName() == 'LICENSE'
) {
$toPath = str_replace($sourceDir, '', (string) $file);
$toPath = $destDir . '/' . $toPath;
$toPath = str_replace('//', '/', $toPath);
$this->deepCopy((string) $file, $toPath);
$total++;
}
}
$this->debug("Copied $total files from $sourceDir");
$this->endSection();
}
public function exec($command)
{
$this->debug("Executing: $command");
$output = $returnValue = null;
exec($command, $output, $returnValue);
if ($returnValue != 0) {
throw new \RuntimeException('Error executing command: '
. $command . ' : ' . implode("\n", $output));
}
return implode("\n", $output);
}
public function createAutoloader($files = array(), $filename = 'autoloader.php')
{
$sourceDir = realpath($this->stageDir);
$iter = new \RecursiveDirectoryIterator($sourceDir);
$iter = new \RecursiveIteratorIterator($iter);
$this->startSection('autoloader');
$this->debug('Creating classmap autoloader');
$this->debug("Collecting valid PHP files from {$this->stageDir}");
$classMap = array();
foreach ($iter as $file) {
if ($file->getExtension() == 'php') {
$location = str_replace($this->stageDir . '/', '', (string) $file);
$className = str_replace('/', '\\', $location);
$className = substr($className, 0, -4);
if (strpos($className, 'src\\') === 0
|| strpos($className, 'lib\\') === 0
) {
$className = substr($className, 4);
}
$classMap[$className] = "__DIR__ . '/$location'";
$this->debug("Found $className");
}
}
$destFile = $this->stageDir . '/' . $filename;
$this->debug("Writing autoloader to {$destFile}");
if (!($h = fopen($destFile, 'w'))) {
throw new \RuntimeException('Unable to open file for writing');
}
$this->debug('Writing classmap files');
fwrite($h, "<?php\n\n");
fwrite($h, "\$mapping = array(\n");
foreach ($classMap as $c => $f) {
fwrite($h, " '$c' => $f,\n");
}
fwrite($h, ");\n\n");
fwrite($h, <<<EOT
spl_autoload_register(function (\$class) use (\$mapping) {
if (isset(\$mapping[\$class])) {
require \$mapping[\$class];
}
}, true);
EOT
);
fwrite($h, "\n");
$this->debug('Writing automatically included files');
foreach ($files as $file) {
fwrite($h, "require __DIR__ . '/$file';\n");
}
fclose($h);
$this->endSection();
}
/**
* Creates a default stub for the phar that includeds the generated
* autoloader.
*
* This phar also registers a constant that can be used to check if you
* are running the phar. The constant is the basename of the $dest variable
* without the extension, with "_PHAR" appended, then converted to all
* caps (e.g., "/foo/guzzle.phar" gets a contant defined as GUZZLE_PHAR.
*
* @param $dest
* @param string $autoloaderFilename Name of the autoloader file.
*
* @return string
*/
private function createStub($dest, $autoloaderFilename = 'autoloader.php')
{
$this->startSection('stub');
$this->debug("Creating phar stub at $dest");
$alias = basename($dest);
$constName = str_replace('.phar', '', strtoupper($alias)) . '_PHAR';
$stub = "<?php\n";
$stub .= "define('$constName', true);\n";
$stub .= "require 'phar://$alias/{$autoloaderFilename}';\n";
$stub .= "__HALT_COMPILER();\n";
$this->endSection();
return $stub;
}
/**
* Creates a phar that automatically registers an autoloader.
*
* Call this only after your staging directory is built.
*
* @param string $dest Where to save the file. The basename of the file
* is also used as the alias name in the phar
* (e.g., /path/to/guzzle.phar => guzzle.phar).
* @param string|bool|null $stub The path to the phar stub file. Pass or
* leave null to automatically have one created for you. Pass false
* to no use a stub in the generated phar.
* @param string $autoloaderFilename Name of the autolaoder filename.
*/
public function createPhar(
$dest,
$stub = null,
$autoloaderFilename = 'autoloader.php'
) {
$this->startSection('phar');
$this->debug("Creating phar file at $dest");
$this->createDirIfNeeded(dirname($dest));
$phar = new \Phar($dest, 0, basename($dest));
$phar->buildFromDirectory($this->stageDir);
if ($stub !== false) {
if (!$stub) {
$stub = $this->createStub($dest, $autoloaderFilename);
}
$phar->setStub($stub);
}
$this->debug("Created phar at $dest");
$this->endSection();
}
/**
* Creates a zip file containing the staged files of your project.
*
* Call this only after your staging directory is built.
*
* @param string $dest Where to save the zip file
*/
public function createZip($dest)
{
$this->startSection('zip');
$this->debug("Creating a zip file at $dest");
$this->createDirIfNeeded(dirname($dest));
chdir($this->stageDir);
$this->exec("zip -r $dest ./");
$this->debug(" > Created at $dest");
chdir(__DIR__);
$this->endSection();
}
private function createDirIfNeeded($dir)
{
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
throw new \RuntimeException("Could not create dir: $dir");
}
}
}