@@ -24,7 +24,7 @@ class SerializableClosure implements \Serializable
24
24
/**
25
25
* @var array The calculated state to serialize
26
26
*/
27
- private $ state ;
27
+ protected $ state ;
28
28
29
29
/**
30
30
* @param \Closure $closure
@@ -65,20 +65,14 @@ public function __invoke()
65
65
}
66
66
67
67
/**
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
69
69
*
70
70
* @return string
71
71
*/
72
72
public function serialize ()
73
73
{
74
- // Prepare the state to serialize using a ClosureParser
75
74
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 ();
82
76
}
83
77
84
78
return serialize ($ this ->state );
@@ -104,4 +98,17 @@ public function unserialize($__serialized__)
104
98
// Evaluate the code to recreate the Closure
105
99
eval ("\$this->closure = {$ __code__ }; " );
106
100
}
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
+ }
107
114
}
0 commit comments