CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
      • Attribute
      • Decorator
      • Exception
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

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');
Namespace: Cake\Event

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 with on. 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:

    1. For method-level attributes: the name of the method the attribute is placed on.
    2. For class-level attributes: the explicit method argument when provided.
    3. For class-level attributes: __invoke when present on the class.
    4. 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
void

connectListenerClass() ¶ 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
void
Throws
Cake\Event\Exception\EventAttributeException
When 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
object

Listener 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
string

Inferred 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\EventManagerInterface
Throws
Cake\Event\Exception\EventAttributeException
When 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:

  1. For method-level attributes: the name of the method the attribute is placed on.
  2. For class-level attributes: the explicit method argument when provided.
  3. For class-level attributes: __invoke when present on the class.
  4. 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
string

Resolved method name.

Throws
Cake\Event\Exception\EventAttributeException
When 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\EventAttributeException
When a listener method cannot be resolved.
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs