Browse Source

Merge pull request #3117 from AD7six/3.0-config-model

Update behavior config usage
Andy Dawson 12 years ago
parent
commit
e9bbb22177

+ 1 - 1
src/Model/Behavior/CounterCacheBehavior.php

@@ -133,7 +133,7 @@ class CounterCacheBehavior extends Behavior {
  * @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);
 		}

+ 10 - 8
src/Model/Behavior/TimestampBehavior.php

@@ -36,7 +36,7 @@ class TimestampBehavior extends Behavior {
  *
  * @var array
  */
-	protected static $_defaultConfig = [
+	protected $_defaultConfig = [
 		'implementedFinders' => [],
 		'implementedMethods' => [
 			'timestamp' => 'timestamp',
@@ -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 @@ class TimestampBehavior extends Behavior {
 				($when === 'new' && $new) ||
 				($when === 'existing' && !$new)
 			) {
-				$this->_updateField($entity, $field, $config['refreshTimestamp']);
+				$this->_updateField($entity, $field, $refresh);
 			}
 		}
 
@@ -140,18 +141,19 @@ class TimestampBehavior extends Behavior {
  * @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);
 			}
 		}
 

+ 8 - 8
src/Model/Behavior/TranslateBehavior.php

@@ -58,7 +58,7 @@ class TranslateBehavior extends Behavior {
  *
  * @var array
  */
-	protected static $_defaultConfig = [
+	protected $_defaultConfig = [
 		'implementedFinders' => ['translations' => 'findTranslations'],
 		'implementedMethods' => ['locale' => 'locale'],
 		'fields' => [],
@@ -140,7 +140,7 @@ class TranslateBehavior extends Behavior {
 		};
 
 		$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 @@ class TranslateBehavior extends Behavior {
  */
 	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 @@ class TranslateBehavior extends Behavior {
 			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 @@ class TranslateBehavior extends Behavior {
  */
 	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 @@ class TranslateBehavior extends Behavior {
 		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 @@ class TranslateBehavior extends Behavior {
 			return;
 		}
 
-		$fields = $this->config()['fields'];
+		$fields = $this->config('fields');
 		$primaryKey = (array)$this->_table->primaryKey();
 		$key = $entity->get(current($primaryKey));
 		$find = [];
@@ -394,7 +394,7 @@ class TranslateBehavior extends Behavior {
  * @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))

+ 5 - 18
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 @@ use Cake\Event\EventListener;
  */
 class Behavior implements EventListener {
 
+	use InstanceConfigTrait;
+
 /**
  * Reflection method cache for behaviors.
  *
@@ -109,14 +112,7 @@ class Behavior implements EventListener {
  *
  * @var array
  */
-	protected static $_defaultConfig = [];
-
-/**
- * Contains configuration.
- *
- * @var array
- */
-	protected $_config = [];
+	protected $_defaultConfig = [];
 
 /**
  * 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 + static::$_defaultConfig;
-	}
-
-/**
- * Read the configuration being used.
- *
- * @return array
- */
-	public function config() {
-		return $this->_config;
+		$this->config($config);
 	}
 
 /**