Commit 46041d0c by Qiang Xue

Fixes #3280: Support dynamically attaching anonymous behaviors

parent 0087339a
...@@ -126,6 +126,16 @@ $component->attachBehavior('myBehavior3', [ ...@@ -126,6 +126,16 @@ $component->attachBehavior('myBehavior3', [
]); ]);
``` ```
You may attach multiple behaviors at once by using the [[yii\base\Component::attachBehaviors()]] method.
For example,
```php
$component->attachBehaviors([
'myBehavior1' => new MyBehavior, // a named behavior
MyBehavior::className(), // an anonymous behavior
]);
```
You may also attach behaviors through [configurations](concept-configurations.md). For more details, please You may also attach behaviors through [configurations](concept-configurations.md). For more details, please
refer to the [Configurations](concept-configurations.md#configuration-format) section. refer to the [Configurations](concept-configurations.md#configuration-format) section.
......
...@@ -71,6 +71,7 @@ Yii Framework 2 Change Log ...@@ -71,6 +71,7 @@ Yii Framework 2 Change Log
- Enh #3232: Added `export()` and `exportAsString()` methods to `yii\helpers\BaseVarDumper` (klimov-paul) - Enh #3232: Added `export()` and `exportAsString()` methods to `yii\helpers\BaseVarDumper` (klimov-paul)
- Enh #3244: Allow logging complex data such as arrays and object via the log system (cebe) - Enh #3244: Allow logging complex data such as arrays and object via the log system (cebe)
- Enh #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe) - Enh #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe)
- Enh #3280: Support dynamically attaching anonymous behaviors (qiangxue)
- Enh #3284: Added support for checking multiple ETags by `yii\filters\HttpCache` (qiangxue) - Enh #3284: Added support for checking multiple ETags by `yii\filters\HttpCache` (qiangxue)
- Enh #3298: Supported configuring `View::theme` using a class name (netyum, qiangxue) - Enh #3298: Supported configuring `View::theme` using a class name (netyum, qiangxue)
- Enh #3328: `BaseMailer` generates better text body from html body (armab) - Enh #3328: `BaseMailer` generates better text body from html body (armab)
......
...@@ -660,7 +660,9 @@ class Component extends Object ...@@ -660,7 +660,9 @@ class Component extends Object
/** /**
* Attaches a behavior to this component. * Attaches a behavior to this component.
* @param string $name the name of the behavior. * @param string|integer $name the name of the behavior. If this is an integer, it means the behavior
* is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name
* will be detached first.
* @param string|array|Behavior $behavior the behavior to be attached * @param string|array|Behavior $behavior the behavior to be attached
* @return Behavior the attached behavior. * @return Behavior the attached behavior.
*/ */
...@@ -669,11 +671,17 @@ class Component extends Object ...@@ -669,11 +671,17 @@ class Component extends Object
if (!($behavior instanceof Behavior)) { if (!($behavior instanceof Behavior)) {
$behavior = Yii::createObject($behavior); $behavior = Yii::createObject($behavior);
} }
if (isset($this->_behaviors[$name])) { if (is_int($name)) {
$this->_behaviors[$name]->detach(); $behavior->attach($this);
$this->_behaviors[] = $behavior;
} else {
if (isset($this->_behaviors[$name])) {
$this->_behaviors[$name]->detach();
}
$behavior->attach($this);
$this->_behaviors[$name] = $behavior;
} }
$behavior->attach($this);
return $this->_behaviors[$name] = $behavior; return $behavior;
} }
} }
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