/** * Connects to the websocket * * @param boolean $keepAlive keep alive the connection (not supported yet) ? * @return $this */ public function initialize($keepAlive = false) { try { $this->engine->connect(); $this->isConnected = true; if (true === $keepAlive) { $this->engine->keepAlive(); } } catch (SocketException $e) { $this->logger->error('Could not connect to the server', ['exception' => $e]); throw $e; } return $this; }
/** * Import handler for the endpoint's underlying data * * \seekat\Call will call this when the request will have finished. * * @param Response $response * @return API self * @throws UnexpectedValueException * @throws RequestException * @throws \Exception */ function import(Response $response) : API { $this->__log->info(__FUNCTION__ . ": " . $response->getInfo(), ["url" => (string) $this->__url]); if ($response->getResponseCode() >= 400) { $e = new RequestException($response); $this->__log->critical(__FUNCTION__ . ": " . $e->getMessage(), ["url" => (string) $this->__url]); throw $e; } if (!($type = $response->getHeader("Content-Type", Header::class))) { $e = new RequestException($response); $this->__log->error(__FUNCTION__ . ": Empty Content-Type -> " . $e->getMessage(), ["url" => (string) $this->__url]); throw $e; } try { $this->__type = new ContentType($type); $this->__data = $this->__type->parseBody($response->getBody()); if ($link = $response->getHeader("Link", Header::class)) { $this->__links = new Links($link); } } catch (\Exception $e) { $this->__log->error(__FUNCTION__ . ": " . $e->getMessage(), ["url" => (string) $this->__url]); throw $e; } return $this; }
/** * Removes all payment rules from the db */ public function removePaymentRules($paymentId) { $sql = "DELETE FROM `s_core_rulesets` WHERE `paymentID` = ?"; try { Shopware()->Db()->query($sql, array($paymentId)); } catch (Exception $exception) { $this->log->error("There was an Error removing the payment rules: " . $exception->getMessage()); throw new Exception("There was an Error removing the payment rules: " . $exception->getMessage()); } }
public function run_command($args, $assoc_args = array()) { try { list($command, $final_args, $cmd_path) = $this->find_command_to_run($args); $name = implode(' ', $cmd_path); if (isset($this->extra_config[$name])) { $extra_args = $this->extra_config[$name]; } else { $extra_args = array(); } $command->invoke($final_args, $assoc_args, $extra_args); } catch (\Exception $e) { if (method_exists($e, 'getReplacements')) { $this->logger->error($e->getMessage(), $e->getReplacements()); } else { $this->logger->error($e->getMessage()); } exit(1); } }
/** * Get specific cell of the first row of the query result. * * @param DatabaseQueryResult $query The result of the run query * @param String $cell The title of the requested cell * * @return mixed $return FALSE on failure, mixed otherwise */ protected function result_cell($query, $cell) { if ($query->has_failed() === TRUE) { $context = ['query' => $query->query(), 'error' => $query->error_message()]; $this->logger->error('{query}; failed with error: {error}', $context); return FALSE; } if ($query->number_of_rows() == 0) { return ''; } else { return $query->result_cell($cell); } }
/** * Fetch and parse results as though they were a query string. * * @param String $url API URL * @param Array $params Array of parameters for the API request * @param String $method Request method to use, either 'get' or 'post' * * @return Array $result Array of return values */ protected function get_json_results($url, $params = [], $method = 'get') { if (strtolower($method) === 'get') { $response = $this->curl->get_request($url . '?' . http_build_query($params)); } else { $response = $this->curl->post_request($url, $params); } $result = json_decode($response->get_result(), TRUE); if ($response->http_code !== 200) { $error = $result['errors'][0]; $context = ['message' => $error['message'], 'code' => $error['code'], 'request' => $url]; $this->logger->error('Twitter API Request ({request}) failed, ({code}): {message}', $context); $result = ''; } unset($response); return $result; }
/** * 断片的なHTMLをbody要素の子として挿入し解析します。 * @param string $input * @return \DOMElement|null body要素。 */ protected function parse(string $input) { $prependedRandom = bin2hex(random_bytes(10)); $appendedRandom = bin2hex(random_bytes(10)); $html = "<!DOCTYPE html>\n <html>\n <head>\n <title>Filter</title>\n </head>\n <body><!--{$prependedRandom}-->{$input}<!--{$appendedRandom}--></body>\n </html>\n "; $html5 = new html5\HTML5(); $document = $html5->loadHTML($html); foreach ($html5->getErrors() as $error) { $this->logger->warning($error); } $body = $document->getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'body')->item(0); if ($body && $body->hasChildNodes() && $body->firstChild->nodeType === \XML_COMMENT_NODE && $body->firstChild->data === $prependedRandom && $body->lastChild->nodeType === \XML_COMMENT_NODE && $body->lastChild->data === $appendedRandom) { $body->removeChild($body->firstChild); $body->removeChild($body->lastChild); return $body; } $this->logger->error(_('HTMLの解析に失敗しました。')); }
/** * Fetch and parse results as though they were a query string. * * @param String $url API URL * @param Array $params Array of parameters for the API request * @param String $method Request method to use, either 'get' or 'post' * * @return Array $parts Array of return values */ protected function get_json_results($url, $params = [], $method = 'get') { $this->curl->set_option('CURLOPT_FAILONERROR', FALSE); if (strtolower($method) === 'get') { $response = $this->curl->get_request($url . '?' . http_build_query($params)); } else { $response = $this->curl->post_request($url, $params); } $result = json_decode($response->get_result(), TRUE); if ($response->http_code !== 200) { $error = $result['error']; $result = []; $context = ['message' => $error['message'], 'code' => $error['code'], 'type' => $error['type'], 'request' => $url]; $this->logger->error('Facebook API Request ({request}) failed, {type} ({code}): {message}', $context); } unset($response); return $result; }
/** * @return Memcached * @throws CacheException */ protected function getConnection() { if (null === $this->memcached) { $memcached = new Memcached($this->persistentId); $this->setMemcachedOptions($memcached); if (!count($this->servers)) { throw new CacheException("[{$this->persistentId}] At least one server must be specified."); } if (null === $this->persistentId) { if (!$memcached->addServers($this->servers)) { throw new CacheException("[{$this->persistentId}] Unable to add memcache servers [{$memcached->getResultCode()}:{$memcached->getResultMessage()}]."); } } else { $currentServers = $memcached->getServerList(); if (!count($currentServers)) { $attempts = 0; while (true) { $attempts++; if ($attempts > 3) { throw new CacheException("[{$this->persistentId}] Unable to add persistent memcache servers [{$memcached->getResultCode()}:{$memcached->getResultMessage()}]."); } if (!$memcached->addServers($this->servers)) { $this->Logger->error("[{$this->persistentId}] Unable to add persistent memcache servers [{$memcached->getResultCode()}:{$memcached->getResultMessage()}]."); $memcached->resetServerList(); $this->setMemcachedOptions($memcached); usleep(pow(2, $attempts) * 100000); } else { break; } } } else { $this->Logger->debug("[{$this->persistentId}] Servers already on persistent connection: " . print_r($currentServers, true)); } } $this->memcached = $memcached; } return $this->memcached; }
/** * Recursively removes a directory and its contents. * * @param String $dir_path The directory path to be removed * * @return Boolean TRUE when directory is removed and FALSE in a failure. */ public function rmdir($dir_path) { try { $directory = new RecursiveDirectoryIterator($dir_path, FilesystemIterator::SKIP_DOTS); $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::CHILD_FIRST); foreach ($iterator as $file) { if ($file->isFile()) { unlink($file->getPathname()); } else { rmdir($file->getPathname()); } } return rmdir($dir_path); } catch (UnexpectedValueException $unexpected) { $context = ['directory' => $dir_path, 'message' => $unexpected->getMessage()]; $this->logger->error("Couldn't recurse on directory '{directory}': {message}", $context); return FALSE; } catch (RuntimeException $runtime) { $context = ['message' => $runtime->getMessage()]; $this->logger->warning('{message}', $context); return FALSE; } }
/** * Delegate request to service. * * Makes sure that exceptions are catched. * * @return Page|Error */ protected function queryService($params) { $supportedTypes = $this->service->getSupportedTypes(); $supportedParameters = $this->service->getSupportedParameters(); $possibleParameters = array_merge($supportedParameters, \JSKOS\QueryModifiers); # filter out queries for unsupported types if (count($supportedTypes) and isset($params['type'])) { if (!in_array($params['type'], $supportedTypes)) { return new Page([]); } } # remove unknown parameters foreach (array_keys($params) as $name) { if (!in_array($name, $possibleParameters)) { $this->logger->notice('Unsupported query parameter {name}', ['name' => $name, 'value' => $params[$name]]); unset($params[$name]); } } # make sure all supported query parameters exist foreach ($supportedParameters as $name) { if (!isset($params[$name])) { $params[$name] = null; } } try { $response = $this->service->query($params); } catch (\Exception $e) { $this->logger->error('Service Exception', ['exception' => $e]); return new Error(500, '???', 'Internal server error'); } if (is_null($response)) { return new Page([]); } elseif ($response instanceof Item) { return new Page([$response]); } elseif ($response instanceof Page or $response instanceof Error) { return $response; } else { $this->logger->error('Service response has wrong type', ['response' => $response]); return new Error(500, '???', 'Internal server error'); } return $response; }
private function handleError() { $this->logger->error(var_export(func_get_args(), true)); }