Skip to content

Load Metadata lazily through Metadata::lazy() - #9

Open
loevgaard wants to merge 1 commit into
masterfrom
lazy-metadata
Open

loevgaard wants to merge 1 commit into
masterfrom
lazy-metadata

Conversation

@loevgaard

@loevgaard loevgaard commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Metadata::lazy($loader) creates metadata whose entries a loader provides on first use. hasChanged() never calls the loader, so a store can skip metadata that was never used without fetching it. This lets setono/client-bundle drop LazyGhostTrait, which Symfony deprecated in 7.3 and removed in 8.0 (Setono/client-bundle#31).

 Metadata
+  lazy(loader)                      # keeps the loader, doesn't call it
   has / get / set / remove / ArrayAccess / count / iteration / toArray / jsonSerialize
+    load()                          # first call only: read loader() like constructor input, then drop the loader
+                                    # loader throws → nothing changes, the next call tries again
     ...unchanged
   hasChanged()                      # never loads: unused lazy metadata hasn't changed
+  __serialize()                     # loads, then PHP's default property array minus the loader
   clone                             # an unloaded clone stays lazy and loads on its own

What it replaces in the bundle:

-return LazyMetadata::createLazyGhost(function (Metadata $instance) use ($clientId): void {
-    $instance->__construct($this->find($clientId));
-});
+return Metadata::lazy(fn (): array => $this->find($clientId));

__serialize() is used instead of the __sleep() the issue suggests. Released bundle versions make lazy ghosts with LazyGhostTrait, and its __serialize() raises an E_USER_NOTICE for each __sleep() name it can't resolve. set()/offsetSet() calls rejected for their arguments don't load.

Evidence

  • Before: Metadata::lazy() doesn't exist.
    Error: Call to undefined method Setono\Client\Metadata::lazy()
    
    After: 115 tests pass, including:
    lazy(loader) → hasChanged() is false, loader calls = 0
    each of the 12 read/alter methods → loader calls = 1, again → still 1
    lazy(fn => $data) gives the same results and hasChanged() as new Metadata($data), expired key included
    loader throws on set() → exception reaches caller, hasChanged() false, toArray() then loads
    unserialize(serialize(lazy)) → loaded data, loader calls = 1
    setono/client 1.2.0 serialized string → unserializes, and serialize() gives the same bytes
    
  • The 1.2.0 fixture test passes on both master and this branch, so eager metadata serializes as before.
  • Infection: no escaped mutants in Metadata.php. Only the 4 known Client.php ones remain.
  • PHPStan (max), ECS, and roave-backward-compatibility-check --from=origin/master ("No backwards-incompatible changes detected").
  • Checked against the real LazyGhostTrait (symfony/var-exporter 7.3) with the bundle's 2.x LazyMetadata and 1.x ChangeAwareMetadata/LazyChangeAwareMetadata in a scratch script, run on master and on this branch:
    • Before: ghost creation, clone, pruning through the overridden remove(), and serialization, with no notices.
      After: output identical to master byte for byte, with no notices. The same script with a __sleep() variant raised 4 E_USER_NOTICEs.

Merge Danger

Door: two-way

Revertable until 1.3.0 is tagged. After that, lazy() is public API the bundle will depend on.

Blast Radius: serialization

Every Metadata now has __serialize(). The bytes are unchanged, but a subclass that defines its own __sleep() will have it ignored, because the inherited __serialize() takes precedence. No released bundle class defines one. new Metadata($array) behaves as before.

Closes #8

Metadata::lazy($loader) returns metadata whose entries a loader provides
on first use. The first call that reads or alters it (has(), get(), set(),
remove(), array access, count(), iteration, toArray(), jsonSerialize())
calls the loader once, and the result is read exactly like constructor
input. Loading isn't a change, and hasChanged() never calls the loader, so
a store can skip metadata that was never used without fetching it. If the
loader throws, the metadata stays unloaded and the next call retries. A
call that set() or offsetSet() rejects for its arguments doesn't load.

Serializing lazy metadata loads it, then writes PHP's default format
without the loader, so metadata serialized with 1.2 still unserializes and
eager metadata serializes byte for byte as before. That's done in
__serialize() rather than __sleep(): released setono/client-bundle
versions make lazy ghosts with Symfony's LazyGhostTrait, whose
__serialize() raises notices for __sleep() names it can't resolve. As a
consequence, a __sleep() defined in a subclass is no longer used.

A clone of unloaded lazy metadata is lazy too and loads on its own.

This lets setono/client-bundle drop LazyGhostTrait, which Symfony
deprecated in 7.3 and removed in 8.0 (Setono/client-bundle#31).

Closes #8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Load Metadata lazily through Metadata::lazy()

1 participant