laminas/laminas-i18n

Custom event manager cannot be set at the factory

froschdesign opened this issue · 4 comments

Bug Report

Q A
Version(s) 2.10.2 (current, and lower)

Summary

The factory method of Laminas\I18n\Translator\Translator allows to enable the event manager but not to set a custom event manager.

Current behavior

Setting a custom manager event will be ignored.

How to reproduce

$eventManager = new Laminas\EventManager\EventManager();
$translator = Laminas\I18n\Translator\Translator::factory([
    'event_manager' => $eventManager,
]);

var_dump($eventManager === $translator->getEventManager()); // false

Expected behavior

Allow the set a custom event manager per factory.

@froschdesign I am not sure if it is a bug. It might be "feature request", but at the same time I am not sure if it is needed.

The factory is used to take "static" configuration from the files and we cannot really provide there an object for custom event manager. We can consider providing container key, so the custom event manager will be loaded from container - but again, I am not sure if it is needed.

If you want to set custom Event Manager you can:

  • create delegator and set the Event Manager there
  • create custom factory:
class MyCustomFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $config = $container->get('config')['translator']; // I do not know what is the actual key/if any defined

        $translator = Laminas\I18n\Translator\Translator::factory($config);

        $customEventManager = $container->get(...); // or create it however you want
        $translator->setEventManager($customEventManager);
        
        return $translator;
    }
}

etc.

I cannot see any advantage of adding it as an object to the configuration, as noted above.

@michalbundyra
My mistake. A custom factory or a delegator is the correct way. 👍

@froschdesign We might want to describe it in the documentation :)

@michalbundyra
Will be included in one of the description for application integration.

https://github.com/laminas/laminas-i18n/projects/1