Skip to content

Commit

Permalink
Convert Behavior + BehaviorRegistry to use config instead of settings.
Browse files Browse the repository at this point in the history
Refs #2298
  • Loading branch information
markstory committed Nov 15, 2013
1 parent ca94e7f commit b3d93ad
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 54 deletions.
50 changes: 25 additions & 25 deletions Cake/ORM/Behavior.php
Expand Up @@ -103,61 +103,61 @@ class Behavior implements EventListener {
protected static $_reflectionCache = [];

/**
* Default settings
* Default configuration
*
* These are merged with user-provided settings when the behavior is used.
* These are merged with user-provided configuration when the behavior is used.
*
* @var array
*/
protected $_defaultSettings = [];
protected $_defaultConfig = [];

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

/**
* Constructor
*
* Merge settings with the default and store in the settings property
* Merge config with the default and store in the config property
*
* Does not retain a reference to the Table object. If you need this
* you should override the constructor.
*
* @param Table $table The table this behavior is attached to.
* @param array $settings The settings for this behavior.
* @param array $config The config for this behavior.
*/
public function __construct(Table $table, array $settings = []) {
$this->_settings = $settings + $this->_defaultSettings;
public function __construct(Table $table, array $config = []) {
$this->_config = $config + $this->_defaultConfig;
}

/**
* Read the settings being used.
* Read the configuration being used.
*
* @return array
*/
public function settings() {
return $this->_settings;
public function config() {
return $this->_config;
}

/**
* verifySettings
* verifyConfig
*
* Check that implemented* keys contain values pointing at callable
*
* @return void
* @throws Cake\Error\Exception if settings are invalid
* @throws Cake\Error\Exception if config are invalid
*/
public function verifySettings() {
public function verifyConfig() {
$keys = ['implementedFinders', 'implementedMethods'];
foreach ($keys as $key) {
if (!isset($this->_settings[$key])) {
if (!isset($this->_config[$key])) {
continue;
}

foreach ($this->_settings[$key] as $method) {
foreach ($this->_config[$key] as $method) {
if (!is_callable([$this, $method])) {
throw new Exception(__d('cake_dev', 'The method %s is not callable on class %s', $method, get_class($this)));
}
Expand All @@ -184,8 +184,8 @@ public function implementedEvents() {
'Model.beforeDelete' => 'beforeDelete',
'Model.afterDelete' => 'afterDelete',
];
$settings = $this->settings();
$priority = isset($settings['priority']) ? $settings['priority'] : null;
$config = $this->config();
$priority = isset($config['priority']) ? $config['priority'] : null;
$events = [];

foreach ($eventMap as $event => $method) {
Expand Down Expand Up @@ -219,15 +219,15 @@ public function implementedEvents() {
* With the above example, a call to `$Table->find('this')` will call `$Behavior->findThis()`
* and a call to `$Table->find('alias')` will call `$Behavior->findMethodName()`
*
* It is recommended, though not required, to define implementedFinders in the settings property
* It is recommended, though not required, to define implementedFinders in the config property
* of child classes such that it is not necessary to use reflections to derive the available
* method list. See core behaviors for examples
*
* @return array
*/
public function implementedFinders() {
if (isset($this->_settings['implementedFinders'])) {
return $this->_settings['implementedFinders'];
if (isset($this->_config['implementedFinders'])) {
return $this->_config['implementedFinders'];
}

$reflectionMethods = $this->_reflectionCache();
Expand All @@ -249,15 +249,15 @@ public function implementedFinders() {
* With the above example, a call to `$Table->method()` will call `$Behavior->method()`
* and a call to `$Table->aliasedmethod()` will call `$Behavior->somethingElse()`
*
* It is recommended, though not required, to define implementedFinders in the settings property
* It is recommended, though not required, to define implementedFinders in the config property
* of child classes such that it is not necessary to use reflections to derive the available
* method list. See core behaviors for examples
*
* @return array
*/
public function implementedMethods() {
if (isset($this->_settings['implementedMethods'])) {
return $this->_settings['implementedMethods'];
if (isset($this->_config['implementedMethods'])) {
return $this->_config['implementedMethods'];
}

$reflectionMethods = $this->_reflectionCache();
Expand Down
8 changes: 4 additions & 4 deletions Cake/ORM/BehaviorRegistry.php
Expand Up @@ -104,12 +104,12 @@ protected function _throwMissingClassError($class, $plugin) {
*
* @param string $class The classname that is missing.
* @param string $alias The alias of the object.
* @param array $settings An array of settings to use for the behavior.
* @param array $config An array of config to use for the behavior.
* @return Behavior The constructed behavior class.
*/
protected function _create($class, $alias, $settings) {
$instance = new $class($this->_table, $settings);
$enable = isset($settings['enabled']) ? $settings['enabled'] : true;
protected function _create($class, $alias, $config) {
$instance = new $class($this->_table, $config);
$enable = isset($config['enabled']) ? $config['enabled'] : true;
if ($enable) {
$this->_eventManager->attach($instance);
}
Expand Down
8 changes: 4 additions & 4 deletions Cake/Test/TestCase/ORM/BehaviorRegistryTest.php
Expand Up @@ -56,10 +56,10 @@ public function tearDown() {
*/
public function testLoad() {
Plugin::load('TestPlugin');
$settings = ['alias' => 'Sluggable', 'replacement' => '-'];
$result = $this->Behaviors->load('Sluggable', $settings);
$config = ['alias' => 'Sluggable', 'replacement' => '-'];
$result = $this->Behaviors->load('Sluggable', $config);
$this->assertInstanceOf('TestApp\Model\Behavior\SluggableBehavior', $result);
$this->assertEquals($settings, $result->settings());
$this->assertEquals($config, $result->config());

$result = $this->Behaviors->load('TestPlugin.PersisterOne');
$this->assertInstanceOf('TestPlugin\Model\Behavior\PersisterOneBehavior', $result);
Expand Down Expand Up @@ -145,7 +145,7 @@ public function testHasMethod() {
$this->assertTrue($this->Behaviors->hasMethod('PERSIST'));

$this->assertFalse($this->Behaviors->hasMethod('__construct'));
$this->assertFalse($this->Behaviors->hasMethod('settings'));
$this->assertFalse($this->Behaviors->hasMethod('config'));
$this->assertFalse($this->Behaviors->hasMethod('implementedEvents'));

$this->assertFalse($this->Behaviors->hasMethod('nope'));
Expand Down
35 changes: 14 additions & 21 deletions Cake/Test/TestCase/ORM/BehaviorTest.php
Expand Up @@ -78,13 +78,6 @@ public function findFoo() {
public function doSomething() {
}

/**
* verifySettings
*/
public function verifySettings() {
parent::verifySettings();
}

/**
* implementedEvents
*
Expand Down Expand Up @@ -133,9 +126,9 @@ class BehaviorTest extends TestCase {
*/
public function testConstructor() {
$table = $this->getMock('Cake\ORM\Table');
$settings = ['key' => 'value'];
$behavior = new TestBehavior($table, $settings);
$this->assertEquals($settings, $behavior->settings());
$config = ['key' => 'value'];
$behavior = new TestBehavior($table, $config);
$this->assertEquals($config, $behavior->config());
}

public function testReflectionCache() {
Expand Down Expand Up @@ -277,34 +270,34 @@ public function testImplementedFindersDisabled() {
}

/**
* testVerifySettings
* testVerifyConfig
*
* Don't expect an exception to be thrown
*
* @return void
*/
public function testVerifySettings() {
public function testVerifyConfig() {
$table = $this->getMock('Cake\ORM\Table');
$behavior = new Test2Behavior($table);
$behavior->verifySettings();
$behavior->verifyConfig();
$this->assertTrue(true, 'No exception thrown');
}

/**
* testVerifySettingsImplementedFindersOverriden
* testVerifyConfigImplementedFindersOverriden
*
* Simply don't expect an exception to be thrown
*
* @return void
*/
public function testVerifySettingsImplementedFindersOverriden() {
public function testVerifyConfigImplementedFindersOverriden() {
$table = $this->getMock('Cake\ORM\Table');
$behavior = new Test2Behavior($table, [
'implementedFinders' => [
'aliased' => 'findFoo'
]
]);
$behavior->verifySettings();
$behavior->verifyConfig();
$this->assertTrue(true, 'No exception thrown');
}

Expand All @@ -323,25 +316,25 @@ public function testVerifyImplementedFindersInvalid() {
'aliased' => 'findNotDefined'
]
]);
$behavior->verifySettings();
$behavior->verifyConfig();
}

/**
* testVerifySettingsImplementedMethodsOverriden
* testVerifyConfigImplementedMethodsOverriden
*
* Don't expect an exception to be thrown
*
* @return void
*/
public function testVerifySettingsImplementedMethodsOverriden() {
public function testVerifyConfigImplementedMethodsOverriden() {
$table = $this->getMock('Cake\ORM\Table');
$behavior = new Test2Behavior($table);
$behavior = new Test2Behavior($table, [
'implementedMethods' => [
'aliased' => 'doSomething'
]
]);
$behavior->verifySettings();
$behavior->verifyConfig();
$this->assertTrue(true, 'No exception thrown');
}

Expand All @@ -360,7 +353,7 @@ public function testVerifyImplementedMethodsInvalid() {
'aliased' => 'iDoNotExist'
]
]);
$behavior->verifySettings();
$behavior->verifyConfig();
}

}

0 comments on commit b3d93ad

Please sign in to comment.