protected function checkLock($idx) { $list = Json::decode(file_get_contents($this->lockfile)) ?: []; $s = static::LOCKEXPIRE; $expire = new DateTime("-{$s} seconds"); if (count($list)) { foreach ($list as $_idx => $time) { $time = new DateTime($time); if ($time < $expire) { unset($list[$_idx]); } } uasort($list, function ($a, $b) { return preg_replace('#[^\\d]#', '', $a) < preg_replace('#[^\\d]#', '', $b); }); $list = array_slice($list, 0, static::LOCKLIMIT); file_put_contents($this->lockfile, Json::encode($list)); } if (isset($list[$idx])) { $this->_throw("checkLock by key '{$idx}', go vim {$this->lockfile}", SmsApiException::LOCKERROR); } $list[$idx] = date('Y-m-d H:i:s'); file_put_contents($this->lockfile, Json::encode($list)); }
/** * @param null $method * @param string $path * @param array $data * @param array $headers * @return array|string * @throws Exception * http://httpd.pl/bundles/toolssitecommon/tools/transform.php */ public function api($method = null, $path = '', $data = array(), $headers = array()) { if (!$method) { $method = 'GET'; } $method = strtoupper($method); $ch = curl_init(); curl_setopt($ch, CURLOPT_ENCODING, ''); // curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->password); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_URL, $this->url . $path); if (!is_string($data) && $data) { $data = Json::encode($data); } if ($data) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } // $headers = array_merge($headers, array( // 'Content-Type: application/json', // )); if (count($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_VERBOSE, true); // dobre do debugowania curl_setopt($ch, CURLOPT_HEADER, 1); $response = null; $response = curl_exec($ch); // Then, after your curl_exec call: $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); $body = substr($response, $header_size); $data = array(); $data['body'] = Json::decode($body) ?: $body; $data['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($data['status'] === 0) { throw new Exception("Unable to connect to elasticsearch {$this->url}"); } curl_close($ch); $header = explode("\n", $header); $hlist = array(); foreach ($header as &$d) { $dd = explode(':', $d, 2); if (count($dd) === 2) { $hlist[$dd[0]] = trim($dd[1]); } } $data['header'] = $hlist; return $data; }
public function findOrThrowOneBy($conditions = []) { $row = $this->findOneBy($conditions); if ($row) { return $row; } $table = static::TABLE; throw new NotFoundHttpException("Nie odnaleziono encji '{$table}', kryteria: " . Json::encode($conditions)); }