Fr3nch13/CakePHP Utilities

BehaviorRegistry extends ObjectRegistry
in package
implements EventDispatcherInterface uses EventDispatcherTrait

BehaviorRegistry is used as a registry for loaded behaviors and handles loading and constructing behavior objects.

This class also provides method for checking and dispatching behavior methods.

Tags
extends

\Cake\Core\ObjectRegistry<\Cake\ORM\Behavior>

Table of Contents

Interfaces

EventDispatcherInterface
Objects implementing this interface can emit events.

Properties

$_eventClass  : string
Default class name for new event objects.
$_eventManager  : EventManagerInterface|null
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
$_finderMap  : array<string, array<string|int, mixed>>
Finder method mappings.
$_loaded  : array<string|int, object>
Map of loaded objects.
$_methodMap  : array<string, array<string|int, mixed>>
Method mappings.
$_table  : Table
The table using this registry.

Methods

__construct()  : mixed
Constructor
__debugInfo()  : array<string, mixed>
Debug friendly object properties.
__get()  : object|null
Provide public read access to the loaded objects
__isset()  : bool
Provide isset access to _loaded
__set()  : void
Sets an object.
__unset()  : void
Unsets an object.
call()  : mixed
Invoke a method on a behavior.
callFinder()  : Query
Invoke a finder on a behavior.
className()  : string|null
Resolve a behavior classname.
count()  : int
Returns the number of loaded objects.
dispatchEvent()  : EventInterface
Wrapper for creating and dispatching events.
get()  : object
Get loaded object instance.
getEventManager()  : EventManagerInterface
Returns the Cake\Event\EventManager manager instance for this object.
getIterator()  : Traversable
Returns an array iterator.
has()  : bool
Check whether a given object is loaded.
hasFinder()  : bool
Check if any loaded behavior implements the named finder.
hasMethod()  : bool
Check if any loaded behavior implements a method.
load()  : mixed
Loads/constructs an object instance.
loaded()  : array<string|int, string>
Get the list of loaded objects.
normalizeArray()  : array<string, array<string|int, mixed>>
Normalizes an object array, creates an array that makes lazy loading easier
reset()  : $this
Clear loaded instances in the registry.
set()  : $this
Set an object directly into the registry by name.
setEventManager()  : $this
Returns the Cake\Event\EventManagerInterface instance for this object.
setTable()  : void
Attaches a table instance to this registry.
unload()  : $this
Remove an object from the registry.
_checkDuplicate()  : void
Check for duplicate object loading.
_create()  : Behavior
Create the behavior instance.
_getMethods()  : array<string|int, mixed>
Get the behavior methods and ensure there are no duplicates.
_resolveClassName()  : string|null
Resolve a behavior classname.
_throwMissingClassError()  : void
Throws an exception when a behavior is missing.

Properties

$_eventClass

Default class name for new event objects.

protected string $_eventClass = \Cake\Event\Event::class

$_finderMap

Finder method mappings.

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

$_loaded

Map of loaded objects.

protected array<string|int, object> $_loaded = []
Tags
psalm-var

array<array-key, TObject>

$_methodMap

Method mappings.

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

Methods

__construct()

Constructor

public __construct([Table|null $table = null ]) : mixed
Parameters
$table : Table|null = null

The table this registry is attached to.

__debugInfo()

Debug friendly object properties.

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__get()

Provide public read access to the loaded objects

public __get(string $name) : object|null
Parameters
$name : string

Name of property to read

Tags
psalm-return

TObject|null

Return values
object|null

__isset()

Provide isset access to _loaded

public __isset(string $name) : bool
Parameters
$name : string

Name of object being checked.

Return values
bool

__set()

Sets an object.

public __set(string $name, object $object) : void
Parameters
$name : string

Name of a property to set.

$object : object

Object to set.

Tags
psalm-param

TObject $object

__unset()

Unsets an object.

public __unset(string $name) : void
Parameters
$name : string

Name of a property to unset.

call()

Invoke a method on a behavior.

public call(string $method[, array<string|int, mixed> $args = [] ]) : mixed
Parameters
$method : string

The method to invoke.

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

The arguments you want to invoke the method with.

Tags
throws
BadMethodCallException

When the method is unknown.

Return values
mixed

The return value depends on the underlying behavior method.

callFinder()

Invoke a finder on a behavior.

public callFinder(string $type[, array<string|int, mixed> $args = [] ]) : Query
Parameters
$type : string

The finder type to invoke.

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

The arguments you want to invoke the method with.

Tags
throws
BadMethodCallException

When the method is unknown.

Return values
Query

The return value depends on the underlying behavior method.

className()

Resolve a behavior classname.

public static className(string $class) : string|null
Parameters
$class : string

Partial classname to resolve.

Tags
psalm-return

class-string|null

Return values
string|null

Either the correct classname or null.

count()

Returns the number of loaded objects.

public count() : int
Return values
int

dispatchEvent()

Wrapper for creating and dispatching events.

public dispatchEvent(string $name[, array<string|int, mixed>|null $data = null ][, object|null $subject = null ]) : EventInterface

Returns a dispatched event.

Parameters
$name : string

Name of the event.

$data : array<string|int, mixed>|null = null

Any value you wish to be transported with this event to it can be read by listeners.

$subject : object|null = null

The object that this event applies to ($this by default).

Return values
EventInterface

get()

Get loaded object instance.

public get(string $name) : object
Parameters
$name : string

Name of object.

Tags
throws
RuntimeException

If not loaded or found.

psalm-return

TObject

Return values
object

Object instance.

getEventManager()

Returns the Cake\Event\EventManager manager instance for this object.

public getEventManager() : EventManagerInterface

You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.

Return values
EventManagerInterface

getIterator()

Returns an array iterator.

public getIterator() : Traversable
Tags
psalm-return

\Traversable<string, TObject>

Return values
Traversable

has()

Check whether a given object is loaded.

public has(string $name) : bool
Parameters
$name : string

The object name to check for.

Return values
bool

True is object is loaded else false.

hasFinder()

Check if any loaded behavior implements the named finder.

public hasFinder(string $method) : bool

Will return true if any behavior provides a public method with the chosen name.

Parameters
$method : string

The method to check for.

Return values
bool

hasMethod()

Check if any loaded behavior implements a method.

public hasMethod(string $method) : bool

Will return true if any behavior provides a public non-finder method with the chosen name.

Parameters
$method : string

The method to check for.

Return values
bool

load()

Loads/constructs an object instance.

public load(string $name[, array<string, mixed> $config = [] ]) : mixed

Will return the instance in the registry if it already exists. If a subclass provides event support, you can use $config['enabled'] = false to exclude constructed objects from being registered for events.

Using as an example. You can alias an object by setting the 'className' key, i.e.,

protected $components = [
  'Email' => [
    'className' => 'App\Controller\Component\AliasedEmailComponent'
  ];
];

All calls to the Email component would use AliasedEmail instead.

Parameters
$name : string

The name/class of the object to load.

$config : array<string, mixed> = []

Additional settings to use when loading the object.

Tags
psalm-return

TObject

throws
Exception

If the class cannot be found.

loaded()

Get the list of loaded objects.

public loaded() : array<string|int, string>
Return values
array<string|int, string>

List of object names.

normalizeArray()

Normalizes an object array, creates an array that makes lazy loading easier

public normalizeArray(array<string|int, mixed> $objects) : array<string, array<string|int, mixed>>
Parameters
$objects : array<string|int, mixed>

Array of child objects to normalize.

Return values
array<string, array<string|int, mixed>>

Array of normalized objects.

reset()

Clear loaded instances in the registry.

public reset() : $this

If the registry subclass has an event manager, the objects will be detached from events as well.

Return values
$this

set()

Set an object directly into the registry by name.

public set(string $name, object $object) : $this

If this collection implements events, the passed object will be attached into the event manager

Parameters
$name : string

The name of the object to set in the registry.

$object : object

instance to store in the registry

Tags
psalm-param

TObject $object

psalm-suppress

MoreSpecificReturnType

Return values
$this

setEventManager()

Returns the Cake\Event\EventManagerInterface instance for this object.

public setEventManager(EventManagerInterface $eventManager) : $this

You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.

Parameters
$eventManager : EventManagerInterface

the eventManager to set

Return values
$this

setTable()

Attaches a table instance to this registry.

public setTable(Table $table) : void
Parameters
$table : Table

The table this registry is attached to.

unload()

Remove an object from the registry.

public unload(string $name) : $this

If this registry has an event manager, the object will be detached from any events as well.

Parameters
$name : string

The name of the object to remove from the registry.

Tags
psalm-suppress

MoreSpecificReturnType

Return values
$this

_checkDuplicate()

Check for duplicate object loading.

protected _checkDuplicate(string $name, array<string, mixed> $config) : void

If a duplicate is being loaded and has different configuration, that is bad and an exception will be raised.

An exception is raised, as replacing the object will not update any references other objects may have. Additionally, simply updating the runtime configuration is not a good option as we may be missing important constructor logic dependent on the configuration.

Parameters
$name : string

The name of the alias in the registry.

$config : array<string, mixed>

The config data for the new instance.

Tags
throws
RuntimeException

When a duplicate is found.

_create()

Create the behavior instance.

protected _create(string $class, string $alias, array<string, mixed> $config) : Behavior

Part of the template method for Cake\Core\ObjectRegistry::load() Enabled behaviors will be registered with the event manager.

Parameters
$class : string

The classname that is missing.

$alias : string

The alias of the object.

$config : array<string, mixed>

An array of config to use for the behavior.

Tags
psalm-suppress

MoreSpecificImplementedParamType

Return values
Behavior

The constructed behavior class.

_getMethods()

Get the behavior methods and ensure there are no duplicates.

protected _getMethods(Behavior $instance, string $class, string $alias) : array<string|int, mixed>

Use the implementedEvents() method to exclude callback methods. Methods starting with _ will be ignored, as will methods declared on Cake\ORM\Behavior

Parameters
$instance : Behavior

The behavior to get methods from.

$class : string

The classname that is missing.

$alias : string

The alias of the object.

Tags
throws
LogicException

when duplicate methods are connected.

Return values
array<string|int, mixed>

A list of implemented finders and methods.

_resolveClassName()

Resolve a behavior classname.

protected _resolveClassName(string $class) : string|null

Part of the template method for Cake\Core\ObjectRegistry::load()

Parameters
$class : string

Partial classname to resolve.

Tags
psalm-return

class-string|null

Return values
string|null

Either the correct class name or null.

_throwMissingClassError()

Throws an exception when a behavior is missing.

protected _throwMissingClassError(string $class, string|null $plugin) : void

Part of the template method for Cake\Core\ObjectRegistry::load() and Cake\Core\ObjectRegistry::unload()

Parameters
$class : string

The classname that is missing.

$plugin : string|null

The plugin the behavior is missing in.

Tags
throws
MissingBehaviorException

        
On this page

Search results