backdrop-contrib / rules_cmi

Stores Rules configurations in json config files for Backdrop CMS
GNU General Public License v2.0
0 stars 1 forks source link

How easy would it be to store "data" as JSON as well and not serialized? #26

Open herbdool opened 3 months ago

herbdool commented 3 months ago

It seems odd to have the main data still stored as serialized which includes the \u0000 control character.

{
    "_config_name": "rules_config.uc_payment_method_free_order",
    "status": 2,
    "name": "uc_payment_method_free_order",
    "module": "uc_payment",
    "id": "uc_payment_method_free_order",
    "label": "Free order conditions",
    "plugin": "and",
    "active": 1,
    "weight": 0,
    "dirty": 0,
    "owner": "rules",
    "access_exposed": 0,
    "dependencies": [],
    "tags": [],
    "data": "O:8:\"RulesAnd\":19:{s:9:\"\u0000*\u0000parent\";N;s:2:\"id\";N;s:12:\"\u0000*\u0000elementId\";N;s:6:\"weight\";i:0;s:8:\"settings\";a:0:{}s:4:\"name\";s:28:\"uc_payment_method_free_order\";s:5:\"label\";s:21:\"Free order conditions\";s:6:\"plugin\";s:3:\"and\";s:6:\"active\";i:1;s:6:\"status\";i:2;s:5:\"dirty\";i:0;s:6:\"module\";s:10:\"uc_payment\";s:5:\"owner\";s:5:\"rules\";s:14:\"access_exposed\";i:0;s:4:\"data\";r:1;s:4:\"tags\";a:0:{}s:11:\"\u0000*\u0000children\";a:0:{}s:7:\"\u0000*\u0000info\";a:1:{s:9:\"variables\";a:1:{s:5:\"order\";a:2:{s:4:\"type\";s:8:\"uc_order\";s:5:\"label\";s:5:\"Order\";}}}s:9:\"\u0000*\u0000negate\";b:0;}"
}
argiepiano commented 3 months ago

Yes, this was brought up in a PM with the Rules maintainers. Rules stores the plugin object as serialized string in database exactly as you see it. Changing this would require a lot more tweaking at the Rules plugin class level (not the entity controller).

There are two issues with making this into a json encoded string:

  1. These are objects. Unserialize automagically creates the object. The class name of the object is lost when encoding as json, unless you create a special property to store it
  2. Serialize uses the __sleep() method for all these classes, pruning unwanted properties. Reproducing this when encoding as json requires extra coding

So, switching to json encoded strings should ideally be done both with the "normal" database storage, and the CMI storage. Otherwise we'll end up with a lot of extended plugin classes that act differently depending on the storage location.

If you can think of an easier way, I'm all ears!

herbdool commented 3 months ago

I cannot think of an easier way. The Internet seems to know of ways to do it but from what I saw it didn't seem as easy as this.