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:
<?php
namespace ZfConsoleHelper\Controller\Console;
use Exception;
use RuntimeException;
use Zend\Mvc\Controller\AbstractConsoleController as ParentClass;
use Zend\Console\ColorInterface;
use Zend\Console\Request as ConsoleRequest;
class AbstractConsoleController extends ParentClass
{
private $stopExecution = false;
public function defaultSignalHandler($signal)
{
$this->stopExecution();
}
protected function stopExecution()
{
$this->stopExecution = true;
}
protected function attachSignalHandler(AbstractConsoleController $object, $methodName = 'defaultSignalHandler')
{
declare(ticks = 10);
pcntl_signal(SIGHUP, array($object, $methodName));
pcntl_signal(SIGINT, array($object, $methodName));
pcntl_signal(SIGUSR1, array($object, $methodName));
pcntl_signal(SIGUSR2, array($object, $methodName));
pcntl_signal(SIGQUIT, array($object, $methodName));
pcntl_signal(SIGILL, array($object, $methodName));
pcntl_signal(SIGABRT, array($object, $methodName));
pcntl_signal(SIGFPE, array($object, $methodName));
pcntl_signal(SIGSEGV, array($object, $methodName));
pcntl_signal(SIGPIPE, array($object, $methodName));
pcntl_signal(SIGALRM, array($object, $methodName));
pcntl_signal(SIGCONT, array($object, $methodName));
pcntl_signal(SIGTSTP, array($object, $methodName));
pcntl_signal(SIGTTIN, array($object, $methodName));
pcntl_signal(SIGTTOU, array($object, $methodName));
}
protected function handleException(Exception $exception)
{
$console = $this->getConsole();
$console->setColor(ColorInterface::RED);
$console->writeLine('');
$console->writeLine('caught exception ' . get_class($exception));
$console->writeLine('----------------');
$console->writeLine('with message: ');
$console->writeLine('');
$console->setColor(ColorInterface::RESET);
$console->writeLine($exception->getMessage());
$console->setColor(ColorInterface::RED);
$console->writeLine('----------------');
$console->writeLine('and trace: ');
$console->writeLine('');
$console->setColor(ColorInterface::GRAY);
$console->writeLine($exception->getTraceAsString());
$console->setColor(ColorInterface::RESET);
}
protected function processItems($items, AbstractConsoleController $object, $methodName, $arguments = array())
{
foreach ($items as $item) {
if ($this->stopExecution) {
break 1;
} else {
$parameters = $arguments;
array_unshift($parameters, $item);
call_user_func_array(array($object, $methodName), $parameters);
pcntl_signal_dispatch();
}
}
}
protected function getParameter($name)
{
return $this->getRequest()->getParam($name);
}
public function getRequest()
{
return parent::getRequest();
}
protected function hasBooleanParameter($shortName = '', $longName = '')
{
$has = (($shortName !== '') && ($this->hasParameter($shortName))
|| ($longName !== '') && ($this->hasParameter($longName)));
return $has;
}
protected function hasParameter($name)
{
return (!is_null($this->getParameter($name)));
}
protected function throwExceptionIfNotCalledInsideAnCliEnvironment()
{
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest){
throw new RuntimeException(
'only callable inside console environment'
);
}
}
}