Skip to content

Commit

Permalink
use config method in behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Mar 25, 2014
1 parent e70ee09 commit da8de77
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Model/Behavior/CounterCacheBehavior.php
Expand Up @@ -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);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Model/Behavior/TimestampBehavior.php
Expand Up @@ -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)
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/Model/Behavior/TranslateBehavior.php
Expand Up @@ -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;
Expand All @@ -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'];

Expand All @@ -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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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))
Expand Down
21 changes: 4 additions & 17 deletions src/ORM/Behavior.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ORM;

use Cake\Core\InstanceConfigTrait;
use Cake\Error\Exception;
use Cake\Event\EventListener;

Expand Down Expand Up @@ -92,6 +93,8 @@
*/
class Behavior implements EventListener {

use InstanceConfigTrait;

/**
* Reflection method cache for behaviors.
*
Expand All @@ -111,13 +114,6 @@ class Behavior implements EventListener {
*/
protected $_defaultConfig = [];

/**
* Contains configuration.
*
* @var array
*/
protected $_config = [];

/**
* Constructor
*
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit da8de77

Please sign in to comment.