Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

History.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 5.04 KiB


001  <?php
002  namespace GuzzleHttp\Subscriber;
003   
004  use GuzzleHttp\Event\CompleteEvent;
005  use GuzzleHttp\Event\ErrorEvent;
006  use GuzzleHttp\Event\RequestEvents;
007  use GuzzleHttp\Event\SubscriberInterface;
008  use GuzzleHttp\Message\RequestInterface;
009  use GuzzleHttp\Message\ResponseInterface;
010   
011  /**
012   * Maintains a list of requests and responses sent using a request or client
013   */
014  class History implements SubscriberInterface, \IteratorAggregate, \Countable
015  {
016      /** @var int The maximum number of requests to maintain in the history */
017      private $limit;
018   
019      /** @var array Requests and responses that have passed through the plugin */
020      private $transactions = [];
021   
022      public function __construct($limit = 10)
023      {
024          $this->limit = $limit;
025      }
026   
027      public function getEvents()
028      {
029          return [
030              'complete' => ['onComplete', RequestEvents::EARLY],
031              'error'    => ['onError', RequestEvents::EARLY],
032          ];
033      }
034   
035      /**
036       * Convert to a string that contains all request and response headers
037       *
038       * @return string
039       */
040      public function __toString()
041      {
042          $lines = array();
043          foreach ($this->transactions as $entry) {
044              $response = isset($entry['response']) ? $entry['response'] : '';
045              $lines[] = '> ' . trim($entry['sent_request'])
046                  . "\n\n< " . trim($response) . "\n";
047          }
048   
049          return implode("\n", $lines);
050      }
051   
052      public function onComplete(CompleteEvent $event)
053      {
054          $this->add($event->getRequest(), $event->getResponse());
055      }
056   
057      public function onError(ErrorEvent $event)
058      {
059          // Only track when no response is present, meaning this didn't ever
060          // emit a complete event
061          if (!$event->getResponse()) {
062              $this->add($event->getRequest());
063          }
064      }
065   
066      /**
067       * Returns an Iterator that yields associative array values where each
068       * associative array contains the following key value pairs:
069       *
070       * - request: Representing the actual request that was received.
071       * - sent_request: A clone of the request that will not be mutated.
072       * - response: The response that was received (if available).
073       *
074       * @return \Iterator
075       */
076      public function getIterator()
077      {
078          return new \ArrayIterator($this->transactions);
079      }
080   
081      /**
082       * Get all of the requests sent through the plugin.
083       *
084       * Requests can be modified after they are logged by the history
085       * subscriber. By default this method will return the actual request
086       * instances that were received. Pass true to this method if you wish to
087       * get copies of the requests that represent the request state when it was
088       * initially logged by the history subscriber.
089       *
090       * @param bool $asSent Set to true to get clones of the requests that have
091       *                     not been mutated since the request was received by
092       *                     the history subscriber.
093       *
094       * @return RequestInterface[]
095       */
096      public function getRequests($asSent = false)
097      {
098          return array_map(function ($t) use ($asSent) {
099              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  }
173