diff --git a/src/Model/Behavior/CounterCacheBehavior.php b/src/Model/Behavior/CounterCacheBehavior.php index a13daf4433e..300912d841c 100644 --- a/src/Model/Behavior/CounterCacheBehavior.php +++ b/src/Model/Behavior/CounterCacheBehavior.php @@ -133,7 +133,7 @@ public function afterDelete(Event $event, Entity $entity) { * @return void */ protected function _processAssociations(Event $event, Entity $entity) { - foreach ($this->_config as $assoc => $settings) { + foreach ($this->config() as $assoc => $settings) { $assoc = $this->_table->association($assoc); $this->_processAssociation($event, $entity, $assoc, $settings); } diff --git a/src/Model/Behavior/TimestampBehavior.php b/src/Model/Behavior/TimestampBehavior.php index c35ae9117af..ca4df06c899 100644 --- a/src/Model/Behavior/TimestampBehavior.php +++ b/src/Model/Behavior/TimestampBehavior.php @@ -71,11 +71,12 @@ class TimestampBehavior extends Behavior { */ public function handleEvent(Event $event, Entity $entity) { $eventName = $event->name(); - $config = $this->config(); + $events = $this->config('events'); $new = $entity->isNew() !== false; + $refresh = $this->config('refreshTimestamp'); - foreach ($config['events'][$eventName] as $field => $when) { + foreach ($events[$eventName] as $field => $when) { if (!in_array($when, ['always', 'new', 'existing'])) { throw new \UnexpectedValueException( sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when) @@ -86,7 +87,7 @@ public function handleEvent(Event $event, Entity $entity) { ($when === 'new' && $new) || ($when === 'existing' && !$new) ) { - $this->_updateField($entity, $field, $config['refreshTimestamp']); + $this->_updateField($entity, $field, $refresh); } } @@ -140,18 +141,19 @@ public function timestamp(\DateTime $ts = null, $refreshTimestamp = false) { * @return bool true if a field is updated, false if no action performed */ public function touch(Entity $entity, $eventName = 'Model.beforeSave') { - $config = $this->config(); - if (!isset($config['events'][$eventName])) { + $events = $this->config('events'); + if (empty($events[$eventName])) { return false; } $return = false; + $refresh = $this->config('refreshTimestamp'); - foreach ($config['events'][$eventName] as $field => $when) { + foreach ($events[$eventName] as $field => $when) { if (in_array($when, ['always', 'existing'])) { $return = true; $entity->dirty($field, false); - $this->_updateField($entity, $field, $config['refreshTimestamp']); + $this->_updateField($entity, $field, $refresh); } } diff --git a/src/Model/Behavior/TranslateBehavior.php b/src/Model/Behavior/TranslateBehavior.php index 59fae9dd265..a9bf640faed 100644 --- a/src/Model/Behavior/TranslateBehavior.php +++ b/src/Model/Behavior/TranslateBehavior.php @@ -140,7 +140,7 @@ public function beforeFind(Event $event, $query) { }; $contain = []; - $fields = $this->config()['fields']; + $fields = $this->config('fields'); $alias = $this->_table->alias(); foreach ($fields as $field) { $contain[$alias . '_' . $field . '_translation'] = $conditions; @@ -163,7 +163,7 @@ public function beforeFind(Event $event, $query) { */ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) { $locale = $entity->get('_locale') ?: $this->locale(); - $table = $this->config()['translationTable']; + $table = $this->config('translationTable'); $newOptions = [$table => ['validate' => false]]; $options['associated'] = $newOptions + $options['associated']; @@ -173,7 +173,7 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) { return; } - $values = $entity->extract($this->config()['fields'], true); + $values = $entity->extract($this->config('fields'), true); $fields = array_keys($values); $primaryKey = (array)$this->_table->primaryKey(); $key = $entity->get(current($primaryKey)); @@ -258,7 +258,7 @@ public function locale($locale = null) { */ public function findTranslations($query, $options) { $locales = isset($options['locales']) ? $options['locales'] : []; - $table = $this->config()['translationTable']; + $table = $this->config('translationTable'); return $query ->contain([$table => function($q) use ($locales, $table) { if ($locales) { @@ -281,7 +281,7 @@ protected function _rowMapper($results, $locale) { return $results->map(function($row) use ($locale) { $options = ['setter' => false, 'guard' => false]; - foreach ($this->config()['fields'] as $field) { + foreach ($this->config('fields') as $field) { $name = $field . '_translation'; $translation = $row->get($name); @@ -348,7 +348,7 @@ protected function _bundleTranslatedFields($entity) { return; } - $fields = $this->config()['fields']; + $fields = $this->config('fields'); $primaryKey = (array)$this->_table->primaryKey(); $key = $entity->get(current($primaryKey)); $find = []; @@ -394,7 +394,7 @@ protected function _bundleTranslatedFields($entity) { * @return array */ protected function _findExistingTranslations($ruleSet) { - $association = $this->_table->association($this->config()['translationTable']); + $association = $this->_table->association($this->config('translationTable')); $query = $association->find() ->select(['id', 'num' => 0]) ->where(current($ruleSet)) diff --git a/src/ORM/Behavior.php b/src/ORM/Behavior.php index 379bc96c946..b84be6dc2ca 100644 --- a/src/ORM/Behavior.php +++ b/src/ORM/Behavior.php @@ -14,6 +14,7 @@ */ namespace Cake\ORM; +use Cake\Core\InstanceConfigTrait; use Cake\Error\Exception; use Cake\Event\EventListener; @@ -92,6 +93,8 @@ */ class Behavior implements EventListener { + use InstanceConfigTrait; + /** * Reflection method cache for behaviors. * @@ -111,13 +114,6 @@ class Behavior implements EventListener { */ protected $_defaultConfig = []; -/** - * Contains configuration. - * - * @var array - */ - protected $_config = []; - /** * Constructor * @@ -130,16 +126,7 @@ class Behavior implements EventListener { * @param array $config The config for this behavior. */ public function __construct(Table $table, array $config = []) { - $this->_config = $config + $this->_defaultConfig; - } - -/** - * Read the configuration being used. - * - * @return array - */ - public function config() { - return $this->_config; + $this->config($config); } /**