Skip to content

Commit

Permalink
Allow . in ObjectRegistry keys.
Browse files Browse the repository at this point in the history
Allow `.` in object registry keys when a configuration array is present.
This allows cache engines to have dotted names, and also helpers. Doing
this with helpers is a bit silly as you'll end up with helper names you
cannot use. But for Cache it makes sense.

Refs #6919
  • Loading branch information
markstory committed Jul 2, 2015
1 parent 29ca3d1 commit 94635f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Core/ObjectRegistry.php
Expand Up @@ -69,7 +69,12 @@ abstract class ObjectRegistry
*/
public function load($objectName, $config = [])
{
list(, $name) = pluginSplit($objectName);
if (empty($config) && !isset($this->_loaded[$objectName])) {
list(, $name) = pluginSplit($objectName);
} else {
$name = $objectName;
}

$loaded = isset($this->_loaded[$name]);
if ($loaded && !empty($config)) {
$this->_checkDuplicate($name, $config);
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Cache/CacheTest.php
Expand Up @@ -250,6 +250,26 @@ public function testConfigRead()
$this->assertEquals($expected, Cache::config('tests'));
}

/**
* test config() with dotted name
*
* @return void
*/
public function testConfigDottedAlias()
{
Cache::config('cache.dotted', [
'className' => 'File',
'path' => TMP,
'prefix' => 'cache_value_'
]);

$engine = Cache::engine('cache.dotted');
$this->assertContains('cache.dotted', Cache::configured());
$this->assertNotContains('dotted', Cache::configured());
$this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
Cache::drop('cache.dotted');
}

/**
* testGroupConfigs method
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/View/HelperRegistryTest.php
Expand Up @@ -195,6 +195,32 @@ public function testLoadPluginHelper()
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
}

/**
* test loading helpers with dotted aliases
*
* @return void
*/
public function testLoadPluginHelperDottedAlias()
{
Plugin::load(['TestPlugin']);

$result = $this->Helpers->load('thing.helper', [
'className' => 'TestPlugin.OtherHelper',
]);
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
$this->assertInstanceOf(
'TestPlugin\View\Helper\OtherHelperHelper',
$this->Helpers->get('thing.helper'),
'Class is wrong'
);
$this->assertTrue($this->Helpers->has('thing.helper'));
$this->assertFalse($this->Helpers->has('thing'));
$this->assertFalse($this->Helpers->has('helper'));

$this->Helpers->unload('thing.helper');
$this->assertFalse($this->Helpers->has('thing.helper'), 'Should be gone now.');
}

/**
* Test reset.
*
Expand Down

0 comments on commit 94635f0

Please sign in to comment.