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

Promise.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 5.47 KiB


 001  <?php
 002   
 003  namespace React\Promise;
 004   
 005  class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface
 006  {
 007      private $canceller;
 008      private $result;
 009   
 010      private $handlers = [];
 011      private $progressHandlers = [];
 012   
 013      private $requiredCancelRequests = 0;
 014      private $cancelRequests = 0;
 015   
 016      public function __construct(callable $resolver, callable $canceller = null)
 017      {
 018          $this->canceller = $canceller;
 019          $this->call($resolver);
 020      }
 021   
 022      public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
 023      {
 024          if (null !== $this->result) {
 025              return $this->result()->then($onFulfilled, $onRejected, $onProgress);
 026          }
 027   
 028          if (null === $this->canceller) {
 029              return new static($this->resolver($onFulfilled, $onRejected, $onProgress));
 030          }
 031   
 032          $this->requiredCancelRequests++;
 033   
 034          return new static($this->resolver($onFulfilled, $onRejected, $onProgress), function () {
 035              if (++$this->cancelRequests < $this->requiredCancelRequests) {
 036                  return;
 037              }
 038   
 039              $this->cancel();
 040          });
 041      }
 042   
 043      public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
 044      {
 045          if (null !== $this->result) {
 046              return $this->result()->done($onFulfilled, $onRejected, $onProgress);
 047          }
 048   
 049          $this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
 050              $promise
 051                  ->done($onFulfilled, $onRejected);
 052          };
 053   
 054          if ($onProgress) {
 055              $this->progressHandlers[] = $onProgress;
 056          }
 057      }
 058   
 059      public function otherwise(callable $onRejected)
 060      {
 061          return $this->then(null, function ($reason) use ($onRejected) {
 062              if (!_checkTypehint($onRejected, $reason)) {
 063                  return new RejectedPromise($reason);
 064              }
 065   
 066              return $onRejected($reason);
 067          });
 068      }
 069   
 070      public function always(callable $onFulfilledOrRejected)
 071      {
 072          return $this->then(function ($value) use ($onFulfilledOrRejected) {
 073              return resolve($onFulfilledOrRejected())->then(function () use ($value) {
 074                  return $value;
 075              });
 076          }, function ($reason) use ($onFulfilledOrRejected) {
 077              return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
 078                  return new RejectedPromise($reason);
 079              });
 080          });
 081      }
 082   
 083      public function progress(callable $onProgress)
 084      {
 085          return $this->then(null, null, $onProgress);
 086      }
 087   
 088      public function cancel()
 089      {
 090          if (null === $this->canceller || null !== $this->result) {
 091              return;
 092          }
 093   
 094          $canceller = $this->canceller;
 095          $this->canceller = null;
 096   
 097          $this->call($canceller);
 098      }
 099   
 100      private function resolver(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
 101      {
 102          return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected, $onProgress) {
 103              if ($onProgress) {
 104                  $progressHandler = function ($update) use ($notify, $onProgress) {
 105                      try {
 106                          $notify($onProgress($update));
 107                      } catch (\Throwable $e) {
 108                          $notify($e);
 109                      } catch (\Exception $e) {
 110                          $notify($e);
 111                      }
 112                  };
 113              } else {
 114                  $progressHandler = $notify;
 115              }
 116   
 117              $this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject, $progressHandler) {
 118                  $promise
 119                      ->then($onFulfilled, $onRejected)
 120                      ->done($resolve, $reject, $progressHandler);
 121              };
 122   
 123              $this->progressHandlers[] = $progressHandler;
 124          };
 125      }
 126   
 127      private function resolve($value = null)
 128      {
 129          if (null !== $this->result) {
 130              return;
 131          }
 132   
 133          $this->settle(resolve($value));
 134      }
 135   
 136      private function reject($reason = null)
 137      {
 138          if (null !== $this->result) {
 139              return;
 140          }
 141   
 142          $this->settle(reject($reason));
 143      }
 144   
 145      private function notify($update = null)
 146      {
 147          if (null !== $this->result) {
 148              return;
 149          }
 150   
 151          foreach ($this->progressHandlers as $handler) {
 152              $handler($update);
 153          }
 154      }
 155   
 156      private function settle(ExtendedPromiseInterface $promise)
 157      {
 158          $handlers = $this->handlers;
 159   
 160          $this->progressHandlers = $this->handlers = [];
 161          $this->result = $promise;
 162   
 163          foreach ($handlers as $handler) {
 164              $handler($promise);
 165          }
 166      }
 167   
 168      private function result()
 169      {
 170          while ($this->result instanceof self && null !== $this->result->result) {
 171              $this->result = $this->result->result;
 172          }
 173   
 174          return $this->result;
 175      }
 176   
 177      private function call(callable $callback)
 178      {
 179          try {
 180              $callback(
 181                  function ($value = null) {
 182                      $this->resolve($value);
 183                  },
 184                  function ($reason = null) {
 185                      $this->reject($reason);
 186                  },
 187                  function ($update = null) {
 188                      $this->notify($update);
 189                  }
 190              );
 191          } catch (\Throwable $e) {
 192              $this->reject($e);
 193          } catch (\Exception $e) {
 194              $this->reject($e);
 195          }
 196      }
 197  }
 198