KnpLabs / KnpMenu

Menu Library for PHP

Home Page:https://knplabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow factory extensions to declare supported items

emodric opened this issue · comments

Hi,

Would it be possible to add a feature to factory item extensions that allows them to specify on which items they operate? Something like public function matches(array $options = array()); that would return true or false based on the state of $options argument?

I have three factories that act on a object of the same class passed to the menu item factory, but all of them are mutualy exclusive and their execution depends on the internal state of the object. In order not to repeat the logic to skip the extension in buildOptions and buildItem, it would be awesome to have a third method specifying if extension can be used on an item, which would then the factory read and act upon.

In this scenario, in RoutingExtension for example, check for existence if (!empty($options['route'])) would move to the new method.

I'm willing to create a PR if this is something you deem acceptable!

Thanks for listening!

Well, this would not change things much, because the MenuFactory would still be calling matches twice. And it would make things worse for cases where one of the methods is currently a no-op (like the RoutingExtension) as it would re-run the logic.

If you don't want to duplicate the logic (in case it is complex), you could use a private method to share the same logic between buildOptions and buildItem.

Thus, the condition to skip the logic in each method may even not be the same for all extensions (as you could also pass some options explicitly which correspond to the output of buildOptions and still expect the item building to act on them).

Can you give more details about your use case ?

I agree with you, it's not really useful here. My usecase was a bit specific and a bit convoluted with using eZ Platform, but I solved it by implementing my own Knp\Menu\FactoryInterface with its own set of extensions and rules, thus keeping the original factory clean.

I will close the issue, but thanks for your reply and insight!