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

Mock.php

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


001  <?php
002  namespace GuzzleHttp\Subscriber;
003   
004  use GuzzleHttp\Event\RequestEvents;
005  use GuzzleHttp\Event\SubscriberInterface;
006  use GuzzleHttp\Event\BeforeEvent;
007  use GuzzleHttp\Exception\RequestException;
008  use GuzzleHttp\Message\MessageFactory;
009  use GuzzleHttp\Message\ResponseInterface;
010  use GuzzleHttp\Stream\StreamInterface;
011   
012  /**
013   * Queues mock responses or exceptions and delivers mock responses or
014   * exceptions in a fifo order.
015   */
016  class Mock implements SubscriberInterface, \Countable
017  {
018      /** @var array Array of mock responses / exceptions */
019      private $queue = [];
020   
021      /** @var bool Whether or not to consume an entity body when mocking */
022      private $readBodies;
023   
024      /** @var MessageFactory */
025      private $factory;
026   
027      /**
028       * @param array $items      Array of responses or exceptions to queue
029       * @param bool  $readBodies Set to false to not consume the entity body of
030       *                          a request when a mock is served.
031       */
032      public function __construct(array $items = [], $readBodies = true)
033      {
034          $this->factory = new MessageFactory();
035          $this->readBodies = $readBodies;
036          $this->addMultiple($items);
037      }
038   
039      public function getEvents()
040      {
041          // Fire the event last, after signing
042          return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST - 10]];
043      }
044   
045      /**
046       * @throws \OutOfBoundsException|\Exception
047       */
048      public function onBefore(BeforeEvent $event)
049      {
050          if (!$item = array_shift($this->queue)) {
051              throw new \OutOfBoundsException('Mock queue is empty');
052          } elseif ($item instanceof RequestException) {
053              throw $item;
054          }
055   
056          // Emulate reading a response body
057          $request = $event->getRequest();
058          if ($this->readBodies && $request->getBody()) {
059              while (!$request->getBody()->eof()) {
060                  $request->getBody()->read(8096);
061              }
062          }
063   
064          $saveTo = $event->getRequest()->getConfig()->get('save_to');
065   
066          if (null !== $saveTo) {
067              $body = $item->getBody();
068   
069              if (is_resource($saveTo)) {
070                  fwrite($saveTo, $body);
071              } elseif (is_string($saveTo)) {
072                  file_put_contents($saveTo, $body);
073              } elseif ($saveTo instanceof StreamInterface) {
074                  $saveTo->write($body);
075              }
076          }
077   
078          $event->intercept($item);
079      }
080   
081      public function count()
082      {
083          return count($this->queue);
084      }
085   
086      /**
087       * Add a response to the end of the queue
088       *
089       * @param string|ResponseInterface $response Response or path to response file
090       *
091       * @return self
092       * @throws \InvalidArgumentException if a string or Response is not passed
093       */
094      public function addResponse($response)
095      {
096          if (is_string($response)) {
097              $response = file_exists($response)
098                  ? $this->factory->fromMessage(file_get_contents($response))
099                  : $this->factory->fromMessage($response);
100          } elseif (!($response instanceof ResponseInterface)) {
101              throw new \InvalidArgumentException('Response must a message '
102                  . 'string, response object, or path to a file');
103          }
104   
105          $this->queue[] = $response;
106   
107          return $this;
108      }
109   
110      /**
111       * Add an exception to the end of the queue
112       *
113       * @param RequestException $e Exception to throw when the request is executed
114       *
115       * @return self
116       */
117      public function addException(RequestException $e)
118      {
119          $this->queue[] = $e;
120   
121          return $this;
122      }
123   
124      /**
125       * Add multiple items to the queue
126       *
127       * @param array $items Items to add
128       */
129      public function addMultiple(array $items)
130      {
131          foreach ($items as $item) {
132              if ($item instanceof RequestException) {
133                  $this->addException($item);
134              } else {
135                  $this->addResponse($item);
136              }
137          }
138      }
139   
140      /**
141       * Clear the queue
142       */
143      public function clearQueue()
144      {
145          $this->queue = [];
146      }
147  }
148