laravel/serializable-closure
Securely serialize and unserialize PHP closures with Laravel’s maintained fork of opis/closure 3.x, updated for modern PHP without requiring FFI. Wrap closures in SerializableClosure, set a secret key, and safely persist or transport executable callbacks.
Bus::chain).serialize()/unserialize() in queues, cache, or sessions). Avoids FFI (Foreign Function Interface) dependencies, making it web-server-compatible by default.laravel/serializable-closure) with zero runtime configuration required for basic use. Secret key management is explicit but optional (recommended for security).opis/closure@3.x, ensuring stability for existing Laravel projects. No breaking changes since v2.0.0 (2023).Bus::chain).| Risk Area | Severity | Mitigation |
|---|---|---|
| Security | Medium | Requires setSecretKey() for signed closures (default: unsigned). |
| Performance Overhead | Low | Minimal runtime cost; serialization is optimized for closure-specific logic. |
| PHP Version Lock-in | Low | Supports PHP 7.4–8.5 (active development for 8.5+). |
| Closure Signature Collisions | Medium | Caveat: Closures on the same line with identical signatures may fail. |
| Laravel Version Dependency | Low | Explicitly supports Laravel 12/13; no hard dependencies on other packages. |
serialize() (with __serialize()/__unserialize()) suffice for simpler cases? If not, justify the package’s overhead.Illuminate\Bus\Queueable, Illuminate\Queue\Jobs\Job).Illuminate\Cache\Repository with serialized values).opis/closure (v3.x).serialize(closure) with SerializableClosure::serialize($closure).setSecretKey() if security is required.| Compatibility Factor | Status |
|---|---|
| Laravel Versions | ✅ 10+ (explicit support for 12/13; likely works on 9 with minor tweaks). |
| PHP Versions | ✅ 7.4–8.5 (active development for 8.5+). |
| Closure Features | ✅ First-class callables, attributes, enums, named args, virtual props. |
| Serialization Contexts | ✅ Queues, cache, sessions (avoids FFI for web compatibility). |
| Existing Code | ⚠️ May require refactoring for unsupported syntax (e.g., same-line closures). |
setSecretKey() vulnerabilities (though MIT license reduces risk).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid signature | Closure execution fails silently. | Use try-catch with SerializableClosure::getClosure() or enable unsigned mode. |
| Unsupported PHP syntax | Serialization throws RuntimeException. |
Refactor closures or downgrade PHP version. |
| Secret key leakage | Security vulnerability. | Rotate keys periodically; use environment variables. |
| Queue job deserialization failure | Job hangs or crashes. | Implement retry logic with exponential backoff. |
| Same-line closure collision | Random failures in production. | Enforce linter rules (e.g., PSR-12) to separate closures. |
How can I help you explore Laravel packages today?