From 28322ce4d32d84db70167397304b33507dc259c2 Mon Sep 17 00:00:00 2001 From: Michal Moravec Date: Mon, 14 Mar 2016 11:44:48 +0100 Subject: [PATCH] Fixed serialize issue, fixes #63 --- lib/ILess/Cache/Cache.php | 5 +++-- lib/ILess/FileInfo.php | 7 ++++-- lib/ILess/ImportedFile.php | 9 +++++--- lib/ILess/Node.php | 1 + lib/ILess/Node/ImportNode.php | 4 ++-- lib/ILess/Node/RulesetNode.php | 10 ++++----- lib/ILess/Util/Serializer.php | 39 ++++++++++++++++++++++++++++++++++ 7 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 lib/ILess/Util/Serializer.php diff --git a/lib/ILess/Cache/Cache.php b/lib/ILess/Cache/Cache.php index 5d3854c..c781cf0 100644 --- a/lib/ILess/Cache/Cache.php +++ b/lib/ILess/Cache/Cache.php @@ -10,6 +10,7 @@ namespace ILess\Cache; use ILess\Configurable; +use ILess\Util\Serializer; use InvalidArgumentException; /** @@ -53,7 +54,7 @@ protected function getLifetime($lifetime) */ protected function serialize($data) { - return serialize($data); + return Serializer::serialize($data); } /** @@ -65,6 +66,6 @@ protected function serialize($data) */ protected function unserialize($data) { - return unserialize($data); + return Serializer::unserialize($data); } } diff --git a/lib/ILess/FileInfo.php b/lib/ILess/FileInfo.php index 5062a46..d499373 100644 --- a/lib/ILess/FileInfo.php +++ b/lib/ILess/FileInfo.php @@ -9,6 +9,8 @@ namespace ILess; +use ILess\Util\Serializer; + /** * File information. */ @@ -85,12 +87,13 @@ public function serialize() unset($vars['reference']); unset($vars['rootPath']); - return serialize($vars); + return Serializer::serialize($vars); } public function unserialize($serialized) { - $unserialized = unserialize($serialized); + $unserialized = Serializer::unserialize($serialized); + foreach ($unserialized as $var => $val) { $this->$var = $val; } diff --git a/lib/ILess/ImportedFile.php b/lib/ILess/ImportedFile.php index ff3b92a..70bb7aa 100644 --- a/lib/ILess/ImportedFile.php +++ b/lib/ILess/ImportedFile.php @@ -11,6 +11,7 @@ use ILess\Exception\Exception; use ILess\Node\RulesetNode; +use ILess\Util\Serializer; /** * Import. @@ -146,17 +147,19 @@ public function getLastModified() public function serialize() { - return serialize([ + return Serializer::serialize([ $this->path, $this->lastModified, base64_encode($this->content), - $this->ruleset, + // we cannot include the parsed ruleset, it contains circular references + // Is there any way to handle it? ]); } public function unserialize($serialized) { - list($this->path, $this->lastModified, $this->content, $this->ruleset) = unserialize($serialized); + list($this->path, $this->lastModified, $this->content) = Serializer::unserialize($serialized); + $this->content = base64_decode($this->content); } } diff --git a/lib/ILess/Node.php b/lib/ILess/Node.php index b6e9fc7..8e9559f 100644 --- a/lib/ILess/Node.php +++ b/lib/ILess/Node.php @@ -261,4 +261,5 @@ public function compileFirst() { return $this->compileFirst; } + } diff --git a/lib/ILess/Node/ImportNode.php b/lib/ILess/Node/ImportNode.php index 1e32022..53605d5 100644 --- a/lib/ILess/Node/ImportNode.php +++ b/lib/ILess/Node/ImportNode.php @@ -325,12 +325,12 @@ public function serialize() $vars = get_object_vars($this); unset($vars['skip'], $vars['error']); - return serialize($vars); + return Util\Serializer::serialize($vars); } public function unserialize($serialized) { - $unserialized = unserialize($serialized); + $unserialized = Util\Serializer::unserialize($serialized); foreach ($unserialized as $var => $val) { $this->$var = $val; } diff --git a/lib/ILess/Node/RulesetNode.php b/lib/ILess/Node/RulesetNode.php index 51cf194..2aec1ec 100644 --- a/lib/ILess/Node/RulesetNode.php +++ b/lib/ILess/Node/RulesetNode.php @@ -14,6 +14,7 @@ use ILess\Exception\ParserException; use ILess\Node; use ILess\Output\OutputInterface; +use ILess\Util\Serializer; use ILess\Visitor\VisitorInterface; /** @@ -1001,18 +1002,15 @@ public function isRulesetLike() public function serialize() { $vars = get_object_vars($this); - unset($vars['functionRegistry']); - $serialized = serialize($vars); - - return $serialized; + return Serializer::serialize($vars); } public function unserialize($serialized) { - $unserialized = unserialize($serialized); + $unserialized = Serializer::unserialize($serialized); foreach ($unserialized as $var => $val) { $this->$var = $val; - }; + } } } diff --git a/lib/ILess/Util/Serializer.php b/lib/ILess/Util/Serializer.php new file mode 100644 index 0000000..f9c5c58 --- /dev/null +++ b/lib/ILess/Util/Serializer.php @@ -0,0 +1,39 @@ +