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:
<?php
namespace splitbrain\phpcli;
abstract class CLI
{
protected $bin;
protected $options;
public $colors;
protected $loglevel = array(
'debug' => array('', Colors::C_RESET, STDOUT),
'info' => array('ℹ ', Colors::C_CYAN, STDOUT),
'notice' => array('☛ ', Colors::C_CYAN, STDOUT),
'success' => array('✓ ', Colors::C_GREEN, STDOUT),
'warning' => array('⚠ ', Colors::C_BROWN, STDERR),
'error' => array('✗ ', Colors::C_RED, STDERR),
'critical' => array('☠ ', Colors::C_LIGHTRED, STDERR),
'alert' => array('✖ ', Colors::C_LIGHTRED, STDERR),
'emergency' => array('✘ ', Colors::C_LIGHTRED, STDERR),
);
protected $logdefault = 'info';
public function __construct($autocatch = true)
{
if ($autocatch) {
set_exception_handler(array($this, 'fatal'));
}
$this->colors = new Colors();
$this->options = new Options($this->colors);
}
abstract protected function setup(Options $options);
abstract protected function main(Options $options);
public function run()
{
if ('cli' != php_sapi_name()) {
throw new Exception('This has to be run from the command line');
}
$this->setup($this->options);
$this->registerDefaultOptions();
$this->parseOptions();
$this->handleDefaultOptions();
$this->setupLogging();
$this->checkArgments();
$this->execute();
exit(0);
}
protected function registerDefaultOptions()
{
$this->options->registerOption(
'help',
'Display this help screen and exit immediately.',
'h'
);
$this->options->registerOption(
'no-colors',
'Do not use any colors in output. Useful when piping output to other tools or files.'
);
$this->options->registerOption(
'loglevel',
'Minimum level of messages to display. Default is ' . $this->colors->wrap($this->logdefault, Colors::C_CYAN) . '. ' .
'Valid levels are: debug, info, notice, success, warning, error, critical, alert, emergency.',
null,
'level'
);
}
protected function handleDefaultOptions()
{
if ($this->options->getOpt('no-colors')) {
$this->colors->disable();
}
if ($this->options->getOpt('help')) {
echo $this->options->help();
exit(0);
}
}
protected function setupLogging()
{
$level = $this->options->getOpt('loglevel', $this->logdefault);
if (!isset($this->loglevel[$level])) $this->fatal('Unknown log level');
foreach (array_keys($this->loglevel) as $l) {
if ($l == $level) break;
unset($this->loglevel[$l]);
}
}
protected function parseOptions()
{
$this->options->parseOptions();
}
protected function checkArgments()
{
$this->options->checkArguments();
}
protected function execute()
{
$this->main($this->options);
}
public function fatal($error, array $context = array())
{
$code = 0;
if (is_object($error) && is_a($error, 'Exception')) {
$this->debug(get_class($error) . ' caught in ' . $error->getFile() . ':' . $error->getLine());
$this->debug($error->getTraceAsString());
$code = $error->getCode();
$error = $error->getMessage();
}
if (!$code) {
$code = Exception::E_ANY;
}
$this->critical($error, $context);
exit($code);
}
public function emergency($message, array $context = array())
{
$this->log('emergency', $message, $context);
}
public function alert($message, array $context = array())
{
$this->log('alert', $message, $context);
}
public function critical($message, array $context = array())
{
$this->log('critical', $message, $context);
}
public function error($message, array $context = array())
{
$this->log('error', $message, $context);
}
public function warning($message, array $context = array())
{
$this->log('warning', $message, $context);
}
public function success($string, array $context = array())
{
$this->log('success', $string, $context);
}
public function notice($message, array $context = array())
{
$this->log('notice', $message, $context);
}
public function info($message, array $context = array())
{
$this->log('info', $message, $context);
}
public function debug($message, array $context = array())
{
$this->log('debug', $message, $context);
}
public function log($level, $message, array $context = array())
{
if (!isset($this->loglevel[$level])) return;
list($prefix, $color, $channel) = $this->loglevel[$level];
if (!$this->colors->isEnabled()) $prefix = '';
$message = $this->interpolate($message, $context);
$this->colors->ptln($prefix . $message, $color, $channel);
}
function interpolate($message, array $context = array())
{
$replace = array();
foreach ($context as $key => $val) {
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
$replace['{' . $key . '}'] = $val;
}
}
return strtr($message, $replace);
}
}