Commit e7295ad5 by Qiang Xue

Use __METHOD__ as log category.

parent b11925d1
...@@ -541,7 +541,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -541,7 +541,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function onUnsafeAttribute($name, $value) public function onUnsafeAttribute($name, $value)
{ {
if (YII_DEBUG) { if (YII_DEBUG) {
\Yii::info("Failed to set unsafe attribute '$name' in '" . get_class($this) . "'.", __CLASS__); \Yii::info("Failed to set unsafe attribute '$name' in '" . get_class($this) . "'.", __METHOD__);
} }
} }
......
...@@ -346,7 +346,7 @@ abstract class Module extends Component ...@@ -346,7 +346,7 @@ abstract class Module extends Component
if ($this->_modules[$id] instanceof Module) { if ($this->_modules[$id] instanceof Module) {
return $this->_modules[$id]; return $this->_modules[$id];
} elseif ($load) { } elseif ($load) {
Yii::trace("Loading module: $id", __CLASS__); Yii::trace("Loading module: $id", __METHOD__);
return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this); return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this);
} }
} }
...@@ -452,7 +452,7 @@ abstract class Module extends Component ...@@ -452,7 +452,7 @@ abstract class Module extends Component
if ($this->_components[$id] instanceof Component) { if ($this->_components[$id] instanceof Component) {
return $this->_components[$id]; return $this->_components[$id];
} elseif ($load) { } elseif ($load) {
Yii::trace("Loading component: $id", __CLASS__); Yii::trace("Loading component: $id", __METHOD__);
return $this->_components[$id] = Yii::createObject($this->_components[$id]); return $this->_components[$id] = Yii::createObject($this->_components[$id]);
} }
} }
......
...@@ -134,7 +134,7 @@ class Command extends \yii\base\Component ...@@ -134,7 +134,7 @@ class Command extends \yii\base\Component
try { try {
$this->pdoStatement = $this->db->pdo->prepare($sql); $this->pdoStatement = $this->db->pdo->prepare($sql);
} catch (\Exception $e) { } catch (\Exception $e) {
Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __CLASS__); Yii::error($e->getMessage() . "\nFailed to prepare SQL: $sql", __METHOD__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($e->getMessage(), $errorInfo, (int)$e->getCode()); throw new Exception($e->getMessage(), $errorInfo, (int)$e->getCode());
} }
...@@ -266,15 +266,16 @@ class Command extends \yii\base\Component ...@@ -266,15 +266,16 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true); $paramLog = "\nParameters: " . var_export($this->_params, true);
} }
Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__); Yii::trace("Executing SQL: {$sql}{$paramLog}", __METHOD__);
if ($sql == '') { if ($sql == '') {
return 0; return 0;
} }
try { try {
$token = "SQL: $sql";
if ($this->db->enableProfiling) { if ($this->db->enableProfiling) {
Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__); Yii::beginProfile($token, __METHOD__);
} }
$this->prepare(); $this->prepare();
...@@ -282,16 +283,16 @@ class Command extends \yii\base\Component ...@@ -282,16 +283,16 @@ class Command extends \yii\base\Component
$n = $this->pdoStatement->rowCount(); $n = $this->pdoStatement->rowCount();
if ($this->db->enableProfiling) { if ($this->db->enableProfiling) {
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile($token, __METHOD__);
} }
return $n; return $n;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->db->enableProfiling) { if ($this->db->enableProfiling) {
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile($token, __METHOD__);
} }
$message = $e->getMessage(); $message = $e->getMessage();
Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __CLASS__); Yii::error("$message\nFailed to execute SQL: {$sql}{$paramLog}", __METHOD__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode()); throw new Exception($message, $errorInfo, (int)$e->getCode());
...@@ -383,7 +384,7 @@ class Command extends \yii\base\Component ...@@ -383,7 +384,7 @@ class Command extends \yii\base\Component
$paramLog = "\nParameters: " . var_export($this->_params, true); $paramLog = "\nParameters: " . var_export($this->_params, true);
} }
Yii::trace("Querying SQL: {$sql}{$paramLog}", __CLASS__); Yii::trace("Querying SQL: {$sql}{$paramLog}", __METHOD__);
/** @var $cache \yii\caching\Cache */ /** @var $cache \yii\caching\Cache */
if ($db->enableQueryCache && $method !== '') { if ($db->enableQueryCache && $method !== '') {
...@@ -399,14 +400,15 @@ class Command extends \yii\base\Component ...@@ -399,14 +400,15 @@ class Command extends \yii\base\Component
$paramLog, $paramLog,
)); ));
if (($result = $cache->get($cacheKey)) !== false) { if (($result = $cache->get($cacheKey)) !== false) {
Yii::trace('Query result served from cache', __CLASS__); Yii::trace('Query result served from cache', __METHOD__);
return $result; return $result;
} }
} }
try { try {
$token = "SQL: $sql";
if ($db->enableProfiling) { if ($db->enableProfiling) {
Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__); Yii::beginProfile($token, __METHOD__);
} }
$this->prepare(); $this->prepare();
...@@ -423,21 +425,21 @@ class Command extends \yii\base\Component ...@@ -423,21 +425,21 @@ class Command extends \yii\base\Component
} }
if ($db->enableProfiling) { if ($db->enableProfiling) {
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile($token, __METHOD__);
} }
if (isset($cache, $cacheKey) && $cache instanceof Cache) { if (isset($cache, $cacheKey) && $cache instanceof Cache) {
$cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency); $cache->set($cacheKey, $result, $db->queryCacheDuration, $db->queryCacheDependency);
Yii::trace('Saved query result in cache', __CLASS__); Yii::trace('Saved query result in cache', __METHOD__);
} }
return $result; return $result;
} catch (\Exception $e) { } catch (\Exception $e) {
if ($db->enableProfiling) { if ($db->enableProfiling) {
Yii::endProfile(__METHOD__ . "($sql)", __CLASS__); Yii::endProfile($token, __METHOD__);
} }
$message = $e->getMessage(); $message = $e->getMessage();
Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __CLASS__); Yii::error("$message\nCommand::$method() failed: {$sql}{$paramLog}", __METHOD__);
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; $errorInfo = $e instanceof \PDOException ? $e->errorInfo : null;
throw new Exception($message, $errorInfo, (int)$e->getCode()); throw new Exception($message, $errorInfo, (int)$e->getCode());
} }
......
...@@ -324,12 +324,12 @@ class Connection extends Component ...@@ -324,12 +324,12 @@ class Connection extends Component
throw new InvalidConfigException('Connection::dsn cannot be empty.'); throw new InvalidConfigException('Connection::dsn cannot be empty.');
} }
try { try {
\Yii::trace('Opening DB connection: ' . $this->dsn, __CLASS__); \Yii::trace('Opening DB connection: ' . $this->dsn, __METHOD__);
$this->pdo = $this->createPdoInstance(); $this->pdo = $this->createPdoInstance();
$this->initConnection(); $this->initConnection();
} }
catch (\PDOException $e) { catch (\PDOException $e) {
\Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __CLASS__); \Yii::error("Failed to open DB connection ({$this->dsn}): " . $e->getMessage(), __METHOD__);
$message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.'; $message = YII_DEBUG ? 'Failed to open DB connection: ' . $e->getMessage() : 'Failed to open DB connection.';
throw new Exception($message, $e->errorInfo, (int)$e->getCode()); throw new Exception($message, $e->errorInfo, (int)$e->getCode());
} }
...@@ -343,7 +343,7 @@ class Connection extends Component ...@@ -343,7 +343,7 @@ class Connection extends Component
public function close() public function close()
{ {
if ($this->pdo !== null) { if ($this->pdo !== null) {
\Yii::trace('Closing DB connection: ' . $this->dsn, __CLASS__); \Yii::trace('Closing DB connection: ' . $this->dsn, __METHOD__);
$this->pdo = null; $this->pdo = null;
$this->_schema = null; $this->_schema = null;
$this->_transaction = null; $this->_transaction = null;
......
...@@ -66,7 +66,7 @@ class Transaction extends \yii\base\Object ...@@ -66,7 +66,7 @@ class Transaction extends \yii\base\Object
if ($this->db === null) { if ($this->db === null) {
throw new InvalidConfigException('Transaction::db must be set.'); throw new InvalidConfigException('Transaction::db must be set.');
} }
\Yii::trace('Starting transaction', __CLASS__); \Yii::trace('Starting transaction', __METHOD__);
$this->db->open(); $this->db->open();
$this->db->pdo->beginTransaction(); $this->db->pdo->beginTransaction();
$this->_active = true; $this->_active = true;
...@@ -80,7 +80,7 @@ class Transaction extends \yii\base\Object ...@@ -80,7 +80,7 @@ class Transaction extends \yii\base\Object
public function commit() public function commit()
{ {
if ($this->_active && $this->db && $this->db->isActive) { if ($this->_active && $this->db && $this->db->isActive) {
\Yii::trace('Committing transaction', __CLASS__); \Yii::trace('Committing transaction', __METHOD__);
$this->db->pdo->commit(); $this->db->pdo->commit();
$this->_active = false; $this->_active = false;
} else { } else {
...@@ -95,7 +95,7 @@ class Transaction extends \yii\base\Object ...@@ -95,7 +95,7 @@ class Transaction extends \yii\base\Object
public function rollback() public function rollback()
{ {
if ($this->_active && $this->db && $this->db->isActive) { if ($this->_active && $this->db && $this->db->isActive) {
\Yii::trace('Rolling back transaction', __CLASS__); \Yii::trace('Rolling back transaction', __METHOD__);
$this->db->pdo->rollBack(); $this->db->pdo->rollBack();
$this->_active = false; $this->_active = false;
} else { } else {
......
...@@ -72,7 +72,7 @@ class PhpMessageSource extends MessageSource ...@@ -72,7 +72,7 @@ class PhpMessageSource extends MessageSource
} }
return $messages; return $messages;
} else { } else {
Yii::error("The message file for category '$category' does not exist: $messageFile", __CLASS__); Yii::error("The message file for category '$category' does not exist: $messageFile", __METHOD__);
return array(); return array();
} }
} }
......
...@@ -16,15 +16,14 @@ interface Identity ...@@ -16,15 +16,14 @@ interface Identity
{ {
/** /**
* Returns an ID that can uniquely identify a user identity. * Returns an ID that can uniquely identify a user identity.
* The returned ID can be a string, an integer, or any serializable data. * @return string|integer an ID that uniquely identifies a user identity.
* @return mixed an ID that uniquely identifies a user identity.
*/ */
public function getId(); public function getId();
/** /**
* Returns a key that can be used to check the validity of a given identity ID. * Returns a key that can be used to check the validity of a given identity ID.
* The space of such keys should be big and random enough to defeat potential identity attacks. * The space of such keys should be big and random enough to defeat potential identity attacks.
* The returned key can be a string, an integer, or any serializable data. * The returned key can be a string, an integer, or any serializable data.
* @return mixed a key that is used to check the validity of a given identity ID. * @return string a key that is used to check the validity of a given identity ID.
* @see validateAuthKey() * @see validateAuthKey()
*/ */
public function getAuthKey(); public function getAuthKey();
...@@ -37,7 +36,7 @@ interface Identity ...@@ -37,7 +36,7 @@ interface Identity
public function validateAuthKey($authKey); public function validateAuthKey($authKey);
/** /**
* Finds an identity by the given ID. * Finds an identity by the given ID.
* @param mixed $id the ID to be looked for * @param string|integer $id the ID to be looked for
* @return Identity the identity object that matches the given ID. * @return Identity the identity object that matches the given ID.
* Null should be returned if such an identity cannot be found. * Null should be returned if such an identity cannot be found.
*/ */
......
...@@ -117,7 +117,7 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co ...@@ -117,7 +117,7 @@ class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Co
$this->_opened = false; $this->_opened = false;
$error = error_get_last(); $error = error_get_last();
$message = isset($error['message']) ? $error['message'] : 'Failed to start session.'; $message = isset($error['message']) ? $error['message'] : 'Failed to start session.';
Yii::error($message, __CLASS__); Yii::error($message, __METHOD__);
} else { } else {
$this->_opened = true; $this->_opened = true;
$this->updateFlashCounters(); $this->updateFlashCounters();
......
...@@ -90,6 +90,9 @@ class User extends Component ...@@ -90,6 +90,9 @@ class User extends Component
{ {
parent::init(); parent::init();
if ($this->identityClass === null) {
throw new InvalidConfigException('User::identityClass must be set.');
}
if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) { if ($this->enableAutoLogin && !isset($this->identityCookie['name'])) {
throw new InvalidConfigException('User::identityCookie must contain the "name" element.'); throw new InvalidConfigException('User::identityCookie must contain the "name" element.');
} }
...@@ -179,7 +182,13 @@ class User extends Component ...@@ -179,7 +182,13 @@ class User extends Component
/** @var $class Identity */ /** @var $class Identity */
$class = $this->identityClass; $class = $this->identityClass;
$identity = $class::findIdentity($id); $identity = $class::findIdentity($id);
if ($identity !== null && $identity->validateAuthKey($authKey) && $this->beforeLogin($identity, true)) { if ($identity === null || !$identity->validateAuthKey($authKey)) {
if ($identity !== null) {
Yii::warning("Invalid auth key attempted for user '$id': $authKey", __METHOD__);
}
return;
}
if ($this->beforeLogin($identity, true)) {
$this->switchIdentity($identity); $this->switchIdentity($identity);
if ($this->autoRenewCookie) { if ($this->autoRenewCookie) {
$this->saveIdentityCookie($identity, $duration); $this->saveIdentityCookie($identity, $duration);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment