$response->getReasonPhrase() ); $summary = ($bodySummarizer ?? new BodySummarizer())->summarize($response); if ($summary !== null) { $message .= ":\n{$summary}\n"; } return new $className($message, $request, $response, $previous, $handlerContext); } /** * Obfuscates URI if there is a username and a password present */ private static function obfuscateUri(UriInterface $uri): UriInterface { $userInfo = $uri->getUserInfo();
return $handler($request, $options)->then( static function (ResponseInterface $response) use ($request, $bodySummarizer) { $code = $response->getStatusCode(); if ($code < 400) { return $response; } throw RequestException::create($request, $response, null, [], $bodySummarizer); } ); }; }; }
* stack trace. Since $handler contains a reference to the callable * itself we get a circular reference. We clear the $handler * here to avoid that memory leak. */ $f = $handler[$index]; unset($handler); $promise->resolve($f($value)); } elseif ($index === 1) { // Forward resolution values as-is. $promise->resolve($value); } else { // Forward rejections down the chain. $promise->reject($value);
// it in the task queue using the correct ID. if (!is_object($value) || !method_exists($value, 'then')) { $id = $state === self::FULFILLED ? 1 : 2; // It's a success, so resolve the handlers in the queue. Utils::queue()->add(static function () use ($id, $value, $handlers) { foreach ($handlers as $handler) { self::callHandler($id, $value, $handler); } }); } elseif ($value instanceof Promise && Is::pending($value)) { // We can just merge our handlers onto the next promise. $value->handlers = array_merge($value->handlers, $handlers); } else {
} public function run() { while ($task = array_shift($this->queue)) { /** @var callable $task */ $task(); } } /** * The task queue will be run and exhausted by default when the process * exits IFF the exit is not the result of a PHP E_ERROR error.
private function invokeWaitFn() { try { $wfn = $this->waitFn; $this->waitFn = null; $wfn(true); } catch (\Exception $reason) { if ($this->state === self::PENDING) { // The promise has not been resolved yet, so reject the promise // with the exception. $this->reject($reason); } else {
private function waitIfPending() { if ($this->state !== self::PENDING) { return; } elseif ($this->waitFn) { $this->invokeWaitFn(); } elseif ($this->waitList) { $this->invokeWaitList(); } else { // If there's no wait function, then reject the promise. $this->reject('Cannot wait on a promise that has ' . 'no internal wait function. You must provide a wait '
{ $waitList = $this->waitList; $this->waitList = null; foreach ($waitList as $result) { do { $result->waitIfPending(); $result = $result->result; } while ($result instanceof Promise); if ($result instanceof PromiseInterface) { $result->wait(false); }
{ if ($this->state !== self::PENDING) { return; } elseif ($this->waitFn) { $this->invokeWaitFn(); } elseif ($this->waitList) { $this->invokeWaitList(); } else { // If there's no wait function, then reject the promise. $this->reject('Cannot wait on a promise that has ' . 'no internal wait function. You must provide a wait ' . 'function when constructing the promise to be able to ' . 'wait on a promise.');
{ return $this->then(null, $onRejected); } public function wait($unwrap = true) { $this->waitIfPending(); if ($this->result instanceof PromiseInterface) { return $this->result->wait($unwrap); } if ($unwrap) { if ($this->state === self::FULFILLED) {
* * @throws GuzzleException */ public function request(string $method, $uri = '', array $options = []): ResponseInterface { $options[RequestOptions::SYNCHRONOUS] = true; return $this->requestAsync($method, $uri, $options)->wait(); } /** * Get a client configuration option. * * These options include default request options of the client, a "handler"
return $this->data($url); } private function data($url) { $res = $this->_client->request('GET', $url); $statusCode = $res->getStatusCode(); $body = $res->getBody(); if ($statusCode !== 200) { $strBody = json_decode((string)$body, true);
'origin' => $departureIata, 'destination' => $arrivalIata, 'currency' => Yii::$app->params['currency'], 'token' => Yii::$app->params['travelpayouts']['token'], ]); return $this->data($url); } /* * Цены на авиабилеты за 48 часов */ public function pricesFoundUsers($departureIata = '', $arrivalIata = '', $oneWay = false)
return $data; }, 60*60*8); } public function setCheapest($departureIata, $arrivalIata) { $this->_cheapest = Yii::$app->cache->getOrSet('cheapest-airtickets-'.$departureIata.'-'.$arrivalIata, function () use ($departureIata, $arrivalIata) { $cheapest = Yii::$app->apiTravelpayouts->cheapest($departureIata, $arrivalIata); if (count($cheapest['data']) == 0) { return false; } $data = $cheapest['data']; array_multisort(array_column($data, 'price'), SORT_ASC, $data);
public function getOrSet($key, $callable, $duration = null, $dependency = null) { if (($value = $this->get($key)) !== false) { return $value; } $value = call_user_func($callable, $this); if (!$this->set($key, $value, $duration, $dependency)) { Yii::warning('Failed to set cache value for key ' . json_encode($key), __METHOD__); } return $value; }
return false; } $data = $cheapest['data']; array_multisort(array_column($data, 'price'), SORT_ASC, $data); return $data; }, 60*60*8); } public function getCheapest() { return $this->_cheapest; }
Yii::$app->routeManager->setAirTravelTime(Yii::$app->routeManager->distance); Yii::$app->routeManager->setDeparture($departureCity); Yii::$app->routeManager->setArrival($arrivalCity); // Установка самых дешевых авиабилетов (значение будет доступно здесь: Yii::$app->routeManager->cheapest) Yii::$app->routeManager->setCheapest(Yii::$app->routeManager->departure->iata, Yii::$app->routeManager->arrival->iata); $seo = SeoPageCityToCity::find()->where(['from' => $departureCityIata, 'to' => $arrivalCityIata])->active()->one(); Yii::$app->customManager->setData($seo); Yii::$app->routeManager->setAllRoutes($model);
$args = $this->controller->bindActionParams($this, $params); Yii::debug('Running action: ' . get_class($this->controller) . '::' . $this->actionMethod . '()', __METHOD__); if (Yii::$app->requestedParams === null) { Yii::$app->requestedParams = $args; } return call_user_func_array([$this->controller, $this->actionMethod], $args); } }
} $result = null; if ($runAction && $this->beforeAction($action)) { // run the action $result = $action->runWithParams($params); $result = $this->afterAction($action, $result); // call afterAction on modules foreach ($modules as $module) { /* @var $module Module */
$parts = $this->createController($route); if (is_array($parts)) { /* @var $controller Controller */ list($controller, $actionID) = $parts; $oldController = Yii::$app->controller; Yii::$app->controller = $controller; $result = $controller->runAction($actionID, $params); if ($oldController !== null) { Yii::$app->controller = $oldController; } return $result; }
$params = $this->catchAll; unset($params[0]); } try { Yii::debug("Route requested: '$route'", __METHOD__); $this->requestedRoute = $route; $result = $this->runAction($route, $params); if ($result instanceof Response) { return $result; } $response = $this->getResponse(); if ($result !== null) {
{ try { $this->state = self::STATE_BEFORE_REQUEST; $this->trigger(self::EVENT_BEFORE_REQUEST); $this->state = self::STATE_HANDLING_REQUEST; $response = $this->handleRequest($this->getRequest()); $this->state = self::STATE_AFTER_REQUEST; $this->trigger(self::EVENT_AFTER_REQUEST); $this->state = self::STATE_SENDING_RESPONSE; $response->send();
require(__DIR__ . '/../../common/config/main-local.php'), require(__DIR__ . '/../config/main.php'), require(__DIR__ . '/../config/main-local.php') ); $application = new yii\web\Application($config); $application->run();
$_GET = [ 'departureCityIata' => 'gzt', 'arrivalCityIata' => 'cas', ];