A behavior can be used to enhance the functionality of an existing component without modifying its code. In particular, it can "inject" its own methods and properties into the component and make them directly accessible via the component. It can also respond to the events triggered in the component and thus intercept the normal code execution. For more details and usage information on Behavior, see the guide article on behaviors.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Object
Example #1
0
 /**
  * @param string|Behavior $behavior
  */
 public function __construct($behavior)
 {
     if ($behavior instanceof Behavior) {
         $this->_behavior = (string) $behavior->className();
     } else {
         if (is_string($behavior)) {
             $this->_behavior = (string) $behavior;
         }
     }
 }
 /**
  * Init
  */
 public function init()
 {
     if ($this->attributes == null) {
         throw new InvalidConfigException('Property "attributes" must be set');
     }
     parent::init();
 }
Example #3
0
 public function attach($owner)
 {
     parent::attach($owner);
     if (!$this->key) {
         $this->key = constant(get_class($owner) . '::CACHE_KEY');
     }
 }
Example #4
0
 public function attach($owner)
 {
     parent::attach($owner);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_INSERT, [AdminLogEvent::className(), AdminLogEvent::EVENT_CREATE_ITEM], ['title' => $this->titleAttribute, 'icon' => $this->icon]);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_UPDATE, [AdminLogEvent::className(), AdminLogEvent::EVENT_UPDATE_ITEM], ['title' => $this->titleAttribute, 'icon' => $this->icon]);
     $owner->on(BaseActiveRecord::EVENT_BEFORE_DELETE, [AdminLogEvent::className(), AdminLogEvent::EVENT_DELETE_ITEM], ['title' => $this->titleAttribute, 'icon' => $this->icon]);
 }
Example #5
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     if (!is_array($this->attributes) || empty($this->attributes)) {
         throw new InvalidParamException('Invalid or empty attributes array.');
     } else {
         foreach ($this->attributes as $attribute => $config) {
             if (!isset($config['path']) || empty($config['path'])) {
                 throw new InvalidParamException('Path must be set for all attributes.');
             }
             if (!isset($config['tempPath']) || empty($config['tempPath'])) {
                 throw new InvalidParamException('Temporary path must be set for all attributes.');
             }
             if (!isset($config['url']) || empty($config['url'])) {
                 $config['url'] = $this->publish($config['path']);
             }
             $this->attributes[$attribute]['path'] = FileHelper::normalizePath(Yii::getAlias($config['path'])) . DIRECTORY_SEPARATOR;
             $this->attributes[$attribute]['tempPath'] = FileHelper::normalizePath(Yii::getAlias($config['tempPath'])) . DIRECTORY_SEPARATOR;
             $this->attributes[$attribute]['url'] = rtrim($config['url'], '/') . '/';
             $validator = Validator::createValidator('string', $this->owner, $attribute);
             $this->owner->validators[] = $validator;
             unset($validator);
         }
     }
 }
Example #6
0
 /**
  * @inheritdoc
  */
 public function canSetProperty($name, $checkVars = true)
 {
     if (in_array($name, [$this->titleAttribute, $this->keywordsAttribute, $this->descriptionAttribute])) {
         return true;
     }
     return parent::canSetProperty($name, $checkVars);
 }
 /**
  * @param \yii\base\Component $owner
  */
 public function attach($owner)
 {
     parent::attach($owner);
     if (!$this->group) {
         $this->group = get_class($this->owner);
     }
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (empty($this->copyTo)) {
         throw new InvalidConfigException('attribute copyTo must be provided');
     }
 }
Example #9
0
 public function __get($name)
 {
     if ($name == $this->getAttributeName()) {
         return $this->getRelation();
     }
     return parent::__get($name);
 }
Example #10
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     $validators = $owner->validators;
     foreach ($this->rules as $rule) {
         if ($rule instanceof Validator) {
             $validators->append($rule);
             $this->validators[] = $rule;
             // keep a reference in behavior
         } elseif (is_array($rule) && isset($rule[0], $rule[1])) {
             // attributes, validator type
             $validator = Validator::createValidator($rule[1], $owner, (array) $rule[0], array_slice($rule, 2));
             $validators->append($validator);
             $this->validators[] = $validator;
             // keep a reference in behavior
         } else {
             throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
         }
     }
     $owner->on(ActiveRecord::EVENT_BEFORE_INSERT, function () {
         $this->saveRelatedDir($this->txt, $this->id_field, $this->name_field, $this->classDir);
     });
     $owner->on(ActiveRecord::EVENT_BEFORE_UPDATE, function () {
         $this->saveRelatedDir($this->txt, $this->id_field, $this->name_field, $this->classDir);
     });
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (empty($this->sessionVarName)) {
         throw new InvalidConfigException('The $sessionVarName should be configured for this behavior.');
     }
 }
Example #12
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (!$this->app_id || !$this->mch_id || !$this->app_key || !$this->app_secret) {
         throw new InvalidConfigException("WxPay::app_id & mch_id & app_key & app_secret are required to be configured.");
     }
 }
 /**
  * @inheritdoc
  */
 public function canSetProperty($name, $checkVars = true)
 {
     if (isset($this->attributes[$name]) || $name === $this->flagsAttribute) {
         return true;
     }
     return parent::canSetProperty($name, $checkVars);
 }
Example #14
0
 public function attach($owner)
 {
     if (!$owner instanceof ShelterInterface) {
         throw new InvalidParamException('$owner must implement ShelterInterface.');
     }
     parent::attach($owner);
 }
Example #15
0
 public function init()
 {
     parent::init();
     $mark = $this->multiple ? UploadStrategyFactory::UPLOAD_MULTIPLE : UploadStrategyFactory::UPLOAD_SINGLE;
     $this->uploadStrategy = UploadStrategyFactory::get($mark);
     $this->uploadStrategy->setBehavior($this);
 }
 /**
  * @inheritdoc
  */
 public function canSetProperty($name, $checkVars = true)
 {
     if ($name === $this->attribute) {
         return true;
     }
     return parent::canSetProperty($name, $checkVars);
 }
Example #17
0
 /**
  * Indicates whether a property can be read.
  * @param string $name the property name
  * @param boolean $checkVars whether to treat member variables as properties
  */
 public function canGetProperty($name, $checkVars = true)
 {
     if (!in_array($name, $this->attrs)) {
         return parent::canGetProperty($name, $checkVars);
     }
     return true;
 }
Example #18
0
 public function init()
 {
     parent::init();
     //检查配置
     if ($this->offlinUrl !== null) {
     }
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (empty($this->classes)) {
         throw new InvalidConfigException('"classes" param must be provided');
     }
 }
 public function init()
 {
     if (is_null($this->generators)) {
         $this->generators = $this->defaultGenerators();
     }
     parent::init();
 }
Example #21
0
 public function init()
 {
     parent::init();
     if (!$this->meta) {
         $this->meta = ['title' => 'title'];
     }
 }
Example #22
0
 /**
  * @param \skeeks\cms\Controller $owner
  * @throws Exception
  */
 public function attach($owner)
 {
     if (!$owner instanceof \skeeks\cms\Controller) {
         throw new Exception(\Yii::t('app', 'This behavior is designed only to work with {class}', ['class' => \skeeks\cms\Controller::className()]));
     }
     parent::attach($owner);
 }
 /**
  * @inheritdoc
  * @param ActiveRecord $owner
  */
 public function attach($owner)
 {
     if (false === $owner instanceof ActiveRecord) {
         throw new InvalidParamException('SafeSaver can only be attached to ' . 'instances of ActiveRecord');
     }
     parent::attach($owner);
 }
 public function attach($owner)
 {
     parent::attach($owner);
     if ($this->hasMethod('defaultScope')) {
         call_user_func([$owner->modelClass, 'defaultScope'], $owner);
     }
 }
 /**
  * @inheritdoc
  * @param Controller $owner
  */
 public function attach($owner)
 {
     if (false === $owner instanceof Controller) {
         throw new InvalidParamException('QueryLanguageBehaviour can only be attached to ' . 'instances of Controller');
     }
     parent::attach($owner);
 }
Example #26
0
 public function attach($owner)
 {
     parent::attach($owner);
     if ($this->owner->scenario == "search") {
         return true;
     }
     if (empty($this->initial)) {
         throw new Exception("It's required to set an initial state");
     }
     if (empty($this->model_label)) {
         throw new Exception("It's required to set a model label");
     }
     if (empty($this->namespace)) {
         $this->namespace = strtolower(get_class($this->owner)) . '\\status';
     }
     $this->options = array_keys($this->transitions);
     foreach ($this->transitions as $k => $t) {
         if (!is_array($t)) {
             $this->transitions[$k] = explode(",", $t);
         } else {
             $this->transitions[$k] = $t;
         }
     }
     $this->getStatus();
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if (!$this->owner->canGetProperty('idAttribute')) {
         throw new \LogicException("Model {$this->owner->className()} has not 'idAttribute' property");
     }
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if ($this->mode === self::MODE_EVENT) {
         throw new Exception('TrackableBehavior does not yet support events.');
     }
 }
 /** @inheritdoc */
 public function attach($owner)
 {
     if (!is_subclass_of($owner, BaseImagable::className())) {
         throw new Exception("The owner must be inherited from " . BaseImagable::className());
     }
     parent::attach($owner);
 }
Example #30
0
 /**
  * @inheritdoc
  */
 public function attach($owner)
 {
     parent::attach($owner);
     $validators = $this->owner->getValidators();
     $validator = Validator::createValidator('safe', $this->owner, 'template_id');
     $validators->append($validator);
 }