Fr3nch13/CakePHP Utilities

Component
in package
implements EventListenerInterface uses InstanceConfigTrait, LogTrait

Base class for an individual Component. Components provide reusable bits of controller logic that can be composed into a controller. Components also provide request life-cycle callbacks for injecting logic at specific points.

Initialize hook

Like Controller and Table, this class has an initialize() hook that you can use to add custom 'constructor' logic. It is important to remember that each request (and sub-request) will only make one instance of any given component.

Life cycle callbacks

Components can provide several callbacks that are fired at various stages of the request cycle. The available callbacks are:

  • beforeFilter(EventInterface $event) Called before Controller::beforeFilter() method by default.
  • startup(EventInterface $event) Called after Controller::beforeFilter() method, and before the controller action is called.
  • beforeRender(EventInterface $event) Called before Controller::beforeRender(), and before the view class is loaded.
  • afterFilter(EventInterface $event) Called after the action is complete and the view has been rendered but before Controller::afterFilter().
  • beforeRedirect(EventInterface $event $url, Response $response) Called before a redirect is done. Allows you to change the URL that will be redirected to by returning a Response instance with new URL set using Response::location(). Redirection can be prevented by stopping the event propagation.

While the controller is not an explicit argument for the callback methods it is the subject of each event and can be fetched using EventInterface::getSubject().

Tags
link
https://book.cakephp.org/4/en/controllers/components.html
see
Controller::$components
Attributes
#[AllowDynamicProperties]

Table of Contents

Interfaces

EventListenerInterface
Objects implementing this interface should declare the `implementedEvents()` method to notify the event manager what methods should be called when an event is triggered.

Properties

$_componentMap  : array<string, array<string|int, mixed>>
A component lookup table used to lazy load component objects.
$_config  : array<string, mixed>
Runtime config
$_configInitialized  : bool
Whether the config property has already been configured with defaults
$_defaultConfig  : array<string, mixed>
Default config
$_registry  : ComponentRegistry
Component registry class used to lazy load components.
$components  : array<string|int, mixed>
Other Components this component uses.

Methods

__construct()  : mixed
Constructor
__debugInfo()  : array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
__get()  : Component|null
Magic method for lazy loading $components.
configShallow()  : $this
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
getConfig()  : mixed
Returns the config.
getConfigOrFail()  : mixed
Returns the config for this specific key.
getController()  : Controller
Get the controller this component is bound to.
implementedEvents()  : array<string, mixed>
Get the Controller callbacks this Component is interested in.
initialize()  : void
Constructor hook method.
log()  : bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
setConfig()  : $this
Sets the config.
_configDelete()  : void
Deletes a single config key.
_configRead()  : mixed
Reads a config key.
_configWrite()  : void
Writes a config key.

Properties

$_componentMap

A component lookup table used to lazy load component objects.

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

$_configInitialized

Whether the config property has already been configured with defaults

protected bool $_configInitialized = false

$_defaultConfig

Default config

protected array<string, mixed> $_defaultConfig = []

These are merged with user-provided config when the component is used.

$components

Other Components this component uses.

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

Methods

__construct()

Constructor

public __construct(ComponentRegistry $registry[, array<string, mixed> $config = [] ]) : mixed
Parameters
$registry : ComponentRegistry

A component registry this component can use to lazy load its components.

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

Array of configuration settings.

__debugInfo()

Returns an array that can be used to describe the internal state of this object.

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

__get()

Magic method for lazy loading $components.

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

Name of component to get.

Return values
Component|null

A Component object or null.

configShallow()

Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.

public configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this

Setting a specific value:

$this->configShallow('key', $value);

Setting a nested value:

$this->configShallow('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

Return values
$this

getConfig()

Returns the config.

public getConfig([string|null $key = null ][, mixed $default = null ]) : mixed

Usage

Reading the whole config:

$this->getConfig();

Reading a specific value:

$this->getConfig('key');

Reading a nested value:

$this->getConfig('some.nested.key');

Reading with default value:

$this->getConfig('some-key', 'default-value');
Parameters
$key : string|null = null

The key to get or null for the whole config.

$default : mixed = null

The return value when the key does not exist.

Return values
mixed

Configuration data at the named key or null if the key does not exist.

getConfigOrFail()

Returns the config for this specific key.

public getConfigOrFail(string $key) : mixed

The config value for this key must exist, it can never be null.

Parameters
$key : string

The key to get.

Tags
throws
InvalidArgumentException
Return values
mixed

Configuration data at the named key

getController()

Get the controller this component is bound to.

public getController() : Controller
Return values
Controller

The bound controller.

implementedEvents()

Get the Controller callbacks this Component is interested in.

public implementedEvents() : array<string, mixed>

Uses Conventions to map controller events to standard component callback method names. By defining one of the callback methods a component is assumed to be interested in the related event.

Override this method if you need to add non-conventional event listeners. Or if you want components to listen to non-standard events.

Return values
array<string, mixed>

initialize()

Constructor hook method.

public initialize(array<string, mixed> $config) : void

Implement this method to avoid having to overwrite the constructor and call parent.

Parameters
$config : array<string, mixed>

The configuration settings provided to this component.

log()

Convenience method to write a message to Log. See Log::write() for more information on writing to logs.

public log(string $message[, string|int $level = LogLevel::ERROR ][, array<string|int, mixed>|string $context = [] ]) : bool
Parameters
$message : string

Log message.

$level : string|int = LogLevel::ERROR

Error level.

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

Additional log data relevant to this message.

Return values
bool

Success of log write.

setConfig()

Sets the config.

public setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this

Usage

Setting a specific value:

$this->setConfig('key', $value);

Setting a nested value:

$this->setConfig('some.nested.key', $value);

Updating multiple config settings at the same time:

$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
$key : array<string, mixed>|string

The key to set, or a complete array of configs.

$value : mixed|null = null

The value to set.

$merge : bool = true

Whether to recursively merge or overwrite existing config, defaults to true.

Tags
throws
CakeException

When trying to set a key that is invalid.

Return values
$this

_configDelete()

Deletes a single config key.

protected _configDelete(string $key) : void
Parameters
$key : string

Key to delete.

Tags
throws
CakeException

if attempting to clobber existing config

_configRead()

Reads a config key.

protected _configRead(string|null $key) : mixed
Parameters
$key : string|null

Key to read.

_configWrite()

Writes a config key.

protected _configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
$key : array<string, mixed>|string

Key to write to.

$value : mixed

Value to write.

$merge : string|bool = false

True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.

Tags
throws
CakeException

if attempting to clobber existing config


        
On this page

Search results