diff --git a/libraries/src/Plugin/PluginHelper.php b/libraries/src/Plugin/PluginHelper.php index 3a972461a7f59..c7a1a1939fed1 100644 --- a/libraries/src/Plugin/PluginHelper.php +++ b/libraries/src/Plugin/PluginHelper.php @@ -13,6 +13,7 @@ use Joomla\CMS\Factory; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; +use Joomla\Event\SubscriberInterface; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; @@ -231,16 +232,26 @@ protected static function import($plugin, $autocreate = true, ?DispatcherInterfa $plugin = Factory::getApplication()->bootPlugin($plugin->name, $plugin->type); - if ($dispatcher && $plugin instanceof DispatcherAwareInterface) { - $plugin->setDispatcher($dispatcher); - } - if (!$autocreate) { return; } - // @TODO: Starting from 7.0 it should use $dispatcher->addSubscriber($plugin); for plugins which implement SubscriberInterface. - $plugin->registerListeners(); + // Check for overridden registerListeners() + $reflection = new \ReflectionClass($plugin); + $registerOverridden = $reflection->hasMethod('registerListeners') && $reflection->getMethod('registerListeners')->class !== CMSPlugin::class; + + // @TODO: From 7.0 when registerListeners() will be removed from CMSPlugin checking for overridden registerListeners() need to be removed. + if ($plugin instanceof SubscriberInterface && !$registerOverridden) { + $dispatcher->addSubscriber($plugin); + } else { + // @TODO: From 7.0 when DispatcherAwareInterface will be removed from CMSPlugin this should be checked for all plugins. + if ($dispatcher && $plugin instanceof DispatcherAwareInterface) { + $plugin->setDispatcher($dispatcher); + } + + // @TODO: From 7.0 it should use $dispatcher->addSubscriber($plugin); for plugins which implement SubscriberInterface. + $plugin->registerListeners(); + } } /** diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 12a1229520abc..7cb6db4c89ac3 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -14274,6 +14274,16 @@ parameters: count: 4 path: plugins/system/cache/src/Extension/Cache.php + - + message: ''' + #^Call to deprecated method setDispatcher\(\) of class Joomla\\Plugin\\System\\Cache\\Extension\\Cache\: + 5\.2 will be removed in 7\.0 + Plugin should implement DispatcherAwareInterface on its own, when it is needed\.$# + ''' + identifier: method.deprecated + count: 1 + path: plugins/system/cache/src/Extension/Cache.php + - message: ''' #^Call to deprecated method getLanguage\(\) of class Joomla\\CMS\\Factory\: diff --git a/plugins/system/schedulerunner/services/provider.php b/plugins/system/schedulerunner/services/provider.php index 446cf366e4511..aeec8cb805f19 100644 --- a/plugins/system/schedulerunner/services/provider.php +++ b/plugins/system/schedulerunner/services/provider.php @@ -31,14 +31,14 @@ public function register(Container $container): void { $container->set( PluginInterface::class, - function (Container $container) { - $plugin = new ScheduleRunner( + $container->lazy(ScheduleRunner::class, function (Container $container) { + $plugin = new ScheduleRunner( (array) PluginHelper::getPlugin('system', 'schedulerunner') ); $plugin->setApplication(Factory::getApplication()); return $plugin; - } + }) ); } }; diff --git a/plugins/system/tasknotification/services/provider.php b/plugins/system/tasknotification/services/provider.php index 2f5d54b0f8fb3..e7fd317ab1510 100644 --- a/plugins/system/tasknotification/services/provider.php +++ b/plugins/system/tasknotification/services/provider.php @@ -33,8 +33,8 @@ public function register(Container $container): void { $container->set( PluginInterface::class, - function (Container $container) { - $plugin = new TaskNotification( + $container->lazy(TaskNotification::class, function (Container $container) { + $plugin = new TaskNotification( (array) PluginHelper::getPlugin('system', 'tasknotification') ); $plugin->setApplication(Factory::getApplication()); @@ -42,7 +42,7 @@ function (Container $container) { $plugin->setUserFactory($container->get(UserFactoryInterface::class)); return $plugin; - } + }) ); } }; diff --git a/plugins/system/webauthn/services/provider.php b/plugins/system/webauthn/services/provider.php index bd65c9527b7d1..f6895862f48cd 100644 --- a/plugins/system/webauthn/services/provider.php +++ b/plugins/system/webauthn/services/provider.php @@ -40,7 +40,7 @@ public function register(Container $container) { $container->set( PluginInterface::class, - function (Container $container) { + $container->lazy(Webauthn::class, function (Container $container) { $app = Factory::getApplication(); $config = (array) PluginHelper::getPlugin('system', 'webauthn'); $session = $container->has('session') ? $container->get('session') : $this->getSession($app); @@ -54,12 +54,12 @@ function (Container $container) { $params = new Registry($config['params'] ?? '{}'); if ($params->get('attestationSupport', 0) == 1) { - $metadataRepository = $container->has(MetadataStatementRepository::class) + $metadataRepository = $container->has(MetadataStatementRepository::class) ? $container->get(MetadataStatementRepository::class) : new MetadataRepository(); } - $authenticationHelper = $container->has(Authentication::class) + $authenticationHelper = $container->has(Authentication::class) ? $container->get(Authentication::class) : new Authentication($app, $session, $credentialsRepository, $metadataRepository); @@ -70,7 +70,7 @@ function (Container $container) { $plugin->setApplication($app); return $plugin; - } + }) ); }