magallanes/Mage/Console.php

163 lines
4.5 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 static $_log = null;
2011-11-28 03:18:43 +01:00
private static $_logEnabled = true;
2012-01-01 16:36:41 +01:00
private static $_screenBuffer = '';
2012-01-04 02:54:57 +01:00
private static $_commandsOutput = '';
/**
* Runns a Magallanes Command
* @throws Exception
*/
public function run($arguments)
2011-11-22 12:16:06 +01:00
{
$configError = false;
try {
// Load Config
$config = new Mage_Config;
$config->load($arguments);
$configLoadedOk = true;
} catch (Exception $e) {
$configError = $e->getMessage();
2012-02-04 18:45:28 +01:00
}
// Command Option
$commandName = $config->getArgument(0);
2012-09-20 00:16:52 +02:00
// Logging
$showGrettings = true;
if (in_array($commandName, array('install', 'upgrade', 'version'))) {
self::$_logEnabled = false;
$showGrettings = false;
} else {
self::$_logEnabled = $config->general('logging', false);
}
2012-09-20 00:16:52 +02:00
// Grettings
if ($showGrettings) {
Mage_Console::output('Starting <blue>Magallanes</blue>', 0, 2);
}
// Run Command
if ($configError !== false) {
Mage_Console::output('<red>' . $configError . '</red>', 1, 2);
2012-03-31 05:33:17 +02:00
} else {
try {
$command = Mage_Command_Factory::get($commandName, $config);
if ($command instanceOf Mage_Command_RequiresEnvironment) {
if ($config->getEnvironment() == false) {
throw new Exception('You must specify an environment for this command.');
}
2012-03-31 05:33:17 +02:00
}
$command->run();
} catch (Exception $e) {
Mage_Console::output('<red>' . $e->getMessage() . '</red>', 1, 2);
2011-11-22 12:16:06 +01:00
}
}
if ($showGrettings) {
Mage_Console::output('Finished <blue>Magallanes</blue>', 0, 2);
2012-03-31 05:33:17 +02:00
}
2013-07-07 01:46:29 +02:00
self::_checkLogs($config);
2012-03-31 05:33:17 +02:00
}
/**
* Outputs a message to the user screen
*
* @param string $message
* @param integer $tabs
* @param integer $newLine
*/
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));
2012-01-01 16:36:41 +01:00
self::$_screenBuffer .= str_repeat("\t", $tabs)
. strip_tags($message)
. str_repeat(PHP_EOL, $newLine);
$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
}
/**
* Executes a Command on the Shell
*
* @param string $command
* @param string $output
* @return boolean
*/
2012-01-01 16:36:41 +01:00
public static function executeCommand($command, &$output = null)
{
2011-11-27 14:05:06 +01:00
self::log('---------------------------------');
self::log('---- Executing: $ ' . $command);
2012-02-12 18:40:56 +01:00
$return = 1;
$log = array();
exec($command . ' 2>&1', $log, $return);
$log = implode(PHP_EOL, $log);
2012-01-01 16:36:41 +01:00
if (!$return) {
$output = trim($log);
2012-01-01 16:36:41 +01:00
}
self::$_commandsOutput .= PHP_EOL . trim($log) . PHP_EOL;
2011-11-27 14:05:06 +01:00
self::log($log);
self::log('---------------------------------');
return !$return;
}
/**
* Log a message to the logfile.
*
* @param string $message
* @param boolean $continuation
*/
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');
}
2011-11-28 03:18:43 +01:00
$message = date('Y-m-d H:i:s -- ') . $message;
fwrite(self::$_log, $message . PHP_EOL);
2011-11-27 14:05:06 +01:00
}
}
2013-07-07 01:46:29 +02:00
/**
* Check Logs
* @param Mage_Config $config
*/
private static function _checkLogs(Mage_Config $config)
{
if (self::$_logEnabled) {
$maxLogs = $config->general('maxlogs', 30);
$logs = array();
foreach (new RecursiveDirectoryIterator('.mage/logs', RecursiveDirectoryIterator::SKIP_DOTS) as $log) {
if (strpos($log->getFilename(), 'log-') === 0) {
$logs[] = $log->getFilename();
}
}
sort($logs);
if (count($logs) > $maxLogs) {
$logsToDelete = array_slice($logs, 0, count($logs) - $maxLogs);
foreach ($logsToDelete as $logToDeelte) {
unlink('.mage/logs/' . $logToDeelte);
}
}
}
}
}