From 94635f061c6b1b530c7997369d9bced9cbda03c7 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 1 Jul 2015 23:41:09 -0400 Subject: [PATCH] Allow `.` in ObjectRegistry keys. 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 --- src/Core/ObjectRegistry.php | 7 +++++- tests/TestCase/Cache/CacheTest.php | 20 +++++++++++++++++ tests/TestCase/View/HelperRegistryTest.php | 26 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Core/ObjectRegistry.php b/src/Core/ObjectRegistry.php index 4b6272def9a..cc019a7db5a 100644 --- a/src/Core/ObjectRegistry.php +++ b/src/Core/ObjectRegistry.php @@ -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); diff --git a/tests/TestCase/Cache/CacheTest.php b/tests/TestCase/Cache/CacheTest.php index ff9d120e755..beccd574d5c 100644 --- a/tests/TestCase/Cache/CacheTest.php +++ b/tests/TestCase/Cache/CacheTest.php @@ -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 */ diff --git a/tests/TestCase/View/HelperRegistryTest.php b/tests/TestCase/View/HelperRegistryTest.php index 62f266df983..5ddda084f06 100644 --- a/tests/TestCase/View/HelperRegistryTest.php +++ b/tests/TestCase/View/HelperRegistryTest.php @@ -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. *