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.

v1.1.0 2025-04-22 23:00 UTC

This package is auto-updated.

Last update: 2025-04-22 23:00:59 UTC


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

πŸš€ 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.