diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index c952f94..93d11e7 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -38,6 +38,7 @@ Yii Framework 2 Change Log - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) +- Enh: Added support to insert an event handler at the beginning of class-level event handler queue (qiangxue) - Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue) - Chg #3175: InvalidCallException, InvalidParamException, UnknownMethodException are now extended from SPL BadMethodCallException (samdark) - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue) diff --git a/framework/base/Event.php b/framework/base/Event.php index 80bb391..d82c372 100644 --- a/framework/base/Event.php +++ b/framework/base/Event.php @@ -74,11 +74,19 @@ class Event extends Object * @param callable $handler the event handler. * @param mixed $data the data to be passed to the event handler when the event is triggered. * When the event handler is invoked, this data can be accessed via [[Event::data]]. + * @param boolean $append whether to append new event handler to the end of the existing + * handler list. If false, the new handler will be inserted at the beginning of the existing + * handler list. * @see off() */ - public static function on($class, $name, $handler, $data = null) + public static function on($class, $name, $handler, $data = null, $append = true) { - self::$_events[$name][ltrim($class, '\\')][] = [$handler, $data]; + $class = ltrim($class, '\\'); + if ($append || empty(self::$_events[$name][$class])) { + self::$_events[$name][$class][] = [$handler, $data]; + } else { + array_unshift(self::$_events[$name][$class], [$handler, $data]); + } } /**