Browse Source

refactored event declaration convention.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
373e6a705e
  1. 18
      framework/base/Model.php
  2. 61
      framework/db/ActiveRecord.php
  3. 9
      framework/db/Connection.php
  4. 23
      framework/logging/Logger.php

18
framework/base/Model.php

@ -38,15 +38,21 @@ use yii\validators\RequiredValidator;
* @property array $attributes Attribute values (name=>value).
* @property string $scenario The scenario that this model is in.
*
* @event ModelEvent beforeValidate an event raised at the beginning of [[validate()]]. You may set
* [[ModelEvent::isValid]] to be false to stop the validation.
* @event Event afterValidate an event raised at the end of [[validate()]]
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Model extends Component implements \IteratorAggregate, \ArrayAccess
{
/**
* @event ModelEvent an event raised at the beginning of [[validate()]]. You may set
* [[ModelEvent::isValid]] to be false to stop the validation.
*/
const EVENT_BEFORE_VALIDATE = 'beforeValidate';
/**
* @event Event an event raised at the end of [[validate()]]
*/
const EVENT_AFTER_VALIDATE = 'afterValidate';
private static $_attributes = array(); // class name => array of attribute names
private $_errors; // attribute name => array of errors
private $_validators; // Vector of validators
@ -250,7 +256,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function beforeValidate()
{
$event = new ModelEvent($this);
$this->trigger('beforeValidate', $event);
$this->trigger(self::EVENT_BEFORE_VALIDATE, $event);
return $event->isValid;
}
@ -262,7 +268,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/
public function afterValidate()
{
$this->trigger('afterValidate');
$this->trigger(self::EVENT_AFTER_VALIDATE);
}
/**

61
framework/db/ActiveRecord.php

@ -11,6 +11,7 @@
namespace yii\db;
use yii\base\Model;
use yii\base\Event;
use yii\base\ModelEvent;
use yii\base\UnknownMethodException;
use yii\base\InvalidCallException;
@ -32,24 +33,48 @@ use yii\util\StringHelper;
* @property mixed $primaryKey the primary key value.
* @property mixed $oldPrimaryKey the old primary key value.
*
* @event ModelEvent init an event that is triggered when the record is initialized (in [[init()]]).
* @event ModelEvent afterFind an event that is triggered after the record is created and populated with query result.
* @event ModelEvent beforeInsert an event that is triggered before inserting a record.
* You may set [[ModelEvent::isValid]] to be false to stop the insertion.
* @event Event afterInsert an event that is triggered before inserting a record.
* @event ModelEvent beforeUpdate an event that is triggered before updating a record.
* You may set [[ModelEvent::isValid]] to be false to stop the update.
* @event Event afterUpdate an event that is triggered before updating a record.
* @event ModelEvent beforeDelete an event that is triggered before deleting a record.
* You may set [[ModelEvent::isValid]] to be false to stop the deletion.
* @event Event afterDelete an event that is triggered after deleting a record.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveRecord extends Model
{
/**
* @event Event an event that is triggered when the record is initialized via [[init()]].
*/
const EVENT_INIT = 'init';
/**
* @event Event an event that is triggered after the record is created and populated with query result.
*/
const EVENT_AFTER_FIND = 'afterFind';
/**
* @event ModelEvent an event that is triggered before inserting a record.
* You may set [[ModelEvent::isValid]] to be false to stop the insertion.
*/
const EVENT_BEFORE_INSERT = 'beforeInsert';
/**
* @event Event an event that is triggered after a record is inserted.
*/
const EVENT_AFTER_INSERT = 'afterInsert';
/**
* @event ModelEvent an event that is triggered before updating a record.
* You may set [[ModelEvent::isValid]] to be false to stop the update.
*/
const EVENT_BEFORE_UPDATE = 'beforeUpdate';
/**
* @event Event an event that is triggered after a record is updated.
*/
const EVENT_AFTER_UPDATE = 'afterUpdate';
/**
* @event ModelEvent an event that is triggered before deleting a record.
* You may set [[ModelEvent::isValid]] to be false to stop the deletion.
*/
const EVENT_BEFORE_DELETE = 'beforeDelete';
/**
* @event Event an event that is triggered after a record is deleted.
*/
const EVENT_AFTER_DELETE = 'afterDelete';
/**
* @var array attribute values indexed by attribute names
*/
private $_attributes = array();
@ -773,7 +798,7 @@ class ActiveRecord extends Model
public function init()
{
parent::init();
$this->trigger('init');
$this->trigger(self::EVENT_INIT);
}
/**
@ -784,7 +809,7 @@ class ActiveRecord extends Model
*/
public function afterFind()
{
$this->trigger('afterFind');
$this->trigger(self::EVENT_AFTER_FIND);
}
/**
@ -823,7 +848,7 @@ class ActiveRecord extends Model
public function beforeSave($insert)
{
$event = new ModelEvent($this);
$this->trigger($insert ? 'beforeInsert' : 'beforeUpdate', $event);
$this->trigger($insert ? self::EVENT_BEFORE_INSERT : self::EVENT_BEFORE_UPDATE, $event);
return $event->isValid;
}
@ -838,7 +863,7 @@ class ActiveRecord extends Model
*/
public function afterSave($insert)
{
$this->trigger($insert ? 'afterInsert' : 'afterUpdate');
$this->trigger($insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE);
}
/**
@ -863,7 +888,7 @@ class ActiveRecord extends Model
public function beforeDelete()
{
$event = new ModelEvent($this);
$this->trigger('beforeDelete', $event);
$this->trigger(self::EVENT_BEFORE_DELETE, $event);
return $event->isValid;
}
@ -875,7 +900,7 @@ class ActiveRecord extends Model
*/
public function afterDelete()
{
$this->trigger('afterDelete');
$this->trigger(self::EVENT_AFTER_DELETE);
}
/**

9
framework/db/Connection.php

@ -95,14 +95,17 @@ use yii\base\NotSupportedException;
* @property string $driverName Name of the DB driver currently being used.
* @property array $querySummary The statistical results of SQL queries.
*
* @event Event afterOpen this event is triggered after a DB connection is established
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Connection extends \yii\base\ApplicationComponent
{
/**
* @event Event an event that is triggered after a DB connection is established
*/
const EVENT_AFTER_OPEN = 'afterOpen';
/**
* @var string the Data Source Name, or DSN, contains the information required to connect to the database.
* Please refer to the [PHP manual](http://www.php.net/manual/en/function.PDO-construct.php) on
* the format of the DSN string.
@ -382,7 +385,7 @@ class Connection extends \yii\base\ApplicationComponent
if ($this->charset !== null && in_array($this->getDriverName(), array('pgsql', 'mysql', 'mysqli'))) {
$this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));
}
$this->trigger('afterOpen');
$this->trigger(self::EVENT_AFTER_OPEN);
}
/**

23
framework/logging/Logger.php

@ -9,6 +9,8 @@
namespace yii\logging;
use yii\base\Event;
/**
* Logger records logged messages in memory.
*
@ -18,13 +20,16 @@ namespace yii\logging;
*
* Logger provides a set of events for further customization:
*
* - `flush`. Raised on logs flush.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Logger extends \yii\base\Component
{
/**
* @event Event an event that is triggered when messages are being flushed from memory to targets
*/
const EVENT_FLUSH = 'flush';
const LEVEL_ERROR = 'error';
const LEVEL_WARNING = 'warning';
const LEVEL_INFO = 'info';
@ -105,8 +110,8 @@ class Logger extends \yii\base\Component
/**
* Marks the beginning of a code block for profiling.
* This has to be matched with a call to [[endProfile]] with the same category name.
* The begin- and end- calls must also be properly nested. For example,
* This has to be matched with a call to [[endProfile()]] with the same category name.
* The begin- and end- calls must also be properly nested.
* @param string $token token for the code block
* @param string $category the category of this log message
* @see endProfile
@ -118,7 +123,7 @@ class Logger extends \yii\base\Component
/**
* Marks the end of a code block for profiling.
* This has to be matched with a previous call to [[beginProfile]] with the same category name.
* This has to be matched with a previous call to [[beginProfile()]] with the same category name.
* @param string $token token for the code block
* @param string $category the category of this log message
* @see beginProfile
@ -153,14 +158,14 @@ class Logger extends \yii\base\Component
}
}
$this->messages[] = array($message, $level, $category, $time);
if (count($this->messages) >= $this->flushInterval && $this->flushInterval > 0) {
if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) {
$this->flush();
}
}
/**
* Removes all recorded messages from the memory.
* This method will raise a `flush` event.
* Flushes log messages from memory to targets.
* This method will trigger a [[flush]] event.
*/
public function flush()
{
@ -191,7 +196,7 @@ class Logger extends \yii\base\Component
* You can use an asterisk at the end of a category to do a prefix match.
* For example, 'yii\db\*' will match categories starting with 'yii\db\',
* such as 'yii\db\Connection'.
* @param array $excludeCategories list of categories that you are interested in.
* @param array $excludeCategories list of categories that you want to exclude
* @return array the profiling results. Each array element has the following structure:
* `array($token, $category, $time)`.
*/

Loading…
Cancel
Save