Class AttributeEventListenerConnector
Connects event listeners declared via PHP attributes to an event manager.
Reads event listener attribute metadata from the configured AttributeResolver collection and registers the resolved callables on the provided EventManagerInterface.
Usage within Application::events():
public function events(EventManagerInterface $eventManager): EventManagerInterface
{
new AttributeEventListenerConnector(
$eventManager,
$this->getContainer()->get(...),
)->connect();
return $eventManager;
}
Or using the connector directly with a custom config:
(new AttributeEventListenerConnector($eventManager))->connect('myConfig');
Method Summary
-
__construct() public
Constructs an AttributeEventListenerConnector.
-
connect() public
Resolves all EventListener attributes from the configured resolver collection and registers the discovered listeners on the event manager.
Event listeners are connected by class name, then declaration line number. When listeners have the same priority, this determines their invocation order.
-
connectListenerClass() protected
Connects all EventListener attributes declared on a single class.
Skips the class silently when it cannot be instantiated (e.g. abstract classes, interfaces, or traits). Throws EventAttributeException for invalid declarations on concrete classes (missing methods, non-public methods).
-
createListener() protected
Resolves a listener class to an instance.
Uses the configured listener resolver. By default, listener classes are instantiated directly.
-
inferMethodName() protected
Infers a method name from an event name.
Segments split on
.are each uppercased and prefixed withon. For example:Order.afterPlace→onOrderAfterPlace. -
resolveEventManager() protected
Resolves the event manager that a listener registration should be attached to.
-
resolveMethodName() protected
Resolves the listener method name for the given attribute and target.
Resolution order:
- For method-level attributes: the name of the method the attribute is placed on.
- For class-level attributes: the explicit
methodargument when provided. - For class-level attributes:
__invokewhen present on the class. - For class-level attributes: a name inferred from the event name.
-
resolveRegistrations() protected
Resolves the listener registrations for a class.
Returns a deduplicated list of registrations ordered by line number.
Method Detail
__construct() ¶ public
__construct(Cake\Event\EventManagerInterface $eventManager, callable|null $listenerResolver = null, callable|null $managerResolver = null)
Constructs an AttributeEventListenerConnector.
Parameters
-
Cake\Event\EventManagerInterface$eventManager Event manager to attach listeners to.
-
callable|null$listenerResolver optional Resolver used to create listener instances.
-
callable|null$managerResolver optional Resolver used to resolve named event managers.
connect() ¶ public
connect(string $config = 'default'): void
Resolves all EventListener attributes from the configured resolver collection and registers the discovered listeners on the event manager.
Event listeners are connected by class name, then declaration line number. When listeners have the same priority, this determines their invocation order.
Parameters
-
string$config optional Attribute resolver config name.
Returns
voidconnectListenerClass() ¶ protected
connectListenerClass(string $className, list<Cake\AttributeResolver\ValueObject\AttributeInfo> $infos): void
Connects all EventListener attributes declared on a single class.
Skips the class silently when it cannot be instantiated (e.g. abstract classes, interfaces, or traits). Throws EventAttributeException for invalid declarations on concrete classes (missing methods, non-public methods).
Parameters
-
string$className Fully qualified class name to process.
-
list<Cake\AttributeResolver\ValueObject\AttributeInfo>$infos Attribute metadata sorted by line number.
Returns
voidThrows
Cake\Event\Exception\EventAttributeExceptionWhen a listener method cannot be resolved.
createListener() ¶ protected
createListener(string $className): object
Resolves a listener class to an instance.
Uses the configured listener resolver. By default, listener classes are instantiated directly.
Parameters
-
string$className Fully qualified class name to resolve.
Returns
objectListener instance.
inferMethodName() ¶ protected
inferMethodName(string $eventName): string
Infers a method name from an event name.
Segments split on . are each uppercased and prefixed with on.
For example: Order.afterPlace → onOrderAfterPlace.
Parameters
-
string$eventName Event name.
Returns
stringInferred method name.
resolveEventManager() ¶ protected
resolveEventManager(array{event: string, filePath: string, lineNumber: int, manager: (string|null, method: string, priority: (int|null)} $registration): Cake\Event\EventManagerInterface
Resolves the event manager that a listener registration should be attached to.
Parameters
-
array{event: string, filePath: string, lineNumber: int, manager: (string|null, method: string, priority: (int|null)}$registration Listener registration metadata.
Returns
Cake\Event\EventManagerInterfaceThrows
Cake\Event\Exception\EventAttributeExceptionWhen the named manager cannot be resolved.
resolveMethodName() ¶ protected
resolveMethodName(Cake\AttributeResolver\ValueObject\AttributeInfo $info, Cake\Event\Attribute\EventListener $attribute, string $className): string
Resolves the listener method name for the given attribute and target.
Resolution order:
- For method-level attributes: the name of the method the attribute is placed on.
- For class-level attributes: the explicit
methodargument when provided. - For class-level attributes:
__invokewhen present on the class. - For class-level attributes: a name inferred from the event name.
Parameters
-
Cake\AttributeResolver\ValueObject\AttributeInfo$info Attribute metadata.
-
Cake\Event\Attribute\EventListener$attribute Instantiated attribute.
-
string$className Fully qualified class name.
Returns
stringResolved method name.
Throws
Cake\Event\Exception\EventAttributeExceptionWhen the resolved method does not exist or is not public.
resolveRegistrations() ¶ protected
resolveRegistrations(string $className, list<Cake\AttributeResolver\ValueObject\AttributeInfo> $infos): list<array{event: string, filePath: string, lineNumber: int, manager: (string|null, method: string, priority: (int|null)}>
Resolves the listener registrations for a class.
Returns a deduplicated list of registrations ordered by line number.
Parameters
-
string$className Fully qualified class name.
-
list<Cake\AttributeResolver\ValueObject\AttributeInfo>$infos Attribute metadata sorted by line number.
Returns
list<array{event: string, filePath: string, lineNumber: int, manager: (string|null, method: string, priority: (int|null)}>Throws
Cake\Event\Exception\EventAttributeExceptionWhen a listener method cannot be resolved.