Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Serializable Closure Laravel Package

laravel/serializable-closure

Securely serialize and unserialize PHP closures with Laravel’s fork of opis/closure 3.x, updated for modern PHP without requiring FFI. Wrap closures in SerializableClosure, set a secret key for signing, serialize safely, then restore with getClosure().

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Queue Job Serialization: Enable serialization of closures in Laravel queue jobs (e.g., Bus::chain or delayed jobs) to persist logic across processes without rebuilding the closure.
  • Event Listeners with Dynamic Logic: Store event listeners or observers with closures that reference dynamic context (e.g., user-specific rules) in a cache or database.
  • Middleware with Stateful Logic: Serialize middleware closures that rely on external state (e.g., rate-limiting logic tied to user sessions) for reuse across requests.
  • Caching Complex Logic: Cache closures containing business logic (e.g., pricing calculations, validation rules) to avoid recomputation.
  • Database Stored Procedures/Triggers: Store PHP closures as serialized data in a database to execute custom logic later (e.g., audit triggers, dynamic workflows).
  • Build vs. Buy: Buy—avoids reinventing serialization logic for closures, which is non-trivial in PHP due to scoping and variable capture.
  • Roadmap Alignment: Supports Laravel’s push toward serializable, portable logic (e.g., for serverless functions, distributed tasks, or edge computing).

When to Consider This Package

  • Use This Package If:

    • You need to serialize closures for storage/transmission (e.g., Redis, database, cache).
    • Your closures capture variables from their parent scope (e.g., class properties, function args).
    • You’re using Laravel queues, events, or caching and need to persist logic dynamically.
    • You require PHP 7.4+ compatibility with modern features (e.g., attributes, enums, PHP 8.5+ syntax).
    • You want to avoid security risks (e.g., arbitrary code execution) by using Laravel’s vetted fork of opis/closure.
  • Look Elsewhere If:

    • You’re using PHP < 7.4 (package requires PHP 7.4+).
    • Your closures are trivial (e.g., stateless lambdas like fn() => 42—use native serialize instead).
    • You need FFI-based serialization (this fork avoids FFI for web compatibility).
    • Your closures rely on REPL environments (e.g., Laravel Tinker)—this package explicitly disallows it.
    • You’re in a high-security environment where even MIT-licensed packages require deeper audit (though this is a minimal risk).

How to Pitch It (Stakeholders)

For Executives:

*"This package lets us store and reuse PHP logic dynamically—like saving a ‘rule engine’ or ‘user-specific workflow’ in a database or cache, then executing it later. It’s critical for:

  • Scaling async tasks (e.g., background jobs with custom logic).
  • Reducing compute costs by caching complex calculations.
  • Future-proofing our Laravel stack for serverless/distributed architectures. It’s a low-risk, high-reward dependency (MIT-licensed, maintained by Laravel, and battle-tested)."*

For Engineers:

*"This is a drop-in solution for serializing closures in Laravel, solving pain points like:

  • Queue jobs with dynamic logic (e.g., Bus::chain with closures that reference $user).
  • Caching stateful middleware (e.g., rate-limiters tied to user sessions).
  • Storing event listeners with context (e.g., Event::listen(fn($user) => ...)). Why this fork?
  • Avoids FFI (not enabled by default in web requests).
  • Supports PHP 8.5+ (e.g., constant expressions, attributes).
  • Security-hardened (Laravel’s audit + secret-key signing). Tradeoffs:
  • Not for REPL environments (e.g., Tinker).
  • Requires PHP 7.4+. Implementation:
// Serialize
$serialized = serialize(new SerializableClosure(fn($x) => $x * 2));
cache()->put('rule:double', $serialized);

// Deserialize
$closure = unserialize(cache()->get('rule:double'))->getClosure();
$closure(5); // 10
```*
**Ask**: Should we standardize this for all queue jobs/cached logic?"*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai