magallanes/Mage/Console.php

136 lines
3.6 KiB
PHP
Raw Normal View History

2011-11-22 12:16:06 +01:00
<?php
class Mage_Console
2011-11-22 12:16:06 +01:00
{
2011-11-27 14:05:06 +01:00
private $_args = null;
private $_action = null;
private $_actionOptions = null;
private $_environment = null;
private static $_log = null;
2011-11-28 03:18:43 +01:00
private static $_logEnabled = true;
2011-11-22 12:16:06 +01:00
public function setArgs($args)
{
$this->_args = $args;
array_shift($this->_args);
}
public function parse()
{
if ($this->_args[0] == 'deploy') {
2011-11-22 12:16:06 +01:00
$this->_action = 'deploy';
} else if ($this->_args[0] == 'update') {
2011-11-22 12:16:06 +01:00
$this->_action = 'update';
} else if ($this->_args[0] == 'add') {
$this->_action = 'add';
2011-11-28 03:18:43 +01:00
} else if ($this->_args[0] == 'install') {
$this->_action = 'install';
2011-11-24 02:45:04 +01:00
} else if ($this->_args[0] == 'init') {
$this->_action = 'init';
}
foreach ($this->_args as $argument) {
if (preg_match('/to:[\w]+/i', $argument)) {
2011-11-22 12:16:06 +01:00
$this->_environment = str_replace('to:', '', $argument);
}
}
}
public function getAction()
{
return $this->_action;
}
public function getEnvironment()
{
return $this->_environment;
}
public static function output($message, $tabs = 1, $newLine = 1)
2011-11-22 12:16:06 +01:00
{
2011-11-27 14:05:06 +01:00
self::log(strip_tags($message));
$output = str_repeat("\t", $tabs)
. Mage_Console_Colors::color($message)
. str_repeat(PHP_EOL, $newLine);
echo $output;
2011-11-22 12:16:06 +01:00
}
public static function executeCommand($command)
{
2011-11-27 14:05:06 +01:00
self::log('---------------------------------');
self::log('---- Executing: $ ' . $command);
ob_start();
system($command . ' 2>&1', $return);
$log = ob_get_clean();
2011-11-27 14:05:06 +01:00
self::log($log);
self::log('---------------------------------');
return !$return;
}
2011-11-22 12:16:06 +01:00
public function run()
2011-11-28 03:18:43 +01:00
{
// Disable Loging
if ($this->getAction() == 'install') {
self::$_logEnabled = false;
}
Mage_Console::output('Starting <blue>Magallanes</blue>', 0, 2);
$config = new Mage_Config;
$config->loadEnvironment($this->getEnvironment());
$config->loadSCM();
2011-11-22 12:16:06 +01:00
switch ($this->getAction()) {
case 'deploy':
$task = new Mage_Task_Deploy;
$task->run($config);
2011-11-22 12:16:06 +01:00
break;
case 'update';
$task = new Mage_Task_Update;
$task->run($config);
2011-11-22 12:16:06 +01:00
break;
2011-11-28 03:18:43 +01:00
case 'install';
$task = new Mage_Task_Install;
$task->run();
break;
2011-11-24 02:45:04 +01:00
case 'init';
$task = new Mage_Task_Init;
$task->run();
break;
case 'add';
switch ($this->_args[1]) {
case 'environment':
$task = new Mage_Task_Add;
$task->environment($this->_args[2]);
break;
}
break;
2011-11-22 12:16:06 +01:00
}
2011-11-28 03:18:43 +01:00
Mage_Console::output('Finished <blue>Magallanes</blue>', 0, 2);
2011-11-22 12:16:06 +01:00
}
2011-11-27 14:05:06 +01:00
public static function log($message, $continuation = false)
{
2011-11-28 03:18:43 +01:00
if (self::$_logEnabled) {
if (self::$_log == null) {
self::$_log = fopen('.mage/logs/log-' . date('Ymd-His') . '.log', 'w');
}
$message = date('Y-m-d H:i:s -- ') . $message;
fwrite(self::$_log, $message . PHP_EOL);
2011-11-27 14:05:06 +01:00
}
}
}