Docker\API\Resource\MiscResource::getEvents PHP Method

getEvents() public method

Get container events from docker, either in real time via streaming, or via polling (using since).
public getEvents ( array $parameters = [], string $fetch = self::FETCH_OBJECT ) : Psr\Http\Message\ResponseInterface
$parameters array { @var int $since Timestamp used for polling @var int $until Timestamp used for polling @var string $filters A json encoded value of the filters (a map[string][]string) to process on the event list. }
$fetch string Fetch mode (object or response)
return Psr\Http\Message\ResponseInterface
    public function getEvents($parameters = [], $fetch = self::FETCH_OBJECT)
    {
        $queryParam = new QueryParam();
        $queryParam->setDefault('since', null);
        $queryParam->setDefault('until', null);
        $queryParam->setDefault('filters', null);
        $url = '/events';
        $url = $url . ('?' . $queryParam->buildQueryString($parameters));
        $headers = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters));
        $body = $queryParam->buildFormDataString($parameters);
        $request = $this->messageFactory->createRequest('GET', $url, $headers, $body);
        $response = $this->httpClient->sendRequest($request);
        return $response;
    }

Usage Example

Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function getEvents($parameters = [], $fetch = self::FETCH_OBJECT)
 {
     $response = parent::getEvents($parameters, self::FETCH_RESPONSE);
     if (200 === $response->getStatusCode()) {
         if (self::FETCH_STREAM === $fetch) {
             return new EventStream($response->getBody(), $this->serializer);
         }
         if (self::FETCH_OBJECT === $fetch) {
             $eventList = [];
             $stream = new EventStream($response->getBody(), $this->serializer);
             $stream->onFrame(function (Event $event) use(&$eventList) {
                 $eventList[] = $event;
             });
             $stream->wait();
             return $eventList;
         }
     }
     return $response;
 }