Skip to content

Commit

Permalink
Updated Composer dependencies and made the SerializableClosure class …
Browse files Browse the repository at this point in the history
…easier to extend. Fixes #9
  • Loading branch information
jeremeamia committed Oct 9, 2013
1 parent db047d8 commit d054000
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -105,10 +105,12 @@ provide closures (or algorithms) as a service through an API.

## Who Is Using Super Closure?

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

[1]: https://secure.travis-ci.org/jeremeamia/super_closure.png?branch=master
Expand Down
6 changes: 3 additions & 3 deletions composer.json
@@ -1,7 +1,7 @@
{
"name": "jeremeamia/SuperClosure",
"type": "library",
"description": "Doing interesting things with closures like serialization and partial function application.",
"description": "Doing interesting things with closures like serialization.",
"keywords": ["closure", "serialize", "serializable", "function", "parser", "tokenizer"],
"homepage": "https://github.com/jeremeamia/super_closure",
"license": "MIT",
Expand All @@ -12,10 +12,10 @@
],
"require": {
"php": ">=5.3.3",
"nikic/php-parser": "dev-master"
"nikic/php-parser": "~0.9"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "~3.7"
},
"autoload": {
"psr-0": { "Jeremeamia\\SuperClosure": "src/" }
Expand Down
25 changes: 16 additions & 9 deletions src/Jeremeamia/SuperClosure/SerializableClosure.php
Expand Up @@ -24,7 +24,7 @@ class SerializableClosure implements \Serializable
/**
* @var array The calculated state to serialize
*/
private $state;
protected $state;

/**
* @param \Closure $closure
Expand Down Expand Up @@ -65,20 +65,14 @@ public function __invoke()
}

/**
* Uses the closure parser to fetch the closure's code. The code and the closure's context are serialized
* Serialize the code and of context of the closure
*
* @return string
*/
public function serialize()
{
// Prepare the state to serialize using a ClosureParser
if (!$this->state) {
$parser = new ClosureParser($this->getReflection());
$this->state = array($parser->getCode());
// Add the used variables (context) to the state, but wrap all closures with SerializableClosure
$this->state[] = array_map(function ($var) {
return ($var instanceof \Closure) ? new self($var) : $var;
}, $parser->getUsedVariables());
$this->createState();
}

return serialize($this->state);
Expand All @@ -104,4 +98,17 @@ public function unserialize($__serialized__)
// Evaluate the code to recreate the Closure
eval("\$this->closure = {$__code__};");
}

/**
* Uses the closure parser to fetch the closure's code and context
*/
protected function createState()
{
$parser = new ClosureParser($this->getReflection());
$this->state = array($parser->getCode());
// Add the used variables (context) to the state, but wrap all closures with SerializableClosure
$this->state[] = array_map(function ($var) {
return ($var instanceof \Closure) ? new self($var) : $var;
}, $parser->getUsedVariables());
}
}

0 comments on commit d054000

Please sign in to comment.