Commit d2159b12 by Qiang Xue

w

parent 5a2d4406
......@@ -21,7 +21,6 @@ namespace yii\base;
*/
class Behavior extends Component
{
private $_enabled;
private $_owner;
/**
......@@ -34,7 +33,14 @@ class Behavior extends Component
* the behavior is detached from the component.
*
* The method should return an array whose keys are the names of the owner's events
* and values are the names of the behavior methods.
* and values are the names of the behavior methods. For example,
*
* ~~~
* array(
* 'onBeforeValidate' => 'myBeforeValidate',
* 'onAfterValidate' => 'myAfterValidate',
* )
* ~~~
*
* @return array events (keys) and the corresponding behavior method names (values).
*/
......@@ -81,34 +87,4 @@ class Behavior extends Component
{
return $this->_owner;
}
/**
* Returns a value indicating whether this behavior is enabled.
* @return boolean whether this behavior is enabled
*/
public function getEnabled()
{
return $this->_enabled;
}
/**
* Enables or disables the behavior.
* @param boolean $value whether this behavior should be enabled.
*/
public function setEnabled($value)
{
if($this->_enabled!=$value && $this->_owner) {
if($value) {
foreach($this->events() as $event=>$handler) {
$this->_owner->attachEventHandler($event, array($this, $handler));
}
}
else {
foreach($this->events() as $event=>$handler) {
$this->_owner->detachEventHandler($event, array($this, $handler));
}
}
}
$this->_enabled = $value;
}
}
......@@ -97,10 +97,6 @@ namespace yii\base;
* To attach a behavior to a component, call [[attachBehavior]]; and to detach the behavior
* from the component, call [[detachBehavior]].
*
* A behavior can be temporarily enabled or disabled by calling [[enableBehavior]] or
* [[disableBehavior]], respectively. When disabled, the behavior's public properties and methods
* cannot be accessed via the component.
*
* Components created via [[\Yii::createComponent]] have life cycles. In particular,
*
* @author Qiang Xue <qiang.xue@gmail.com>
......@@ -146,7 +142,7 @@ class Component
}
elseif (is_array($this->_b)) { // a behavior property
foreach ($this->_b as $object) {
if ($object->getEnabled() && (property_exists($object, $name) || $object->canGetProperty($name))) {
if (property_exists($object, $name) || $object->canGetProperty($name)) {
return $object->$name;
}
}
......@@ -184,7 +180,7 @@ class Component
}
elseif (is_array($this->_b)) { // behavior
foreach ($this->_b as $object) {
if ($object->getEnabled() && (property_exists($object, $name) || $object->canSetProperty($name))) {
if (property_exists($object, $name) || $object->canSetProperty($name)) {
return $object->$name = $value;
}
}
......@@ -225,7 +221,7 @@ class Component
}
elseif (is_array($this->_b)) {
foreach ($this->_b as $object) {
if ($object->getEnabled() && (property_exists($object, $name) || $object->canGetProperty($name))) {
if (property_exists($object, $name) || $object->canGetProperty($name)) {
return $object->$name !== null;
}
}
......@@ -260,13 +256,11 @@ class Component
}
elseif (is_array($this->_b)) { // behavior property
foreach ($this->_b as $object) {
if ($object->getEnabled()) {
if (property_exists($object, $name)) {
return $object->$name = null;
}
elseif ($object->canSetProperty($name)) {
return $object->$setter(null);
}
if (property_exists($object, $name)) {
return $object->$name = null;
}
elseif ($object->canSetProperty($name)) {
return $object->$setter(null);
}
}
}
......@@ -301,7 +295,7 @@ class Component
{
foreach ($this->_b as $object)
{
if ($object->getEnabled() && method_exists($object, $name)) {
if (method_exists($object, $name)) {
return call_user_func_array(array($object, $name), $parameters);
}
}
......@@ -498,6 +492,9 @@ class Component
public function raiseEvent($name, $event)
{
$name = strtolower($name);
if ($event instanceof Event) {
$event->name = $name;
}
if (isset($this->_e[$name])) {
foreach ($this->_e[$name] as $handler) {
if (is_string($handler) || $handler instanceof \Closure) {
......@@ -612,54 +609,6 @@ class Component
}
/**
* Enables all behaviors attached to this component.
*/
public function enableBehaviors()
{
if ($this->_b !== null) {
foreach ($this->_b as $behavior) {
$behavior->setEnabled(true);
}
}
}
/**
* Disables all behaviors attached to this component.
*/
public function disableBehaviors()
{
if ($this->_b !== null) {
foreach ($this->_b as $behavior) {
$behavior->setEnabled(false);
}
}
}
/**
* Enables an attached behavior.
* A behavior is only effective when it is enabled.
* @param string $name the behavior's name. It uniquely identifies the behavior.
*/
public function enableBehavior($name)
{
if (isset($this->_b[$name])) {
$this->_b[$name]->setEnabled(true);
}
}
/**
* Disables an attached behavior.
* A behavior is only effective when it is enabled.
* @param string $name the behavior's name. It uniquely identifies the behavior.
*/
public function disableBehavior($name)
{
if (isset($this->_b[$name])) {
$this->_b[$name]->setEnabled(false);
}
}
/**
* Evaluates a PHP expression or callback under the context of this component.
*
* Valid PHP callback can be class method name in the form of
......
......@@ -25,12 +25,19 @@ namespace yii\base;
class Event extends Component
{
/**
* @var string the event name. This property is set by [[Component::raiseEvent]].
* Event handlers may use this property to check what event it is handling.
* The event name is in lower case.
*/
public $name;
/**
* @var object the sender of this event
*/
public $sender;
/**
* @var boolean whether the event is handled. Defaults to false.
* When a handler sets this to be true, the rest of the uninvoked event handlers will be canceled.
* When a handler sets this to be true, the event processing will stop and
* ignore the rest of the uninvoked event handlers.
*/
public $handled = false;
/**
......
......@@ -175,13 +175,17 @@ class ComponentTest extends \yii\test\TestCase
$this->setExpectedException('yii\base\Exception');
$this->component->onMyEvent();
}
public function testDetachBehavior() {
public function testDetachBehavior()
{
$component=new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a',$behavior);
$this->assertSame($behavior,$component->detachBehavior('a'));
}
public function testDetachingBehaviors() {
public function testDetachingBehaviors()
{
$component=new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a',$behavior);
......@@ -189,31 +193,17 @@ class ComponentTest extends \yii\test\TestCase
$this->setExpectedException('yii\base\Exception');
$component->test();
}
public function testEnablingBehavior() {
$component=new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a',$behavior);
$component->disableBehavior('a');
$this->assertFalse($behavior->getEnabled());
$component->enableBehavior('a');
$this->assertTrue($behavior->getEnabled());
}
public function testEnablingBehaviors() {
$component=new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a',$behavior);
$component->disableBehaviors();
$this->assertFalse($behavior->getEnabled());
$component->enableBehaviors();
$this->assertTrue($behavior->getEnabled());
}
public function testAsa() {
public function testAsa()
{
$component=new NewComponent;
$behavior = new NewBehavior;
$component->attachBehavior('a',$behavior);
$this->assertSame($behavior,$component->asa('a'));
}
public function testEvaluateExpression() {
public function testEvaluateExpression()
{
$component = new NewComponent;
$this->assertEquals('Hello world',$component->evaluateExpression('"Hello $who"',array('who' => 'world')));
$this->assertEquals('Hello world',$component->evaluateExpression(array($component,'exprEvaluator'),array('who' => 'world')));
......
- add more doc to Model
- CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
- Can consider merging UniqueValidator and ExistValidator and using a NOT property.
- design of component life cycles: init() and afterConstruct()
* construct object
* preinit
* attachBehaviors
* initialize properties
* init
* ...
* destruct
- get/setFlash() should be moved to session component
- support optional parameter in URL patterns
- api doc builder
* support for markdown syntax
* support for [[name]]
* consider to be released as a separate tool for user app docs
- cache components
- base
* add more doc to Model
* error/exception handling
- validators
* CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
* FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
* consider merging UniqueValidator and ExistValidator and using a NOT property.
- console command support
- built-in console commands
+ api doc builder
* support for markdown syntax
* support for [[name]]
* consider to be released as a separate tool for user app docs
- caching
* a way to invalidate/clear cached data
* a command to clear cached data
- console command support
- db
* DAO
* schema
* AR
* document-based
* key-value-based
- gii
- logging
- i18n
* consider using PHP built-in support and data
* message translations, choice format
* formatting: number and date
* parsing??
- error/exception handling
- helpers
* array
* image
* string
- web: TBD
\ No newline at end of file
* file
- web: TBD
* get/setFlash() should be moved to session component
* support optional parameter in URL patterns
- gii
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