KnpLabs / KnpMenu

Menu Library for PHP

Home Page:https://knplabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MenuItem (tree) is not serializable

cjprinse opened this issue · comments

I want the menu to be serializable, because we use a large menu (200+ items) and we want to cache the result.

When serializing the menuItem (including children) an exception will thrown:
Serialization of 'Closure' is not allowed

This is due to the injected ItemFactory.

solution will be to implement Serializable interface on the menuItem and reinject the factory on deserialization

We cannot reinject the factory when using the PHP serialization API, as there is no way to hook in it externally (and we don't want to make the factory available in a global variable to be able to retrieve it from inside the unserialized MenuItem). So we cannot implement Serializable in a usable way.

My suggestion would be to use a more powerful serializer for that (one that allows hooking from the outside), like symfony/serializer or jms/serializer (and both of them would allow you to use the library without modifying it btw).

Ok, I understand. But why would you like the the have the factory injected in the menuItem anyway? menuItem should be a popo right?

well, currently, we have methods to build childs on the MenuItem itself, to build the tree. This is why we need the factory.

Can we close this?

Yes, fine to close