Navigation Menu

Skip to content

Commit

Permalink
Rename WidgetRegistry to WidgetLocator
Browse files Browse the repository at this point in the history
Refs #11697
  • Loading branch information
raul338 committed Feb 13, 2018
1 parent 92b140b commit bcca84e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
39 changes: 20 additions & 19 deletions src/View/Helper/FormHelper.php
Expand Up @@ -24,7 +24,7 @@
use Cake\View\Helper;
use Cake\View\StringTemplateTrait;
use Cake\View\View;
use Cake\View\Widget\WidgetRegistry;
use Cake\View\Widget\WidgetLocator;
use DateTime;
use RuntimeException;
use Traversable;
Expand Down Expand Up @@ -217,11 +217,11 @@ class FormHelper extends Helper
protected $_unlockedFields = [];

/**
* Registry for input widgets.
* Locator for input widgets.
*
* @var \Cake\View\Widget\WidgetRegistry
* @var \Cake\View\Widget\WidgetLocator
*/
protected $_registry;
protected $_locator;

/**
* Context for the current form.
Expand Down Expand Up @@ -260,11 +260,11 @@ class FormHelper extends Helper
*/
public function __construct(View $View, array $config = [])
{
$registry = null;
$locator = null;
$widgets = $this->_defaultWidgets;
if (isset($config['registry'])) {
$registry = $config['registry'];
unset($config['registry']);
if (isset($config['locator'])) {
$locator = $config['locator'];
unset($config['locator']);
}
if (isset($config['widgets'])) {
if (is_string($config['widgets'])) {
Expand All @@ -276,29 +276,30 @@ public function __construct(View $View, array $config = [])

parent::__construct($View, $config);

$this->widgetRegistry($registry, $widgets);
$this->widgetLocator($locator, $widgets);
$this->_idPrefix = $this->getConfig('idPrefix');
}

/**
* Set the widget registry the helper will use.
*
* @param \Cake\View\Widget\WidgetRegistry|null $instance The registry instance to set.
* @param \Cake\View\Widget\WidgetLocator|null $instance The locator instance to set.
* @param array $widgets An array of widgets
* @return \Cake\View\Widget\WidgetRegistry
* @return \Cake\View\Widget\WidgetLocator
* @since 3.6.0
*/
public function widgetRegistry(WidgetRegistry $instance = null, $widgets = [])
public function widgetLocator(WidgetLocator $instance = null, $widgets = [])
{
if ($instance === null) {
if ($this->_registry === null) {
$this->_registry = new WidgetRegistry($this->templater(), $this->_View, $widgets);
if ($this->_locator === null) {
$this->_locator = new WidgetLocator($this->templater(), $this->_View, $widgets);
}

return $this->_registry;
return $this->_locator;
}
$this->_registry = $instance;
$this->_locator = $instance;

return $this->_registry;
return $this->_locator;
}

/**
Expand Down Expand Up @@ -2771,7 +2772,7 @@ protected function _getContext($data = [])
*/
public function addWidget($name, $spec)
{
$this->_registry->add([$name => $spec]);
$this->_locator->add([$name => $spec]);
}

/**
Expand All @@ -2793,7 +2794,7 @@ public function widget($name, array $data = [])
$secure = $data['secure'];
unset($data['secure']);
}
$widget = $this->_registry->get($name);
$widget = $this->_locator->get($name);
$out = $widget->render($data, $this->context());
if (isset($data['name']) && $secure !== null && $secure !== self::SECURE_SKIP) {
foreach ($widget->secureFields($data) as $field) {
Expand Down
Expand Up @@ -9,7 +9,7 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @since 3.6.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Widget;
Expand All @@ -36,7 +36,7 @@
*
* Widgets can ask for the current view by using the `_view` widget.
*/
class WidgetRegistry
class WidgetLocator
{

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -228,21 +228,21 @@ public function testConstructWithWidgets()
]
];
$helper = new FormHelper($this->View, $config);
$registry = $helper->widgetRegistry();
$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
$locator = $helper->widgetLocator();
$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
}

/**
* Test that when specifying custom widgets config file and it should be
* added to widgets array. WidgetRegistry will load widgets in constructor.
* added to widgets array. WidgetLocator will load widgets in constructor.
*
* @return void
*/
public function testConstructWithWidgetsConfig()
{
$helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
$registry = $helper->widgetRegistry();
$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
$locator = $helper->widgetLocator();
$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
}

/**
Expand Down
Expand Up @@ -18,12 +18,12 @@
use Cake\TestSuite\TestCase;
use Cake\View\StringTemplate;
use Cake\View\View;
use Cake\View\Widget\WidgetRegistry;
use Cake\View\Widget\WidgetLocator;

/**
* WidgetRegistry test case
* WidgetLocator test case
*/
class WidgetRegistryTestCase extends TestCase
class WidgetLocatorTestCase extends TestCase
{

/**
Expand All @@ -49,7 +49,7 @@ public function testAddInConstructor()
'text' => ['Cake\View\Widget\BasicWidget'],
'label' => ['Label'],
];
$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
$inputs = new WidgetLocator($this->templates, $this->view, $widgets);
$result = $inputs->get('text');
$this->assertInstanceOf('Cake\View\Widget\BasicWidget', $result);

Expand All @@ -58,13 +58,13 @@ public function testAddInConstructor()
}

/**
* Test getting view instance from registry.
* Test getting view instance from locator.
*
* @return void
*/
public function testGetViewInstance()
{
$inputs = new WidgetRegistry($this->templates, $this->view, []);
$inputs = new WidgetLocator($this->templates, $this->view, []);

$result = $inputs->get('_view');
$this->assertInstanceOf('Cake\View\View', $result);
Expand All @@ -81,7 +81,7 @@ public function testAddWidgetsFromConfigInConstructor()
'text' => ['Cake\View\Widget\BasicWidget'],
'test_widgets',
];
$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
$inputs = new WidgetLocator($this->templates, $this->view, $widgets);
$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
}

Expand All @@ -97,7 +97,7 @@ public function testAddPluginWidgetsFromConfigInConstructor()
'text' => ['Cake\View\Widget\BasicWidget'],
'TestPlugin.test_widgets',
];
$inputs = new WidgetRegistry($this->templates, $this->view, $widgets);
$inputs = new WidgetLocator($this->templates, $this->view, $widgets);
$this->assertInstanceOf('Cake\View\Widget\LabelWidget', $inputs->get('text'));
}

Expand All @@ -108,15 +108,15 @@ public function testAddPluginWidgetsFromConfigInConstructor()
*/
public function testAdd()
{
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$result = $inputs->add([
'text' => ['Cake\View\Widget\BasicWidget'],
]);
$this->assertNull($result);
$result = $inputs->get('text');
$this->assertInstanceOf('Cake\View\Widget\WidgetInterface', $result);

$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$result = $inputs->add([
'hidden' => 'Cake\View\Widget\BasicWidget',
]);
Expand All @@ -134,7 +134,7 @@ public function testAddInvalidType()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Widget objects must implement Cake\View\Widget\WidgetInterface');
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->add([
'text' => new \StdClass()
]);
Expand All @@ -147,7 +147,7 @@ public function testAddInvalidType()
*/
public function testGet()
{
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->add([
'text' => ['Cake\View\Widget\BasicWidget'],
]);
Expand All @@ -163,7 +163,7 @@ public function testGet()
*/
public function testGetFallback()
{
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->add([
'_default' => ['Cake\View\Widget\BasicWidget'],
]);
Expand All @@ -183,7 +183,7 @@ public function testGetNoFallbackError()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unknown widget "foo"');
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->clear();
$inputs->get('foo');
}
Expand All @@ -195,7 +195,7 @@ public function testGetNoFallbackError()
*/
public function testGetResolveDependency()
{
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->clear();
$inputs->add([
'label' => ['Cake\View\Widget\LabelWidget'],
Expand All @@ -214,7 +214,7 @@ public function testGetResolveDependencyMissingClass()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to locate widget class "TestApp\View\DerpWidget"');
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->add(['test' => ['TestApp\View\DerpWidget']]);
$inputs->get('test');
}
Expand All @@ -228,7 +228,7 @@ public function testGetResolveDependencyMissingDependency()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unknown widget "label"');
$inputs = new WidgetRegistry($this->templates, $this->view);
$inputs = new WidgetLocator($this->templates, $this->view);
$inputs->clear();
$inputs->add(['multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'label']]);
$inputs->get('multicheckbox');
Expand Down

0 comments on commit bcca84e

Please sign in to comment.