* @license PHP License * @package WB * @subpackage base */ WBClass::load( 'WBCli' , 'WBObserver'); /** * Command Line Interface class: Event Queue Processor * * Run event queue * * @version 0.1.0 * @package WB * @subpackage base */ class WBCli_EventQueueProcessor extends WBCli implements WBObserver { /** * Command line arguments * @var array */ protected $arguments = array( 'namespace' => array( 'short' => 'n', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'use namespace', ), 'failed' => array( 'short' => 'f', 'max' => 0, 'desc' => 'failed event actions', ), CONSOLE_GETARGS_PARAMS => array( 'min' => 1, 'max' => -1, 'default' => 'ls', 'desc' => 'action', ) ); /** * event processor * @var WBEvent_Processor */ private $proc; /** * event queue's namespace * @var string */ private $namespace = null; /** * display failed items * @var int */ private $failed = 0; /** * 2nd constructor * * Load event processor * * @see include/WB/WBCli#init() */ protected function init() { $this->proc = WBClass::create('WBEvent_Processor'); } /** * execute programme * * Use observer and execute event queue */ protected function execute() { $ns = $this->args->getValue('namespace'); if (!empty($ns)) { $this->namespace = $ns; } if ($this->args->isDefined('failed')) { $this->failed = 1; } if (empty($this->commands)) { $this->commands = array('ls'); } $action = strtolower(array_shift($this->commands)); switch ($action) { case 'ls': $this->executeList(); break; case 'run': $this->executeRun($this->commands); break; case 'rm': $this->executeRemove($this->commands); break; default: $this->pl('Unknown command "' . $action . '".', STDERR); break; } } private function executeRun($ids) { $this->pvl('Run queued events...'); $this->proc->attach($this); if ('all' == $ids[0]) { $this->proc->executeAll(); } else { foreach ($ids as $id) { $this->proc->executeId($id); } } $this->proc->detach($this); } private function executeRemove($ids) { $this->pvl('Remove events...'); $this->proc->attach($this); foreach ($ids as $id) { $this->proc->remove($id); } $this->proc->detach($this); } private function executeList() { $this->pvl('List queued events...'); $list = $this->proc->getList($this->namespace, $this->failed); $this->pv('found: '); if (empty($list)) { $this->pl('0'); return; } $this->pl(count($list)); // list items $fmt = '%s;%s;%s;%s;%s'; if (WBCli::VERBOSITY_VERBOSE == $this->verbosity) { $fmt = '| %8s | %-30s | %-20s | %-15s | %-19s | %1s |'; } $line = '+' . str_repeat('-', 10) . '+' . str_repeat('-', 32) . '+' . str_repeat('-', 22) . '+' . str_repeat('-', 17) . '+' . str_repeat('-', 21) . '+' . str_repeat('-', 3) . '+'; $this->pvl($line); $this->pvl(sprintf($fmt, 'id', 'name', 'action', 'namespace', 'created', 'f')); $this->pvl($line); foreach ($list as $l) { $this->pl(sprintf($fmt, $l['id'], $l['name'], $l['action'], $l['namespace'], $l['created'], $l['failed'])); } $this->pvl($line); } /** * observer method: update * * Observe event processor on update */ public function update($proc) { if ($this->proc->getPosition() == 1) { $this->pvl("process: " . $this->proc->getCount()); } $this->pv('.'); } } ?>