Fr3nch13/CakePHP Utilities

WidgetLocator
in package

A registry/factory for input widgets.

Can be used by helpers/view logic to build form widgets and other HTML widgets.

This class handles the mapping between names and concrete classes. It also has a basic name based dependency resolver that allows widgets to depend on each other.

Each widget should expect a StringTemplate instance as their first argument. All other dependencies will be included after.

Widgets can ask for the current view by using the _view widget.

Table of Contents

Properties

$_templates  : StringTemplate
Templates to use.
$_view  : View
View instance.
$_widgets  : array<string|int, mixed>
Array of widgets + widget configuration.

Methods

__construct()  : mixed
Constructor
add()  : void
Adds or replaces existing widget instances/configuration with new ones.
clear()  : void
Clear the registry and reset the widgets.
get()  : WidgetInterface
Get a widget.
load()  : void
Load a config file containing widgets.
_resolveWidget()  : WidgetInterface
Resolves a widget spec into an instance.

Properties

$_widgets

Array of widgets + widget configuration.

protected array<string|int, mixed> $_widgets = []

Methods

__construct()

Constructor

public __construct(StringTemplate $templates, View $view[, array<string|int, mixed> $widgets = [] ]) : mixed
Parameters
$templates : StringTemplate

Templates instance to use.

$view : View

The view instance to set as a widget.

$widgets : array<string|int, mixed> = []

See add() method for more information.

add()

Adds or replaces existing widget instances/configuration with new ones.

public add(array<string|int, mixed> $widgets) : void

Widget arrays can either be descriptions or instances. For example:

$registry->add([
  'label' => new MyLabelWidget($templates),
  'checkbox' => ['Fancy.MyCheckbox', 'label']
]);

The above shows how to define widgets as instances or as descriptions including dependencies. Classes can be defined with plugin notation, or fully namespaced class names.

Parameters
$widgets : array<string|int, mixed>

Array of widgets to use.

Tags
throws
RuntimeException

When class does not implement WidgetInterface.

clear()

Clear the registry and reset the widgets.

public clear() : void

get()

Get a widget.

public get(string $name) : WidgetInterface

Will either fetch an already created widget, or create a new instance if the widget has been defined. If the widget is undefined an instance of the _default widget will be returned. An exception will be thrown if the _default widget is undefined.

Parameters
$name : string

The widget name to get.

Tags
throws
RuntimeException

when widget is undefined.

Return values
WidgetInterface

WidgetInterface instance.

load()

Load a config file containing widgets.

public load(string $file) : void

Widget files should define a $config variable containing all the widgets to load. Loaded widgets will be merged with existing widgets.

Parameters
$file : string

The file to load

_resolveWidget()

Resolves a widget spec into an instance.

protected _resolveWidget(mixed $config) : WidgetInterface
Parameters
$config : mixed

The widget config.

Tags
throws
ReflectionException
Return values
WidgetInterface

Widget instance.


        
On this page

Search results