/**
  * Load contents of a template, replace placeholders with passed values and save to a target file
  *
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     try {
         $deferred->notify(new Notification("Loading template {$description->getTemplate()}'", Notification::PRIORITY_NORMAL));
         $contents = $this->getFileSystem()->getFileContents($description->getTemplate());
         $keyValuePairs = [];
         foreach ($description->getParams() as $key => $value) {
             $keyValuePairs["{{ " . $key . " }}"] = $value;
         }
         $processedContents = strtr($contents, $keyValuePairs);
         $deferred->notify("File content generated, saving to {$description->getTarget()}");
         if ($this->getFileSystem()->write($description->getTarget(), $processedContents)) {
             $deferred->resolve("Created '{$description->getTarget()}' from template '{$description->getTemplate()}'");
             return;
         }
     } catch (\Exception $e) {
         $deferred->notify("Could not write template: " . $e->getMessage());
     }
     $deferred->reject("Could not create '{$description->getTarget()}' from template '{$description->getTemplate()}'");
 }
Exemplo n.º 2
1
 /**
  * Starts pending tasks end move them from the pendingQueue to the runningQueue
  */
 private function startPendingTasks()
 {
     while (count($this->runningTasks) < $this->concurrencyLimit && !$this->pendingTasks->isEmpty()) {
         /** @var TaskInterface $task */
         $task = $this->pendingTasks->dequeue();
         $task->start()->progress(function (Event $notification) {
             $this->deferred->notify($notification);
         })->always(function () use($task) {
             $this->finishedTasks[] = $task;
             $this->deferred->notify(new TaskFinishedEvent($this, $task));
         });
         $this->deferred->notify(new TaskStartedEvent($this, $task));
         $this->runningTasks->enqueue($task);
     }
 }
 /**
  * Make a directory as described in the task description
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     $deferred->notify(new Notification("Creating directory '{$description->getName()}'", Notification::PRIORITY_NORMAL));
     if ($this->getFileSystem()->isDirectory($description->getName())) {
         $deferred->resolve("Directory '{$description->getName()}' already exists");
         return;
     }
     if ($this->getFileSystem()->makeDirectory($description->getName())) {
         $deferred->resolve("Created directory '{$description->getName()}'");
         return;
     }
     $deferred->reject("Directory '{$description->getName()}' could not be created");
 }
 /**
  * Make a directory as described in the task description
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     $deferred->notify(new Notification("Deleting file or directory '{$description->getName()}'", Notification::PRIORITY_NORMAL));
     if (!$this->getFileSystem()->isFile($description->getName()) && !$this->getFileSystem()->isDirectory($description->getName())) {
         $deferred->resolve("File or directory '{$description->getName()}' does not exist");
         return;
     }
     if ($this->getFileSystem()->delete($description->getName())) {
         $deferred->resolve("Deleted file or directory '{$description->getName()}'");
         return;
     }
     $deferred->reject("File or directory '{$description->getName()}' could not be deleted");
 }
 /**
  * Change the target owner
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     try {
         $deferred->notify(new Notification("Changing '{$description->getTarget()}' owner to '{$description->getOwner()}'", Notification::PRIORITY_NORMAL));
         if ($this->getFileSystem()->changeOwner($description->getTarget(), $description->getOwner())) {
             $deferred->resolve("Changed '{$description->getTarget()}' owner to '{$description->getOwner()}' successfully");
             return;
         }
     } catch (\Exception $e) {
         // do noting, we reject below
         $deferred->reject("Failed to change the owner of {$description->getTarget()}: " . $e->getMessage());
         return;
     }
     $deferred->reject("Failed to change the owner of {$description->getTarget()} to {$description->getOwner()}");
 }
Exemplo n.º 6
1
 private function finishResponse($request, $value, $hash)
 {
     unset($this->waitQueue[$hash]);
     $result = $value instanceof ResponseInterface ? ['request' => $request, 'response' => $value, 'error' => null] : ['request' => $request, 'response' => null, 'error' => $value];
     $this->deferred->notify($result);
 }
Exemplo n.º 7
1
<?php

use React\Promise\Deferred;
require "vendor/autoload.php";
$deferred = new Deferred();
$deferred->promise()->then(function () {
    echo "then\n";
})->always(function () {
    echo "always\n";
})->progress(function () {
    echo "progress\n";
});
$deferred->notify();
$deferred->reject();
$deferred->resolve();
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function tick()
 {
     if ($this->firstTick) {
         $this->firstTick = false;
         $this->deferred->notify(new MessageEvent($this, $this->generator->current()));
     } else {
         $this->deferred->notify(new MessageEvent($this, $this->generator->send(null)));
     }
     if (!$this->generator->valid()) {
         $this->deferred->resolve(new Event($this));
     }
 }
 /**
  * Make a directory as described in the task description
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     $deferred->notify(new Notification("Copying '{$description->getFrom()}' to '{$description->getTo()}'", Notification::PRIORITY_NORMAL));
     try {
         if (!$this->getFileSystem()->copy($description->getFrom(), $description->getTo())) {
             $deferred->notify("Directory permissions were not applied correctly");
         }
     } catch (\Exception $e) {
         $deferred->notify("Copy failed: {$e->getMessage()}");
         $deferred->reject("Could not copy '{$description->getFrom()}' to '{$description->getTo()}'");
         return;
     }
     $deferred->resolve("Copied '{$description->getFrom()}' to '{$description->getTo()}'");
 }
Exemplo n.º 10
0
 public function should($url, $alias = '')
 {
     if (empty($alias)) {
         $alias = $url;
     }
     $request = $this->client->request('GET', $url);
     $this->requests[] = $request;
     $request->on('response', function ($response) use($alias) {
         $response->on('data', function ($data) use($alias) {
             if (empty($data)) {
                 return;
             }
             $this->deferred->notify(['part' => $alias, 'data' => $data]);
         });
     });
     return $this;
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function tick()
 {
     if (!$this->process->isRunning() && $this->outputBuffer->isEmpty()) {
         usleep(1000);
         if ($this->outputBuffer->isEmpty()) {
             if ($this->process->isSuccessful()) {
                 $this->deferred->resolve(new MessageEvent($this, $this->process->getOutput()));
             } else {
                 $this->deferred->reject(new MessageEvent($this, $this->process->getOutput()));
             }
             return;
         }
     }
     if (!$this->outputBuffer->isEmpty()) {
         $this->deferred->notify(new MessageEvent($this, $this->outputBuffer->dequeue()[1]));
     }
 }
 /**
  * Make a directory as described in the task description
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     $deferred->notify(new Notification("Moving '{$description->getFrom()}' to '{$description->getTo()}'", Notification::PRIORITY_NORMAL));
     if ($this->getFileSystem()->move($description->getFrom(), $description->getTo())) {
         $deferred->resolve("Moved '{$description->getFrom()}' to '{$description->getTo()}'");
         return;
     }
     $deferred->reject("Could not move '{$description->getFrom()}' to '{$description->getTo()}'");
 }
 /**
  * Check a file doesn't exist
  * @param Deferred $deferred
  * @param TaskInterface $task
  * @return bool
  */
 protected function processDeferred(Deferred $deferred, TaskInterface $task)
 {
     yield;
     /** @var Description $description */
     $description = $task->getDescription();
     $deferred->notify(new Notification("Checking that file '{$description->getName()}' does not exist", Notification::PRIORITY_NORMAL));
     if (!$this->getFileSystem()->isFile($description->getName())) {
         $deferred->resolve("File '{$description->getName()}' does not exist");
         return;
     }
     $deferred->reject("File '{$description->getName()}' exists");
 }
 /**
  * Calls the given generator periodically and publishes the return value.
  *
  * @param int      $interval
  * @param Message  $message
  * @param callable $generator
  *
  * @return ExtendedPromiseInterface
  */
 public function publishPeriodically($interval, Message $message, callable $generator)
 {
     if (!$this->isConnected) {
         return new RejectedPromise(new \LogicException('The client is not connected.'));
     }
     $deferred = new Deferred();
     $this->timer[] = $this->loop->addPeriodicTimer($interval, function () use($message, $generator, $deferred) {
         $this->publish($message->withPayload($generator($message->getTopic())))->then(function ($value) use($deferred) {
             $deferred->notify($value);
         }, function (\Exception $e) use($deferred) {
             $deferred->reject($e);
         });
     });
     return $deferred->promise();
 }