Browse Source

Merge pull request #6 from iambrosi/compiler

New PHAR compiler for the library
1.0
Andrés Montañez 12 years ago
parent
commit
7d8bf35df4
  1. 52
      Mage/Compiler.php
  2. 8
      Mage/Console.php
  3. 22
      Mage/Task/Compile.php

52
Mage/Compiler.php

@ -0,0 +1,52 @@
<?php
/**
* Class Mage_Compiler
*
* Compiles the library into a .phar file
*
* @author Ismael Ambrosi<ismaambrosi@gmail.com>
*/
class Mage_Compiler
{
/**
* Compiles the library
*
* @param string $file
*/
public function compile($file = 'mage.phar')
{
if (file_exists($file)) {
unlink($file);
}
$phar = new Phar($file, 0, 'mage.phar');
$phar->setSignatureAlgorithm(Phar::SHA1);
$phar->startBuffering();
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__), RecursiveIteratorIterator::CHILD_FIRST);
/** @var $path SplFileInfo */
foreach ($iterator as $path) {
if ($path->isFile()) {
$phar->addFromString(str_replace(dirname(__DIR__).'/', '', $path->getPathname()), file_get_contents($path));
}
}
$phar->addFromString('mage', str_replace(
'$baseDir = dirname(dirname(__FILE__));',
'$baseDir = __DIR__;',
file_get_contents(__DIR__.'/../bin/mage.php')
));
$phar->setStub("#!/usr/bin/env php\n<?php Phar::mapPhar('mage.phar'); require 'phar://mage.phar/mage'; __HALT_COMPILER();");
$phar->stopBuffering();
unset($phar);
chmod($file, 0755);
}
}

8
Mage/Console.php

@ -31,6 +31,9 @@ class Mage_Console
} else if ($this->_args[0] == 'update') {
$this->_action = 'update';
} else if ($this->_args[0] == 'compile') {
$this->_action = 'compile';
} else if ($this->_args[0] == 'add') {
$this->_action = 'add';
@ -165,6 +168,11 @@ class Mage_Console
$task->run($config);
break;
case 'compile';
$task = new Mage_Task_Compile;
$task->run($config);
break;
case 'install';
$task = new Mage_Task_Install;
$task->run();

22
Mage/Task/Compile.php

@ -0,0 +1,22 @@
<?php
/**
* Class Mage_Task_Compile
*
* @author Ismael Ambrosi<ismaambrosi@gmail.com>
*/
class Mage_Task_Compile
{
/**
* @see Mage_Compile::compile()
*/
public function run ()
{
Mage_Console::output('Compiling <dark_gray>Magallanes</dark_gray>... ', 1, 0);
$compiler = new Mage_Compiler();
$compiler->compile();
Mage_Console::output('Mage compiled successfully');
}
}
Loading…
Cancel
Save