/** * @inheritdoc */ public function validate($input = null) { /* @var $targetClass ActiveRecordInterface */ $targetClass = $this->targetClass === null ? get_class($this->model) : $this->targetClass; $targetAttribute = $this->targetAttribute === null ? $this->attribute : $this->targetAttribute; if (is_array($targetAttribute)) { $params = []; foreach ($targetAttribute as $k => $v) { $params[$v] = is_integer($k) ? $this->model->{$v} : $this->model->{$k}; } $this->params['value'] = $params; } else { $params = [$targetAttribute => $this->model[$this->attribute]]; $this->params['value'] = $this->model[$this->attribute]; } foreach ($params as $value) { if (is_array($value)) { if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new ValidateException("{$targetClass}::{$targetAttribute} is invalid: type array()")); Log::err($message); } return false; } } $query = $targetClass::find(); $query->andWhere($params); if ($this->filter instanceof \Closure) { call_user_func($this->filter, $query); } elseif ($this->filter !== null) { $query->andWhere($this->filter); } if (!$this->model instanceof ActiveRecordInterface || $this->model->getIsNewRecord()) { // if current $model isn't in the database yet then it's OK just to call exists() $exists = $query->exists(); } else { // if current $model is in the database already we can't use exists() /* @var $models ActiveRecordInterface[] */ $models = $query->limit(2)->all(); $n = count($models); if ($n === 1) { $keys = array_keys($params); $pks = $targetClass::primaryKey(); sort($keys); sort($pks); if ($keys === $pks) { // primary key is modified and not unique $exists = $this->model->getOldPrimaryKey() != $this->model->getPrimaryKey(); } else { // non-primary key, need to exclude the current record based on PK $exists = $models[0]->getPrimaryKey() != $this->model->getOldPrimaryKey(); } } else { $exists = $n > 1; } } return !$exists; }
/** * {@inheritdoc} */ public function getAttributes($code = null) { if (!isset($code)) { $code = Request::get('code'); } if (empty($code)) { return []; } // This was a callback request from google, get the token $this->service->requestAccessToken($code); // Send a request with it try { return Json::decode($this->service->request($this->apiUrl)); } catch (JsonException $e) { if (class_exists('\\rock\\log\\Log')) { Log::err(BaseException::convertExceptionToString($e)); } } return []; }
/** * @inheritdoc */ public function lock($key, $iteration = 15) { $i = 0; while (!apc_add($this->prepareKey($key, self::LOCK_PREFIX), 1, $this->lockExpire)) { $i++; if ($i > $iteration) { if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new CacheException(CacheException::INVALID_SAVE, ['key' => $key])); Log::err($message); } return false; } usleep(rand(10, 1000)); } return true; }
/** * @inheritdoc */ public function subscribe($topic = '', $limit = 1, $message = '') { if (!$this->beforeSubscription($topic, $message)) { return null; } $connection = new AMQPConnection($this->host, $this->port, $this->user, $this->password); $channel = $connection->channel(); $channel->exchange_declare($this->exchange, $this->type, false, false, false); list($queueName) = $channel->queue_declare("", false, false, true, false); $correlationId = uniqid(); $result = null; $channel->basic_consume($queueName, '', false, false, false, false, function (AMQPMessage $msg) use(&$result, $correlationId) { if ($msg->get('correlation_id') == $correlationId) { $result = $msg->body; } }); register_shutdown_function(function (AMQPChannel $channel, AMQPConnection $connection) { $channel->close(); $connection->close(); }, $channel, $connection); $msg = new AMQPMessage($message, ['correlation_id' => $correlationId, 'reply_to' => $queueName]); $channel->basic_publish($msg, $this->exchange, $topic); if ($this->blocking) { if (count($channel->callbacks)) { $channel->wait(); } $this->afterSubscription($topic, $result); return $result; } // non-blocking if (count($channel->callbacks)) { $count = 0; while ($limit == -1 || $count < $limit) { // add here other sockets that you need to attend $read = []; if (is_resource($connection->getSocket())) { $read = [$connection->getSocket()]; } $write = null; $except = null; if (false === ($num_changed_streams = stream_select($read, $write, $except, $this->timeout))) { break; } elseif ($num_changed_streams > 0) { $channel->wait(); $this->afterSubscription($topic, $result); return $result; } ++$count; } if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new MQException('The receive timed out.')); Log::err($message); } } return $result; }
/** * PHP magic method that returns the string representation of this object. * * @return string the string representation of this object. */ public function __toString() { // __toString cannot throw exception // use trigger_error to bypass this limitation try { return $this->render(); } catch (\Exception $e) { if (class_exists('\\rock\\log\\Log')) { Log::err(BaseException::convertExceptionToString($e)); } return ''; } }
/** * @inheritdoc * @throws MQException */ public function subscribe($topic = '', $limit = -1, $message = '') { if (!$this->beforeSubscription($topic, $message)) { return null; } $subscriber = $this->connection(\ZMQ::SOCKET_SUB); $subscriber->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, $topic); if ($this->blocking) { $result = $subscriber->recvMulti(); $result = isset($result[1]) ? $result[1] : null; $this->afterSubscription($topic, $result); return $result; } $count = 0; while ($limit == -1 || $count < $limit) { try { $result = $subscriber->recvMulti(\ZMQ::MODE_DONTWAIT); if (!empty($result)) { $result = isset($result[1]) ? $result[1] : null; $this->afterSubscription($topic, $result); return $result; } } catch (\ZMQSocketException $e) { throw new MQException($e->getMessage(), [], $e); } ++$count; sleep($this->timeout); } if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new MQException('The receive timed out.')); Log::err($message); } return null; }
/** * Is self domain. * * @param bool $throw throw an exception (default: false) * @throws RequestException * @return bool */ public function isSelfDomain($throw = false) { if (!($domains = $this->allowDomains)) { return true; } if (!in_array(strtolower($_SERVER['SERVER_NAME']), $domains, true) || !in_array(strtolower($_SERVER['HTTP_HOST']), $domains, true)) { if ($throw === true) { throw new RequestException("Invalid domain: {$_SERVER['HTTP_HOST']}"); } else { if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new RequestException("Invalid domain: {$_SERVER['HTTP_HOST']}")); Log::err($message); } } return false; } return true; }
/** * @inheritdoc */ public function send($message = '', $limit = -1) { if (!$this->beforeSend()) { return null; } $client = $this->client(); if ($this->blocking) { $result = null; switch ($this->priority) { case self::PRIORITY_NORMAL: $result = $client->doNormal($this->id, $message); break; case self::PRIORITY_LOW: $result = $client->doLow($this->id, $message); break; default: $result = $client->doHigh($this->id, $message); break; } $this->afterSend($result); return $result; } else { $count = 0; $client->setTimeout($this->timeout * 1000); while ($limit == -1 || $count < $limit) { switch ($this->priority) { case self::PRIORITY_NORMAL: $result = @$client->doNormal($this->id, $message); break; case self::PRIORITY_LOW: $result = @$client->doLow($this->id, $message); break; default: $result = @$client->doHigh($this->id, $message); break; } if ($result) { $this->afterSend($result); return $result; } ++$count; } } if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new MQException('The receive timed out.')); Log::err($message); } return null; }
/** * Rename file. * * @param string $oldPath old path. * @param string $newPath new path. * @return bool */ public static function rename($oldPath, $newPath) { if (!@rename($oldPath, $newPath)) { if (class_exists('\\rock\\log\\Log')) { $message = FileHelperException::convertExceptionToString(new FileHelperException("Error when renaming file: {$oldPath}")); Log::err($message); } return false; } return true; }
/** * @inheritdoc */ public function lock($key, $iteration = 15) { $i = 0; while (!$this->lockInternal($key)) { $i++; if ($i > $iteration) { if (class_exists('\\rock\\log\\Log')) { $message = BaseException::convertExceptionToString(new CacheException(CacheException::INVALID_SAVE, ['key' => $key])); Log::err($message); } return false; } usleep(rand(10, 1000)); } return true; }