mjohann / call-morph
CallMorph is a PHP class that simplifies and secures the serialization of anonymous functions by automatically selecting the appropriate library based on the PHP version, with support for a secret key when using Laravel's Serializable Closures.
Requires
- php: ^7.0|^8.0
- laravel/serializable-closure: ^2.0
- opis/closure: ^3.6
README
CallMorph is a PHP class that provides a simple and secure interface for serializing and unserializing anonymous functions (Closures). It automatically detects the PHP version and uses the appropriate library for maximum compatibility and security, including support for a secret key when using Laravel's Serializable Closures.
π¦ Installation
You can install the library via Packagist/Composer:
composer require mjohann/call-morph
βοΈ Requirements
- PHP 7.0 or higher
- For PHP >= 8.1: laravel/serializable-closure must be installed.
- For PHP < 8.1: opis/closure is required.
π Features
- Serialize and unserialize Closures with automatic compatibility based on PHP version
- Supports secret key configuration for secure serialization in Laravel
- Simple and clean API
Available Methods
__construct(string $secret)
getSecret(): string
setSecret(string $secret): void
serialize(Closure $callback): string
unserialize(string $callback): Closure
π§ͺ Usage Example
Publisher
<?php use MJohann\Packlib\CallMorph; require_once "vendor/autoload.php"; $name = "Johann"; $closure = function () use ($name) { sleep(1); return "My name is " . $name . " - Rand: " . rand(0, 999); }; $cm = new CallMorph("secret"); $serialized = $cm->serialize($closure); echo "CallMorph->serialize", PHP_EOL; var_dump($serialized); echo PHP_EOL, PHP_EOL; echo "CallMorph->unserialize", PHP_EOL; $closure = $cm->unserialize($serialized); echo $closure();
For more examples, see the example/
file in the repository.
π Project Structure
call-morph/
βββ src/
β βββ CallMorph.php
β βββ Facades/
β βββ CallMorph.php
βββ example/
β βββ script.php
βββ composer.json
βββ .gitignore
βββ LICENSE
βββ README.md
π License
This project is licensed under the MIT License. See the LICENSE file for more information.
π¨βπ» Author
Developed by Matheus Johann AraΓΊjo β Pernambuco, Brazil.