function run() { $config = $this->config; $context = $this->context; /* @var $context ExecutionContext */ $write = function ($s) { echo $s; }; $writeln = function ($s) { $d = new \DateTime(); echo $d->format(\DateTime::ISO8601) . ' ' . $s . "\n"; }; $c = $color = new \Colors\Color(); /* @var $c \Colors\Color */ $writeln($c('Running agent `' . $this->name)->bold() . '`'); // generate an array with handlers $writeln('Loading handlers'); $handlers = []; $handlersDir = $config->handlers_dir; if ($handlersDir) { $finder = new \Symfony\Component\Finder\Finder(); foreach ($finder->in($handlersDir)->name('*.php')->sortByName() as $file) { /* @var $file \SplFileInfo */ $handlerId = $file->getBasename('.php'); $handler = (include $file); $handlers[$handlerId] = $handler; $writeln(' setting up handler ' . $color($handlerId)->bold() . $color->yellow(' (in ' . $file . ')')); $eventName = SqsEvents::generateEventForAgentId(SqsEvents::EVENT_SQSAGENT_SQSMESSAGERECEIVED, $this->getId()); $context->getEventDispatcher()->addListener($eventName, function (\T24\Event\SqsMessageReceivedEvent $event) use($write, $writeln, $color, $handler, $handlerId, $eventName) { $writeln($color->bg('blue', $color->white('invoking handler ' . $handlerId . ' for event ' . $eventName))); /* @var Callable $handler */ $result = $handler($event); // write comments $comments = $event->getComments(); foreach ($comments as $comment) { $writeln($color(' comment: ')->yellow() . $comment); } $writeln(' - handler ' . $handlerId . ' had been invoked.'); if ($event->isPropagationStopped()) { $writeln(' - ' . $color->red('the event was stopped from further propagation.')); } else { $writeln(' - the event was not stopped. proceed to next handler'); } return $result; }); } $writeln('Loaded ' . count($handlers) . ' handlers.'); } else { $writeln('default handlers dir not specified, no handlers defined..'); } // @todo: get aws params. $region = ''; $sqsQueueUrl = ''; $sqs = SqsClient::factory(['region' => $config->aws_region, 'version' => '2012-11-05', 'credentials' => ['key' => $config->aws_key, 'secret' => $config->aws_secret]]); // start the process $ttl = (int) $config->ttl; $ttl = min($ttl, 300); $ttl = max($ttl, 5); $sleep = (int) $config->sleep; $sleep = min($sleep, $ttl - 5); $sleep = max($sleep, 0); $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch(); $b = $stopwatch->start('loop'); while ($b->getDuration() < $ttl * 1000) { // get an sqs message from the queue $writeln($c('Polling Sqs for messages')); $result = $sqs->receiveMessage(['QueueUrl' => $config->sqs_queue_url, 'AttributeNames' => ['All'], 'MessageAttributeNames' => ['All'], 'MaxNumberOfMessages' => 1]); /* @var $result \Aws\Result */ if (!$result['Messages'] || $result['Messages'] == 0) { $writeln($c('no messages found in sqs queue')); } else { $writeln($c(sprintf('%d message(s) retrieved from queue.', count($result['Messages'])))); foreach ($result['Messages'] as $message) { // set up an event that the message is received. // the default subscriber will pass the event to the event handlers in the handlers dir $event = new SqsMessageReceivedEvent($context, $message); $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_SQSMESSAGERECEIVED, $event); $context->getEventDispatcher()->dispatch(SqsEvents::generateEventForAgentId(SqsEvents::EVENT_SQSAGENT_SQSMESSAGERECEIVED, $this->getId()), $event); /* $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_SQSMESSAGEHANDLED, $event2); if ($event->isProcessed()) { $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_SQSMESSAGEPROCESSED, $event2); } */ if ($event->getRemoveFromQueue()) { $writeln(' - the event handlers indicate the sqs message should be removed from the queue.'); // remove from sqs $sqsMessage = $event->getSqsMessage(); $removed = $sqs->deleteMessage(['QueueUrl' => $config->sqs_queue_url, 'ReceiptHandle' => $sqsMessage['ReceiptHandle']]); $writeln(' - ' . ($removed ? $c->green('succesfully removed the message from SQS') : $c->red('failed to remove the message from SQS'))); } else { $writeln(' - the event handlers indicate the sqs messageshould not be removed from the queue.'); } } } $writeln($color->cyan('sleeping for ' . $sleep . ' seconds')); sleep($sleep); } // end stopwatch while loop $writeln('Stopped after ' . (int) ($b->getDuration() / 1000) . ' secs.'); // send the finish event. $event = new \T24\Event\AgentEvent($context); $context->getEventDispatcher()->dispatch(SqsEvents::EVENT_SQSAGENT_FINISH, $event); $context->getEventDispatcher()->dispatch(SqsEvents::generateEventForAgentId(SqsEvents::EVENT_SQSAGENT_FINISH, $this->getId()), $event); }
{ function __invoke($string = null) { $obj = new Colors\Color($string); return $obj; } } $c = new Kolor(); $c = new Colors\Color(); function color($string) { return new Colors\Color($string); } echo color('Hello World!')->red()->white() . color("some other text 1 ")->green() . color("some other text 2 ")->blue() . color("some other text 3 ")->yellow() . color("some other text 4 ")->white() . PHP_EOL; exit; print "we r here \n"; print $c->red("this is red") . "\n"; print $c->bold($c->red("this is red and bold")) . "\n"; print $c("also this is red and bold")->red->bold . "\n"; $c("this is some text"); var_dump($c); print "===============================================================\n"; $c("this is some text")->red(); var_dump($c); print "===============================================================\n"; $c("different text"); var_dump($c); print "===============================================================\n"; $c("different text")->blue(); var_dump($c); print "===============================================================\n";
/** * @return string */ public function getHelp() { $color = new \Colors\Color(); $help = ''; $isNamed = $this->type & self::TYPE_NAMED; if ($isNamed) { $help .= PHP_EOL . (mb_strlen($this->name, 'UTF-8') === 1 ? '-' : '--') . $this->name; if (!empty($this->aliases)) { foreach ($this->aliases as $alias) { $help .= (mb_strlen($alias, 'UTF-8') === 1 ? '/-' : '/--') . $alias; } } if (!$this->isBoolean()) { $help .= ' ' . $color->underline('<argument>'); } $help .= PHP_EOL; } else { $help .= (empty($this->title) ? "arg {$this->name}" : $this->title) . PHP_EOL; } // bold what has been displayed so far $help = $color->bold($help); $titleLine = ''; if ($isNamed && $this->title) { $titleLine .= $this->title . '.'; if ($this->isRequired()) { $titleLine .= ' '; } } if ($this->isRequired()) { $titleLine .= $color->red('Required.'); } if ($titleLine) { $titleLine .= ' '; } $description = $titleLine . $this->description; if (!empty($description)) { $descriptionArray = explode(PHP_EOL, trim($description)); foreach ($descriptionArray as $descriptionLine) { $help .= Terminal::wrap($descriptionLine, 5, 1) . PHP_EOL; } } return $help; }