From f65e9c160e8162573b8dfffc2e84402173069e69 Mon Sep 17 00:00:00 2001 From: VennV <111500380+VennDev@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:36:05 +0700 Subject: [PATCH] Delete src/vennv directory --- src/vennv/AggregateError.php | 50 --- src/vennv/AssumptionFailedError.php | 50 --- src/vennv/Async.php | 145 -------- src/vennv/AsyncError.php | 50 --- src/vennv/Error.php | 44 --- src/vennv/EventQueue.php | 403 -------------------- src/vennv/EventQueueError.php | 50 --- src/vennv/Info.php | 40 -- src/vennv/InterfaceAsync.php | 53 --- src/vennv/InterfaceEventQueue.php | 85 ----- src/vennv/InterfacePromise.php | 98 ----- src/vennv/InterfaceQueue.php | 212 ----------- src/vennv/InterfaceSystem.php | 83 ----- src/vennv/Internet.php | 225 ------------ src/vennv/InternetException.php | 50 --- src/vennv/InternetRequestResult.php | 60 --- src/vennv/Promise.php | 197 ---------- src/vennv/PromiseException.php | 50 --- src/vennv/PromiseResult.php | 48 --- src/vennv/Queue.php | 552 ---------------------------- src/vennv/QueueError.php | 50 --- src/vennv/SamplePromise.php | 48 --- src/vennv/StatusQueue.php | 36 -- src/vennv/System.php | 141 ------- src/vennv/Utils.php | 37 -- 25 files changed, 2857 deletions(-) delete mode 100644 src/vennv/AggregateError.php delete mode 100644 src/vennv/AssumptionFailedError.php delete mode 100644 src/vennv/Async.php delete mode 100644 src/vennv/AsyncError.php delete mode 100644 src/vennv/Error.php delete mode 100644 src/vennv/EventQueue.php delete mode 100644 src/vennv/EventQueueError.php delete mode 100644 src/vennv/Info.php delete mode 100644 src/vennv/InterfaceAsync.php delete mode 100644 src/vennv/InterfaceEventQueue.php delete mode 100644 src/vennv/InterfacePromise.php delete mode 100644 src/vennv/InterfaceQueue.php delete mode 100644 src/vennv/InterfaceSystem.php delete mode 100644 src/vennv/Internet.php delete mode 100644 src/vennv/InternetException.php delete mode 100644 src/vennv/InternetRequestResult.php delete mode 100644 src/vennv/Promise.php delete mode 100644 src/vennv/PromiseException.php delete mode 100644 src/vennv/PromiseResult.php delete mode 100644 src/vennv/Queue.php delete mode 100644 src/vennv/QueueError.php delete mode 100644 src/vennv/SamplePromise.php delete mode 100644 src/vennv/StatusQueue.php delete mode 100644 src/vennv/System.php delete mode 100644 src/vennv/Utils.php diff --git a/src/vennv/AggregateError.php b/src/vennv/AggregateError.php deleted file mode 100644 index 18e38aa99..000000000 --- a/src/vennv/AggregateError.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} \ No newline at end of file diff --git a/src/vennv/AssumptionFailedError.php b/src/vennv/AssumptionFailedError.php deleted file mode 100644 index 790ed49c2..000000000 --- a/src/vennv/AssumptionFailedError.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} diff --git a/src/vennv/Async.php b/src/vennv/Async.php deleted file mode 100644 index 63ca59f79..000000000 --- a/src/vennv/Async.php +++ /dev/null @@ -1,145 +0,0 @@ -id = EventQueue::addQueue( - new Fiber($callable), - $callable, - false, - false, - 0.0 - ); - - $queue = EventQueue::getQueue($this->id); - - if (!is_null($queue)) - { - $fiber = $queue->getFiber(); - if (!$fiber->isStarted()) - { - try - { - $fiber->start(); - } - catch (Exception | Throwable $error) - { - EventQueue::rejectQueue($this->id, $error->getMessage()); - } - } - } - } - - /** - * @throws Throwable - */ - public static function await(callable|Promise|Async $callable) : mixed - { - $result = $callable; - - if (is_callable($callable)) - { - $fiber = new Fiber($callable); - $fiber->start(); - - if (!$fiber->isTerminated()) - { - self::wait(); - } - - $result = $fiber->getReturn(); - } - - if ($callable instanceof Promise || $callable instanceof Async) - { - self::awaitPromise($callable, $result); - } - - if ($result instanceof Promise || $result instanceof Async) - { - self::awaitPromise($result, $result); - } - - return $result; - } - - /** - * @throws Throwable - */ - private static function awaitPromise(Async|Promise $promise, mixed &$result) : void - { - $queue = EventQueue::getQueue($promise->getId()); - - if (!is_null($queue)) - { - while ($queue->getStatus() == StatusQueue::PENDING) - { - if ( - $queue->getStatus() == StatusQueue::REJECTED || - $queue->getStatus() == StatusQueue::FULFILLED - ) - { - break; - } - self::wait(); - } - - $result = $queue->getReturn(); - } - } - - /** - * @throws Throwable - */ - public static function wait() : void - { - $fiber = Fiber::getCurrent(); - if (!is_null($fiber)) - { - Fiber::suspend(); - } - } - - public function getId() : int - { - return $this->id; - } - -} \ No newline at end of file diff --git a/src/vennv/AsyncError.php b/src/vennv/AsyncError.php deleted file mode 100644 index eefbbbc2f..000000000 --- a/src/vennv/AsyncError.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} diff --git a/src/vennv/Error.php b/src/vennv/Error.php deleted file mode 100644 index 6eba4acee..000000000 --- a/src/vennv/Error.php +++ /dev/null @@ -1,44 +0,0 @@ -= PHP_INT_MAX) - { - self::$nextId = 0; - } - return self::$nextId++; - } - - public static function getNextId() : int - { - if (self::$nextId >= PHP_INT_MAX) - { - self::$nextId = 0; - } - return self::$nextId + 1; - } - - public static function isMaxId() : bool - { - return self::$nextId >= PHP_INT_MAX; - } - - /** - * @throws Throwable - */ - public static function addQueue( - Fiber $fiber, - callable $promiseCallable, - bool $isPromise = false, - bool $isRepeatable = false, - float $timeOut = 0.0 - ) : int - { - $id = self::generateId(); - - self::$queues[$id] = new Queue( - $id, - $fiber, - $promiseCallable, - $timeOut, - StatusQueue::PENDING, - $isPromise, - $isRepeatable - ); - - return $id; - } - - public static function getQueue(int $id) : ?Queue - { - return self::$queues[$id] ?? null; - } - - public static function getReturn(int $id) : ?Queue - { - return self::$returns[$id] ?? null; - } - - public static function unsetReturn(int $id) : void - { - unset(self::$returns[$id]); - } - - private static function getResultFiber(Fiber $fiber) : mixed - { - try - { - $result = $fiber->getReturn(); - } - catch (FiberError $error) - { - $result = $error->getMessage(); - } - return $result; - } - - /** - * @throws Throwable - */ - private static function doResult(int $id) : void - { - $queue = self::getQueue($id); - if (!is_null($queue)) - { - $status = $queue->getStatus(); - $result = $queue->getReturn(); - - switch ($status) - { - case StatusQueue::FULFILLED: - $queue->useCallableResolve($result); - break; - case StatusQueue::REJECTED: - $queue->useCallableReject($result); - break; - case StatusQueue::PENDING: - throw new EventQueueError( - str_replace("%id%", "$id", Error::QUEUE_STILL_PENDING) - ); - } - - $callablePromise = $queue->getPromiseCallable(); - - if ($queue->isRepeatable() && is_callable($callablePromise)) - { - $fiber = new Fiber($callablePromise); - - self::addQueue( - $fiber, - $callablePromise, - $queue->isPromise(), - $queue->isRepeatable(), - $queue->getTimeOut() - ); - } - - self::$returns[$id] = $queue; - unset(self::$queues[$id]); - } - else - { - throw new EventQueueError( - str_replace("%id%", "$id", Error::QUEUE_NOT_FOUND) - ); - } - } - - /** - * @throws Throwable - */ - public static function runQueue(int $id) : void - { - $queue = self::getQueue($id); - if ($queue !== null) - { - $fiber = $queue->getFiber(); - if (!$fiber->isStarted()) - { - try - { - $fiber->start(); - } - catch (Exception | Throwable $error) - { - EventQueue::rejectQueue($id, $error->getMessage()); - } - } - } - } - - /** - * @throws Throwable - */ - public static function rejectQueue(int $id, mixed $result) : void - { - $queue = self::getQueue($id); - if (!is_null($queue)) - { - $queue->setStatus(StatusQueue::REJECTED); - $queue->setReturn($result); - self::doResult($id); - } - } - - /** - * @throws Throwable - */ - public static function fulfillQueue(int $id, mixed $result) : void - { - $queue = self::getQueue($id); - if (!is_null($queue)) - { - $queue->setStatus(StatusQueue::FULFILLED); - $queue->setReturn($result); - self::doResult($id); - } - } - - /** - * @throws Throwable - */ - public static function fulfillPromise(int $id) : void - { - $queue = self::getQueue($id); - if (!is_null($queue)) - { - self::doResult($id); - } - } - - /** - * @throws Throwable - */ - private static function checkStatus(int $id) : void - { - $queue = self::getQueue($id); - - if (!is_null($queue)) - { - $id = $queue->getId(); - $fiber = $queue->getFiber(); - $isPromise = $queue->isPromise(); - - if ($isPromise) - { - $resolve = function($result) use ($id) - { - Promise::resolve($id, $result); - }; - - $reject = function($result) use ($id) - { - Promise::reject($id, $result); - }; - } - - if (!$fiber->isStarted() && !$isPromise) - { - try - { - $fiber->start(); - } - catch (Exception | Throwable $error) - { - self::rejectQueue($id, $error->getMessage()); - } - } - - if (!$fiber->isStarted() && $isPromise) - { - try - { - $fiber->start($resolve, $reject); - } - catch (Exception | Throwable $error) - { - self::rejectQueue($id, $error->getMessage()); - } - } - - if ($fiber->isSuspended()) - { - try - { - $fiber->resume(); - } - catch (Exception | Throwable $error) - { - self::rejectQueue($id, $error->getMessage()); - } - } - - if ($fiber->isTerminated()) - { - if (!$isPromise) - { - self::fulfillQueue($id, self::getResultFiber($fiber)); - } - else - { - $specialPromise = - $queue->isPromiseAll() || - $queue->isAllSettled() || - $queue->isRacePromise() || - $queue->isAnyPromise(); - - if ($specialPromise) - { - if ($queue->hasCompletedAllPromises()) - { - self::fulfillPromise($id); - } - } - elseif ($queue->getStatus() !== StatusQueue::PENDING) - { - self::fulfillPromise($id); - } - } - } - } - else - { - throw new EventQueueError( - str_replace("%id%", "$id", Error::QUEUE_NOT_FOUND) - ); - } - } - - private static function shouldCheckStatus(Queue $queue, float $addTime = 0.0) : bool - { - $timeOut = $queue->getTimeOut(); - $timeStart = $queue->getTimeStart(); - $timeNow = microtime(true); - - $diff = $timeNow - $timeStart; - - return $diff >= $timeOut + $addTime; - } - - /** - * @throws Throwable - */ - private static function run() : void - { - foreach (self::$queues as $id => $queue) - { - if (self::shouldCheckStatus($queue)) - { - self::checkStatus($id); - } - - // If the queue is still pending after 10 seconds of timeout, reject it. - // If you encounter this problem, your current promise trick is too bad. - if (self::shouldCheckStatus($queue, self::TIME_OUT)) - { - self::rejectQueue( - $id, str_replace("%id%", "$id", Error::QUEUE_IS_TIMEOUT) - ); - } - } - - foreach (self::$returns as $id => $queue) - { - $canDrop = $queue->canDrop(); - - if ($canDrop) - { - unset(self::$returns[$id]); - } - } - } - - /** - * @throws Throwable - */ - protected static function runSingleJob() : void - { - while (count(self::$queues) > 0) - { - self::run(); - } - } - - /** - * @throws Throwable - */ - protected static function runMultiJobs() : void - { - self::run(); - } - -} \ No newline at end of file diff --git a/src/vennv/EventQueueError.php b/src/vennv/EventQueueError.php deleted file mode 100644 index 55d1bf3f6..000000000 --- a/src/vennv/EventQueueError.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} diff --git a/src/vennv/Info.php b/src/vennv/Info.php deleted file mode 100644 index f388ccfee..000000000 --- a/src/vennv/Info.php +++ /dev/null @@ -1,40 +0,0 @@ - $promises - * - * Fulfills when all the promises fulfill, rejects when any of the promises rejects. - */ - public static function all(array $promises) : Promise; - - /** - * @throws Throwable - * @param array $promises - * - * Settles when any of the promises settles. - * In other words, fulfills when any of the promises fulfills, rejects when any of the promises rejects. - */ - public static function race(array $promises) : Promise; - - /** - * @throws Throwable - * @param array $promises - * - * Fulfills when any of the promises fulfills, rejects when all the promises reject. - */ - public static function any(array $promises) : Promise; - - /** - * @throws Throwable - * @param array $promises - * - * Fulfills when all promises settle. - */ - public static function allSettled(array $promises) : Promise; - - /** - * @throws Throwable - * - * This method is used to get the id of the promise. - */ - public function getId() : int; - -} \ No newline at end of file diff --git a/src/vennv/InterfaceQueue.php b/src/vennv/InterfaceQueue.php deleted file mode 100644 index d1e928adf..000000000 --- a/src/vennv/InterfaceQueue.php +++ /dev/null @@ -1,212 +0,0 @@ - - * - * This method to get waiting promises. - */ - public function getWaitingPromises() : array; - - /** - * @param array $waitingPromises - * - * This method to set waiting promises. - */ - public function setWaitingPromises(array $waitingPromises) : void; - - /** - * This method to check if the queue is repeatable. - */ - public function isRepeatable() : bool; - - /** - * This method to set the queue is a promise race. - */ - public function setRacePromise(bool $isRacePromise) : void; - - /** - * This method to check if the queue is a promise race. - */ - public function isRacePromise() : bool; - - /** - * This method to set the queue is a promise any. - */ - public function setAnyPromise(bool $isAnyPromise) : void; - - /** - * This method to check if the queue is a promise any. - */ - public function isAnyPromise() : bool; - - /** - * This method to set the queue is a promise all settled. - */ - public function setAllSettled(bool $isAllSettled) : void; - - /** - * This method to check if the queue is a promise all settled. - */ - public function isAllSettled() : bool; - - /** - * This method check if the queue is a promise for all. - */ - public function isPromiseAll() : bool; - - /** - * This method to set the queue is a promise all. - */ - public function setPromiseAll(bool $isPromiseAll) : void; - - /** - * @return mixed - * - * This method to get the Callable. - */ - public function getPromiseCallable() : mixed; - - /** - * @throws Throwable - * - * This method to check if the queue has completed all promises. - */ - public function hasCompletedAllPromises() : bool; - -} \ No newline at end of file diff --git a/src/vennv/InterfaceSystem.php b/src/vennv/InterfaceSystem.php deleted file mode 100644 index d2ac224bb..000000000 --- a/src/vennv/InterfaceSystem.php +++ /dev/null @@ -1,83 +0,0 @@ - $options - * @return Promise when Promise resolve InternetRequestResult and when Promise reject Error - * @throws Throwable - * @phpstan-param array{method?: string, headers?: array, timeout?: int, body?: array} $options - * - * This method is used to fetch data from an url. - */ - public static function fetch(string $url, array $options = []) : Promise; - - /** - * @throws Throwable - * This method is used to fetch data from an url. But it uses file_get_contents() instead of curl. - */ - public static function fetchJg(string $url) : Promise; - - /** - * @throws Throwable - * - * This method is used to run a single job. - * It is used when you want to run the event loop in a blocking way. - */ - public static function endSingleJob() : void; - - /** - * @throws Throwable - * - * This method is usually used at the end of the whole chunk of your program, - * it is used to run the event loop. - * - * This method is used when you want to run the event loop in a non-blocking way. - * You should run this method in a separate thread and make it repeat every second. - */ - public static function endMultiJobs() : void; - -} \ No newline at end of file diff --git a/src/vennv/Internet.php b/src/vennv/Internet.php deleted file mode 100644 index a7d23f0e2..000000000 --- a/src/vennv/Internet.php +++ /dev/null @@ -1,225 +0,0 @@ -getMessage(); - return null; - } - } - - /** - * POSTs data to a URL - * NOTE: This is a blocking operation and can take a significant amount of time. It is inadvisable to use this method on the main thread. - * - * @param string[]|string $args - * @param string[] $extraHeaders - * @param string|null $error reference parameter, will be set to the output of curl_error(). Use this to retrieve errors that occurred during the operation. - */ - public static function postURL( - string $page, - array|string $args, - int $timeout = 10, - array $extraHeaders = [], - string &$error = null - ) : ?InternetRequestResult - { - try - { - return self::simpleCurl($page, $timeout, $extraHeaders, [ - CURLOPT_POST => 1, - CURLOPT_POSTFIELDS => $args - ]); - } - catch (InternetException $ex) - { - $error = $ex->getMessage(); - return null; - } - } - - /** - * General cURL shorthand function. - * NOTE: This is a blocking operation and can take a significant amount of time. It is inadvisable to use this method on the main thread. - * - * @param float $timeout The maximum connect timeout and timeout in seconds, correct to ms. - * @param string[] $extraHeaders extra headers to send as a plain string array - * @param array $extraOpts extra CURL-OPT_* to set as an [opt => value] map - * @param Closure|null $onSuccess function to be called if there is no error. Accepts a resource argument as the cURL handle. - * @phpstan-param array $extraOpts - * @phpstan-param list $extraHeaders - * @phpstan-param (Closure(CurlHandle) : void)|null $onSuccess - * - * @throws InternetException if a cURL error occurs - */ - public static function simpleCurl( - string $page, - float $timeout = 10, - array $extraHeaders = [], - array $extraOpts = [], - ?Closure $onSuccess = null - ) : InternetRequestResult - { - - $time = (int) ($timeout * 1000); - - $curlHandle = curl_init($page); - - if ($curlHandle === false) - { - throw new InternetException( - "Unable to create new cURL session" - ); - } - - curl_setopt_array($curlHandle, $extraOpts + - [ - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_SSL_VERIFYHOST => 2, - CURLOPT_FORBID_REUSE => 1, - CURLOPT_FRESH_CONNECT => 1, - CURLOPT_AUTOREFERER => true, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_CONNECTTIMEOUT_MS => $time, - CURLOPT_TIMEOUT_MS => $time, - CURLOPT_HTTPHEADER => array_merge( - ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"], - $extraHeaders - ), - CURLOPT_HEADER => true - ]); - - try - { - $raw = curl_exec($curlHandle); - - if ($raw === false) - { - throw new InternetException(curl_error($curlHandle)); - } - - if (!is_string($raw)) - { - throw new AssumptionFailedError(Error::WRONG_TYPE_WHEN_USE_CURL_EXEC); - } - - $httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); - $headerSize = curl_getinfo($curlHandle, CURLINFO_HEADER_SIZE); - $rawHeaders = substr($raw, 0, $headerSize); - $body = substr($raw, $headerSize); - $headers = []; - - foreach (explode("\r\n\r\n", $rawHeaders) as $rawHeaderGroup) - { - $headerGroup = []; - - foreach (explode("\r\n", $rawHeaderGroup) as $line) - { - $nameValue = explode(":", $line, 2); - - if (isset($nameValue[1])) - { - $headerGroup[trim(strtolower($nameValue[0]))] = trim($nameValue[1]); - } - - } - - $headers[] = $headerGroup; - } - - if (!is_null($onSuccess)) - { - $onSuccess($curlHandle); - } - - return new InternetRequestResult($headers, $body, $httpCode); - } - finally - { - curl_close($curlHandle); - } - } - -} diff --git a/src/vennv/InternetException.php b/src/vennv/InternetException.php deleted file mode 100644 index ae9cfd766..000000000 --- a/src/vennv/InternetException.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} \ No newline at end of file diff --git a/src/vennv/InternetRequestResult.php b/src/vennv/InternetRequestResult.php deleted file mode 100644 index 2c174d6cd..000000000 --- a/src/vennv/InternetRequestResult.php +++ /dev/null @@ -1,60 +0,0 @@ -headers; - } - - public function getBody() : string - { - return $this->body; - } - - public function getCode() : int - { - return $this->code; - } - -} diff --git a/src/vennv/Promise.php b/src/vennv/Promise.php deleted file mode 100644 index 3b1f14204..000000000 --- a/src/vennv/Promise.php +++ /dev/null @@ -1,197 +0,0 @@ -id = EventQueue::addQueue( - $fiber, - $callable, - true, - false, - 0.0 - ); - } - - public function then(callable $callable) : ?Queue - { - $queue = EventQueue::getQueue($this->id); - - if (!is_null($queue)) - { - $queue->setCallableResolve($callable); - return $queue->thenPromise($callable); - } - - return null; - } - - public function catch(callable $callable) : ?Queue - { - $queue = EventQueue::getQueue($this->id); - - if (!is_null($queue)) - { - $queue->setCallableReject($callable); - return $queue->catchPromise($callable); - } - - return null; - } - - public static function resolve(int $id, mixed $result) : void - { - $queue = EventQueue::getQueue($id); - - if (!is_null($queue)) - { - $queue->setReturn($result); - $queue->setStatus(StatusQueue::FULFILLED); - } - } - - public static function reject(int $id, mixed $result) : void - { - $queue = EventQueue::getQueue($id); - - if (!is_null($queue)) - { - $queue->setReturn($result); - $queue->setStatus(StatusQueue::REJECTED); - } - } - - /** - * @throws Throwable - * @param array $promises - */ - public static function all(array $promises) : Promise - { - $samplePromise = self::createEmptyPromise(); - - $queue = $samplePromise->getQueue(); - $promise = $samplePromise->getPromise(); - - if (!is_null($queue)) - { - $queue->setWaitingPromises($promises); - $queue->setPromiseAll(true); - } - - return $promise; - } - - /** - * @throws Throwable - * @param array $promises - */ - public static function race(array $promises) : Promise - { - $samplePromise = self::createEmptyPromise(); - - $queue = $samplePromise->getQueue(); - $promise = $samplePromise->getPromise(); - - if (!is_null($queue)) - { - $queue->setWaitingPromises($promises); - $queue->setRacePromise(true); - } - - return $promise; - } - - /** - * @throws Throwable - * @param array $promises - */ - public static function any(array $promises) : Promise - { - $samplePromise = self::createEmptyPromise(); - - $queue = $samplePromise->getQueue(); - $promise = $samplePromise->getPromise(); - - if (!is_null($queue)) - { - $queue->setWaitingPromises($promises); - $queue->setAnyPromise(true); - } - - return $promise; - } - - /** - * @throws Throwable - * @param array $promises - */ - public static function allSettled(array $promises) : Promise - { - $samplePromise = self::createEmptyPromise(); - - $queue = $samplePromise->getQueue(); - $promise = $samplePromise->getPromise(); - - if (!is_null($queue)) - { - $queue->setWaitingPromises($promises); - $queue->setAllSettled(true); - } - - return $promise; - } - - /** - * @throws Throwable - */ - private static function createEmptyPromise() : SamplePromise - { - $promise = new Promise(function($resolve, $reject) {}); - $queue = EventQueue::getQueue($promise->getId()); - - return new SamplePromise($promise, $queue); - } - - public function getId() : int - { - return $this->id; - } - -} \ No newline at end of file diff --git a/src/vennv/PromiseException.php b/src/vennv/PromiseException.php deleted file mode 100644 index 31de92726..000000000 --- a/src/vennv/PromiseException.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} \ No newline at end of file diff --git a/src/vennv/PromiseResult.php b/src/vennv/PromiseResult.php deleted file mode 100644 index 587aa8428..000000000 --- a/src/vennv/PromiseResult.php +++ /dev/null @@ -1,48 +0,0 @@ -result; - } - - public function getStatus() : StatusQueue - { - return $this->status; - } - -} \ No newline at end of file diff --git a/src/vennv/Queue.php b/src/vennv/Queue.php deleted file mode 100644 index 60ae8b750..000000000 --- a/src/vennv/Queue.php +++ /dev/null @@ -1,552 +0,0 @@ - - */ - private array $callableResolve; - - /** - * @var array - */ - private array $callableReject; - - private mixed $returnResolve; - - private mixed $returnReject; - - /** - * @var array - */ - private array $waitingPromises = []; - - private bool $isRacePromise = false; - - private bool $isAnyPromise = false; - - private bool $isAllSettled = false; - - private bool $isPromiseAll = false; - - - public function __construct( - private readonly int $id, - private readonly Fiber $fiber, - private readonly mixed $promiseCallable, - private readonly float $timeOut, - private StatusQueue $status, - private readonly bool $isPromise, - private readonly bool $isRepeatable - ) - { - $this->timeStart = microtime(true); - $this->timeDrop = 15; - $this->return = null; - $this->callableResolve[self::MAIN_QUEUE] = function($result) {}; - $this->callableReject[self::MAIN_QUEUE] = function($result) {}; - $this->returnResolve = null; - $this->returnReject = null; - } - - public function getId() : int - { - return $this->id; - } - - public function getFiber() : Fiber - { - return $this->fiber; - } - - public function getTimeOut() : float - { - return $this->timeOut; - } - - public function getStatus() : StatusQueue - { - return $this->status; - } - - public function setStatus(StatusQueue $status) : void - { - $this->status = $status; - } - - public function isPromise() : bool - { - return $this->isPromise; - } - - public function getTimeStart() : float - { - return $this->timeStart; - } - - public function getReturn() : mixed - { - return $this->return; - } - - public function setReturn(mixed $return) : void - { - $this->return = $return; - } - - private function getResult(Fiber $fiber) : mixed - { - $timeStart = microtime(true); - - while (!$fiber->isTerminated()) - { - if (microtime(true) - $timeStart > $this->timeDrop) - { - break; - } - } - - try - { - $result = $fiber->getReturn(); - } - catch (Exception | Throwable $error) - { - $result = $error->getMessage(); - $this->callableResolve = []; - $this->callableReject = []; - } - - return $result; - } - - /** - * @throws Throwable - * @param array $callableFc - */ - private function checkStatus(array $callableFc, mixed $return) : void - { - while (count($callableFc) > 0) - { - $firstCheck = false; - $cancel = false; - - foreach ($callableFc as $id => $callable) - { - if ($return === null) - { - $cancel = true; - break; - } - if ( - $id !== self::MAIN_QUEUE && - $return instanceof Promise && - !$firstCheck - ) - { - $queue = EventQueue::getQueue($return->getId()); - - if (!is_null($queue)) - { - $queue->setCallableResolve($callable); - } - $firstCheck = true; - } - elseif ( - $id !== self::MAIN_QUEUE && - $return instanceof Promise - ) - { - $queue = EventQueue::getQueue($return->getId()); - - if (!is_null($queue)) { - $queue->then($callable); - } - - unset($callableFc[$id]); - continue; - } - if (count($callableFc) === 1) - { - $cancel = true; - } - } - - if ($cancel) - { - break; - } - } - } - - /** - * @throws Throwable - */ - public function useCallableResolve(mixed $result) : void - { - if ($this->getStatus() === StatusQueue::FULFILLED) - { - - $fiber = new Fiber(function() use ($result) { - return ($this->callableResolve[self::MAIN_QUEUE])($result); - }); - - // try - // { - // $fiber->start(); - // } - // catch (Throwable | Exception $error) - // { - // throw new QueueError($error->getMessage()); - // } - $fiber->start(); - - unset($this->callableResolve[self::MAIN_QUEUE]); - - $this->returnResolve = $this->getResult($fiber); - - $this->checkStatus($this->callableResolve, $this->returnResolve); - } - } - - public function setCallableResolve(callable $callableResolve) : Queue - { - $this->callableResolve[self::MAIN_QUEUE] = $callableResolve; - return $this; - } - - /** - * @throws Throwable - */ - public function useCallableReject(mixed $result) : void - { - if ($this->getStatus() === StatusQueue::REJECTED) - { - $fiber = new Fiber(function () use ($result) { - return ($this->callableReject[self::MAIN_QUEUE])($result); - }); - - // try - // { - // $fiber->start(); - // } - // catch (Throwable | Exception $error) - // { - // throw new QueueError($error->getMessage()); - // } - $fiber->start(); - - $this->returnReject = $this->getResult($fiber); - - $this->checkStatus($this->callableReject, $this->returnReject); - } - } - - public function setCallableReject(callable $callableReject) : Queue - { - $this->callableReject[self::MAIN_QUEUE] = $callableReject; - return $this; - } - - public function getReturnResolve() : mixed - { - return $this->returnResolve; - } - - public function getReturnReject() : mixed - { - return $this->returnReject; - } - - public function thenPromise(callable $callable) : Queue - { - $this->setCallableResolve($callable); - return $this; - } - - public function catchPromise(callable $callable) : Queue - { - $this->setCallableReject($callable); - return $this; - } - - public function then(callable $callable) : Queue - { - $this->callableResolve[] = $callable; - return $this; - } - - public function catch(callable $callable) : Queue - { - $this->setCallableReject($callable); - return $this; - } - - public function canDrop() : bool - { - return (microtime(true) - $this->timeStart) > $this->timeDrop; - } - - /** @return array */ - public function getWaitingPromises() : array - { - return $this->waitingPromises; - } - - /** - * @param array $waitingPromises - */ - public function setWaitingPromises(array $waitingPromises) : void - { - $this->waitingPromises = $waitingPromises; - } - - public function isRepeatable() : bool - { - return $this->isRepeatable; - } - - public function setRacePromise(bool $isRacePromise) : void - { - $this->isRacePromise = $isRacePromise; - } - - public function isRacePromise() : bool - { - return $this->isRacePromise; - } - - public function setAnyPromise(bool $isAnyPromise) : void - { - $this->isAnyPromise = $isAnyPromise; - } - - public function isAnyPromise() : bool - { - return $this->isAnyPromise; - } - - public function setAllSettled(bool $isAllSettled) : void - { - $this->isAllSettled = $isAllSettled; - } - - public function isAllSettled() : bool - { - return $this->isAllSettled; - } - - public function isPromiseAll() : bool - { - return $this->isPromiseAll; - } - - public function setPromiseAll(bool $isPromiseAll) : void - { - $this->isPromiseAll = $isPromiseAll; - } - - public function getPromiseCallable() : mixed - { - return $this->promiseCallable; - } - - /** - * @throws Throwable - * @return array - */ - private function getResultPromise(Async|Promise $promise) : array - { - $results = []; - $queue = EventQueue::getReturn($promise->getId()); - - if (!is_null($queue)) - { - if ($queue->getStatus() === StatusQueue::FULFILLED) - { - $results[] = new PromiseResult($queue->getReturn(), $queue->getStatus()); - } - - if ($queue->getStatus() === StatusQueue::REJECTED) - { - $results[] = new PromiseResult($queue->getReturn(), $queue->getStatus()); - } - } - - return $results; - } - - /** - * @throws Throwable - */ - public function hasCompletedAllPromises() : bool - { - $return = false; - $results = []; - - foreach ($this->waitingPromises as $value) - { - $result = $value; - - if (is_callable($value)) - { - $fiber = new Fiber($value); - $fiber->start(); - - $timeStart = microtime(true); - - while (!$fiber->isTerminated()) - { - if ((microtime(true) - $timeStart) > $this->timeDrop) - { - break; - } - } - - $result = $fiber->getReturn(); - } - - if ($value instanceof Promise || $value instanceof Async) - { - $results = array_merge($results, $this->getResultPromise($value)); - } - elseif ($result instanceof Promise || $result instanceof Async) - { - $results = array_merge($results, $this->getResultPromise($result)); - } - } - - if (count($results) >= count($this->waitingPromises) && $this->isAllSettled()) - { - $resultPromise = []; - - foreach ($results as $result) - { - $resultPromise[] = $result->getResult(); - } - - $this->return = $resultPromise; - $this->setStatus(StatusQueue::FULFILLED); - $return = true; - } - - if (count($results) >= count($this->waitingPromises) && $this->isPromiseAll()) - { - $haveRejected = false; - - foreach ($results as $result) - { - if ($result->getStatus() === StatusQueue::REJECTED) - { - $this->return = $result->getResult(); - $this->setStatus(StatusQueue::REJECTED); - $return = true; - $haveRejected = true; - break; - } - } - - $resultPromise = []; - if (!$haveRejected) - { - foreach ($results as $result) - { - $resultPromise[] = $result->getResult(); - } - - $this->return = $resultPromise; - $this->setStatus(StatusQueue::FULFILLED); - $return = true; - } - } - - if (count($results) > 0 && $this->isRacePromise()) - { - $this->return = $results[0]->getResult(); - - if ($results[0]->getStatus() === StatusQueue::FULFILLED) - { - $this->setStatus(StatusQueue::FULFILLED); - } - - if ($results[0]->getStatus() === StatusQueue::REJECTED) - { - $this->setStatus(StatusQueue::REJECTED); - } - - $return = true; - } - - if (count($results) > 0 && $this->isAnyPromise()) - { - $countRejected = 0; - - foreach ($results as $result) - { - if ($result->getStatus() === StatusQueue::FULFILLED) - { - $this->return = $result->getResult(); - $this->setStatus(StatusQueue::FULFILLED); - $return = true; - break; - } - else - { - $countRejected++; - } - } - - if ($countRejected === count($this->waitingPromises)) - { - $error = new AggregateError(Error::ALL_PROMISES_WERE_REJECTED); - trigger_error($error->__toString(), E_USER_WARNING); - - $this->return = $error->__toString(); - $this->setStatus(StatusQueue::REJECTED); - $return = true; - } - } - - return $return; - } - -} \ No newline at end of file diff --git a/src/vennv/QueueError.php b/src/vennv/QueueError.php deleted file mode 100644 index abc278588..000000000 --- a/src/vennv/QueueError.php +++ /dev/null @@ -1,50 +0,0 @@ -errorMessage, - $this->errorCode - ); - } - - public function __toString() : string - { - return __CLASS__ . ": [$this->errorCode]: $this->errorMessage\n"; - } - -} \ No newline at end of file diff --git a/src/vennv/SamplePromise.php b/src/vennv/SamplePromise.php deleted file mode 100644 index 7f14155cd..000000000 --- a/src/vennv/SamplePromise.php +++ /dev/null @@ -1,48 +0,0 @@ -promise; - } - - public function getQueue() : ?Queue - { - return $this->queue; - } - -} \ No newline at end of file diff --git a/src/vennv/StatusQueue.php b/src/vennv/StatusQueue.php deleted file mode 100644 index 8ea68cbec..000000000 --- a/src/vennv/StatusQueue.php +++ /dev/null @@ -1,36 +0,0 @@ - $options - * @return Promise when Promise resolve InternetRequestResult and when Promise reject Error - * @throws Throwable - * @phpstan-param array{method?: string, headers?: array, timeout?: int, body?: array} $options - */ - public static function fetch(string $url, array $options = []) : Promise - { - return new Promise(function($resolve, $reject) use ($url, $options) - { - $method = $options["method"] ?? "GET"; - - /** @var array $headers */ - $headers = $options["headers"] ?? []; - - /** @var int $timeout */ - $timeout = $options["timeout"] ?? 10; - - /** @var array $body */ - $body = $options["body"] ?? []; - - if ($method === "GET") - { - $result = Internet::getURL($url, $timeout, $headers); - } - else - { - $result = Internet::postURL($url, $body, $timeout, $headers); - } - - if ($result === null) - { - $reject(Error::FAILED_IN_FETCHING_DATA); - } - else - { - $resolve($result); - } - }); - } - - /** - * @throws Throwable - */ - public static function fetchJg(string $url) : Promise - { - return new Promise(function($resolve, $reject) use ($url) - { - $ch = file_get_contents($url); - if ($ch === false) - { - $reject("Error in fetching data!"); - } - else - { - $resolve($ch); - } - }); - } - - /** - * @throws Throwable - */ - public static function endSingleJob() : void - { - parent::runSingleJob(); - } - - /** - * @throws Throwable - */ - public static function endMultiJobs() : void - { - parent::runMultiJobs(); - } - -} \ No newline at end of file diff --git a/src/vennv/Utils.php b/src/vennv/Utils.php deleted file mode 100644 index 0f6fb1268..000000000 --- a/src/vennv/Utils.php +++ /dev/null @@ -1,37 +0,0 @@ -