Skip to content

Commit d054000

Browse files
committed
Updated Composer dependencies and made the SerializableClosure class easier to extend. Fixes #9
1 parent db047d8 commit d054000

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ provide closures (or algorithms) as a service through an API.
105105

106106
## Who Is Using Super Closure?
107107

108-
- [Laravel 4](https://github.com/laravel/framework) - Serializes a closure to potentially push onto a job queue
108+
- [Laravel 4](https://github.com/laravel/framework) - Serializes a closure to potentially push onto a job queue.
109109
- [HTTP Mock for PHP](https://github.com/InterNations/http-mock) - Serialize a closure to send to remote server within
110-
a test workflow
111-
- [Jumper](https://github.com/kakawait/Jumper) - Serialize a closure to run on remote host via SSH
110+
a test workflow.
111+
- [Jumper](https://github.com/kakawait/Jumper) - Serialize a closure to run on remote host via SSH.
112+
- [nicmart/Benchmark](https://github.com/nicmart/Benchmark) - Uses the `ClosureParser` to display a benchmarked
113+
Closure's code.
112114
- Please let me know if and how your project uses Super Closure.
113115

114116
[1]: https://secure.travis-ci.org/jeremeamia/super_closure.png?branch=master

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jeremeamia/SuperClosure",
33
"type": "library",
4-
"description": "Doing interesting things with closures like serialization and partial function application.",
4+
"description": "Doing interesting things with closures like serialization.",
55
"keywords": ["closure", "serialize", "serializable", "function", "parser", "tokenizer"],
66
"homepage": "https://github.com/jeremeamia/super_closure",
77
"license": "MIT",
@@ -12,10 +12,10 @@
1212
],
1313
"require": {
1414
"php": ">=5.3.3",
15-
"nikic/php-parser": "dev-master"
15+
"nikic/php-parser": "~0.9"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "3.7.*"
18+
"phpunit/phpunit": "~3.7"
1919
},
2020
"autoload": {
2121
"psr-0": { "Jeremeamia\\SuperClosure": "src/" }

src/Jeremeamia/SuperClosure/SerializableClosure.php

+16-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SerializableClosure implements \Serializable
2424
/**
2525
* @var array The calculated state to serialize
2626
*/
27-
private $state;
27+
protected $state;
2828

2929
/**
3030
* @param \Closure $closure
@@ -65,20 +65,14 @@ public function __invoke()
6565
}
6666

6767
/**
68-
* Uses the closure parser to fetch the closure's code. The code and the closure's context are serialized
68+
* Serialize the code and of context of the closure
6969
*
7070
* @return string
7171
*/
7272
public function serialize()
7373
{
74-
// Prepare the state to serialize using a ClosureParser
7574
if (!$this->state) {
76-
$parser = new ClosureParser($this->getReflection());
77-
$this->state = array($parser->getCode());
78-
// Add the used variables (context) to the state, but wrap all closures with SerializableClosure
79-
$this->state[] = array_map(function ($var) {
80-
return ($var instanceof \Closure) ? new self($var) : $var;
81-
}, $parser->getUsedVariables());
75+
$this->createState();
8276
}
8377

8478
return serialize($this->state);
@@ -104,4 +98,17 @@ public function unserialize($__serialized__)
10498
// Evaluate the code to recreate the Closure
10599
eval("\$this->closure = {$__code__};");
106100
}
101+
102+
/**
103+
* Uses the closure parser to fetch the closure's code and context
104+
*/
105+
protected function createState()
106+
{
107+
$parser = new ClosureParser($this->getReflection());
108+
$this->state = array($parser->getCode());
109+
// Add the used variables (context) to the state, but wrap all closures with SerializableClosure
110+
$this->state[] = array_map(function ($var) {
111+
return ($var instanceof \Closure) ? new self($var) : $var;
112+
}, $parser->getUsedVariables());
113+
}
107114
}

0 commit comments

Comments
 (0)