1 |
efrain |
1 |
# Serializable Closure
|
|
|
2 |
|
|
|
3 |
<a href="https://github.com/laravel/serializable-closure/actions">
|
|
|
4 |
<img src="https://github.com/laravel/serializable-closure/workflows/tests/badge.svg" alt="Build Status">
|
|
|
5 |
</a>
|
|
|
6 |
<a href="https://packagist.org/packages/laravel/serializable-closure">
|
|
|
7 |
<img src="https://img.shields.io/packagist/dt/laravel/serializable-closure" alt="Total Downloads">
|
|
|
8 |
</a>
|
|
|
9 |
<a href="https://packagist.org/packages/laravel/serializable-closure">
|
|
|
10 |
<img src="https://img.shields.io/packagist/v/laravel/serializable-closure" alt="Latest Stable Version">
|
|
|
11 |
</a>
|
|
|
12 |
<a href="https://packagist.org/packages/laravel/serializable-closure">
|
|
|
13 |
<img src="https://img.shields.io/packagist/l/laravel/serializable-closure" alt="License">
|
|
|
14 |
</a>
|
|
|
15 |
|
|
|
16 |
## Introduction
|
|
|
17 |
|
|
|
18 |
> This project is a fork of the excellent [opis/closure: 3.x](https://github.com/opis/closure) package. At Laravel, we decided to fork this package as the upcoming version [4.x](https://github.com/opis/closure) is a complete rewrite on top of the [FFI extension](https://www.php.net/manual/en/book.ffi.php). As Laravel is a web framework, and FFI is not enabled by default in web requests, this fork allows us to keep using the `3.x` series while adding support for new PHP versions.
|
|
|
19 |
|
|
|
20 |
Laravel Serializable Closure provides an easy and secure way to **serialize closures in PHP**.
|
|
|
21 |
|
|
|
22 |
## Official Documentation
|
|
|
23 |
|
|
|
24 |
### Installation
|
|
|
25 |
|
|
|
26 |
> **Requires [PHP 7.4+](https://php.net/releases/)**
|
|
|
27 |
|
|
|
28 |
First, install Laravel Serializable Closure via the [Composer](https://getcomposer.org/) package manager:
|
|
|
29 |
|
|
|
30 |
```bash
|
|
|
31 |
composer require laravel/serializable-closure
|
|
|
32 |
```
|
|
|
33 |
|
|
|
34 |
### Usage
|
|
|
35 |
|
|
|
36 |
You may serialize a closure this way:
|
|
|
37 |
|
|
|
38 |
```php
|
|
|
39 |
use Laravel\SerializableClosure\SerializableClosure;
|
|
|
40 |
|
|
|
41 |
$closure = fn () => 'james';
|
|
|
42 |
|
|
|
43 |
// Recommended
|
|
|
44 |
SerializableClosure::setSecretKey('secret');
|
|
|
45 |
|
|
|
46 |
$serialized = serialize(new SerializableClosure($closure));
|
|
|
47 |
$closure = unserialize($serialized)->getClosure();
|
|
|
48 |
|
|
|
49 |
echo $closure(); // james;
|
|
|
50 |
```
|
|
|
51 |
|
|
|
52 |
### Caveats
|
|
|
53 |
|
|
|
54 |
* Anonymous classes cannot be created within closures.
|
|
|
55 |
* Attributes cannot be used within closures.
|
|
|
56 |
* Serializing closures on REPL environments like Laravel Tinker is not supported.
|
|
|
57 |
* Serializing closures that reference objects with readonly properties is not supported.
|
|
|
58 |
|
|
|
59 |
## Contributing
|
|
|
60 |
|
|
|
61 |
Thank you for considering contributing to Serializable Closure! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
|
|
|
62 |
|
|
|
63 |
## Code of Conduct
|
|
|
64 |
|
|
|
65 |
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
|
|
|
66 |
|
|
|
67 |
## Security Vulnerabilities
|
|
|
68 |
|
|
|
69 |
Please review [our security policy](https://github.com/laravel/serializable-closure/security/policy) on how to report security vulnerabilities.
|
|
|
70 |
|
|
|
71 |
## License
|
|
|
72 |
|
|
|
73 |
Serializable Closure is open-sourced software licensed under the [MIT license](LICENSE.md).
|