Skip to content

Comments

Add Dynamic Subscriber Interface#36

Closed
Fedik wants to merge 3 commits intojoomla-framework:3.x-devfrom
Fedik:dynamic-subscr
Closed

Add Dynamic Subscriber Interface#36
Fedik wants to merge 3 commits intojoomla-framework:3.x-devfrom
Fedik:dynamic-subscr

Conversation

@Fedik
Copy link

@Fedik Fedik commented May 6, 2024

Summary of Changes

Current limitation of SubscriberInterface is that it does not allow to return listeners depend from runtime.
It has own pros and cons. However in CMS it is sometimes need to have a diffrenet listeners depend from the Application type.

I think DynamicSubscriberInterface will be a good for it.
The difference from SubscriberInterface is that it uses non-static method, which allows subscriber to configure listeners depending on runtime. Additionally, it supports callables.

Example it allows to use LazyServiceEventListener and protected callbacks,
which is not possible with SubscriberInterface :

public function getSubscribedEvents(): array
{
  $this->lazyListener = $this->lazyListener ?? new LazyServiceEventListener($container, $serviceId);
  $this->protectedListener = $this->protectedListener ?? function(Event $event) { ... };

  return [ 
    'onSomething' => $this->lazyListener,
    'onSomething2' => $this->protectedListener, 
  ];
}

The subscriber should implement only one of these interfaces, implementing both is not allowed (but we can discuss it of course).

Testing Instructions

Test is passes

Documentation Changes Required

New interfaces: DynamicSubscriberInterface and DispatcherDynamicSubscriberInterface.

@HLeithner
Copy link

I'm not a fan of this, at least in the cms I wouldn't like to have the overhead of dynamic loading on boot

@Fedik
Copy link
Author

Fedik commented May 7, 2024

It not replacement, but an optional addittion when you need some kind of "private listeners".

This approach is much better than existing registerListeners and joomla/joomla-cms#43431 and joomla/joomla-cms#39387. Because it keep the Subscribber decoupled from Dispatcher.

This also allows to removeSubscriber() and "unload" the plugin, unlike other 2 approaches which is kind of "one way".

@Fedik Fedik marked this pull request as draft May 12, 2024 09:07
@Fedik Fedik closed this May 12, 2024
@Fedik Fedik deleted the dynamic-subscr branch May 12, 2024 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants