[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/guzzlehttp/guzzle/src/Subscriber/ -> History.php (source)

   1  <?php
   2  namespace GuzzleHttp\Subscriber;
   3  
   4  use GuzzleHttp\Event\CompleteEvent;
   5  use GuzzleHttp\Event\ErrorEvent;
   6  use GuzzleHttp\Event\RequestEvents;
   7  use GuzzleHttp\Event\SubscriberInterface;
   8  use GuzzleHttp\Message\RequestInterface;
   9  use GuzzleHttp\Message\ResponseInterface;
  10  
  11  /**
  12   * Maintains a list of requests and responses sent using a request or client
  13   */
  14  class History implements SubscriberInterface, \IteratorAggregate, \Countable
  15  {
  16      /** @var int The maximum number of requests to maintain in the history */
  17      private $limit;
  18  
  19      /** @var array Requests and responses that have passed through the plugin */
  20      private $transactions = [];
  21  
  22      public function __construct($limit = 10)
  23      {
  24          $this->limit = $limit;
  25      }
  26  
  27      public function getEvents()
  28      {
  29          return [
  30              'complete' => ['onComplete', RequestEvents::EARLY],
  31              'error'    => ['onError', RequestEvents::EARLY],
  32          ];
  33      }
  34  
  35      /**
  36       * Convert to a string that contains all request and response headers
  37       *
  38       * @return string
  39       */
  40      public function __toString()
  41      {
  42          $lines = array();
  43          foreach ($this->transactions as $entry) {
  44              $response = isset($entry['response']) ? $entry['response'] : '';
  45              $lines[] = '> ' . trim($entry['sent_request'])
  46                  . "\n\n< " . trim($response) . "\n";
  47          }
  48  
  49          return implode("\n", $lines);
  50      }
  51  
  52      public function onComplete(CompleteEvent $event)
  53      {
  54          $this->add($event->getRequest(), $event->getResponse());
  55      }
  56  
  57      public function onError(ErrorEvent $event)
  58      {
  59          // Only track when no response is present, meaning this didn't ever
  60          // emit a complete event
  61          if (!$event->getResponse()) {
  62              $this->add($event->getRequest());
  63          }
  64      }
  65  
  66      /**
  67       * Returns an Iterator that yields associative array values where each
  68       * associative array contains the following key value pairs:
  69       *
  70       * - request: Representing the actual request that was received.
  71       * - sent_request: A clone of the request that will not be mutated.
  72       * - response: The response that was received (if available).
  73       *
  74       * @return \Iterator
  75       */
  76      public function getIterator()
  77      {
  78          return new \ArrayIterator($this->transactions);
  79      }
  80  
  81      /**
  82       * Get all of the requests sent through the plugin.
  83       *
  84       * Requests can be modified after they are logged by the history
  85       * subscriber. By default this method will return the actual request
  86       * instances that were received. Pass true to this method if you wish to
  87       * get copies of the requests that represent the request state when it was
  88       * initially logged by the history subscriber.
  89       *
  90       * @param bool $asSent Set to true to get clones of the requests that have
  91       *                     not been mutated since the request was received by
  92       *                     the history subscriber.
  93       *
  94       * @return RequestInterface[]
  95       */
  96      public function getRequests($asSent = false)
  97      {
  98          return array_map(function ($t) use ($asSent) {
  99              return $asSent ? $t['sent_request'] : $t['request'];
 100          }, $this->transactions);
 101      }
 102  
 103      /**
 104       * Get the number of requests in the history
 105       *
 106       * @return int
 107       */
 108      public function count()
 109      {
 110          return count($this->transactions);
 111      }
 112  
 113      /**
 114       * Get the last request sent.
 115       *
 116       * Requests can be modified after they are logged by the history
 117       * subscriber. By default this method will return the actual request
 118       * instance that was received. Pass true to this method if you wish to get
 119       * a copy of the request that represents the request state when it was
 120       * initially logged by the history subscriber.
 121       *
 122       * @param bool $asSent Set to true to get a clone of the last request that
 123       *                     has not been mutated since the request was received
 124       *                     by the history subscriber.
 125       *
 126       * @return RequestInterface
 127       */
 128      public function getLastRequest($asSent = false)
 129      {
 130          return $asSent
 131              ? end($this->transactions)['sent_request']
 132              : end($this->transactions)['request'];
 133      }
 134  
 135      /**
 136       * Get the last response in the history
 137       *
 138       * @return ResponseInterface|null
 139       */
 140      public function getLastResponse()
 141      {
 142          return end($this->transactions)['response'];
 143      }
 144  
 145      /**
 146       * Clears the history
 147       */
 148      public function clear()
 149      {
 150          $this->transactions = array();
 151      }
 152  
 153      /**
 154       * Add a request to the history
 155       *
 156       * @param RequestInterface  $request  Request to add
 157       * @param ResponseInterface $response Response of the request
 158       */
 159      private function add(
 160          RequestInterface $request,
 161          ResponseInterface $response = null
 162      ) {
 163          $this->transactions[] = [
 164              'request'      => $request,
 165              'sent_request' => clone $request,
 166              'response'     => $response
 167          ];
 168          if (count($this->transactions) > $this->limit) {
 169              array_shift($this->transactions);
 170          }
 171      }
 172  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1