Skip to content

Commit

Permalink
Fixed serialize issue, fixes #63
Browse files Browse the repository at this point in the history
  • Loading branch information
mishal committed Mar 14, 2016
1 parent 7df17a4 commit 28322ce
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/ILess/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ILess\Cache;

use ILess\Configurable;
use ILess\Util\Serializer;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ protected function getLifetime($lifetime)
*/
protected function serialize($data)
{
return serialize($data);
return Serializer::serialize($data);
}

/**
Expand All @@ -65,6 +66,6 @@ protected function serialize($data)
*/
protected function unserialize($data)
{
return unserialize($data);
return Serializer::unserialize($data);
}
}
7 changes: 5 additions & 2 deletions lib/ILess/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace ILess;

use ILess\Util\Serializer;

/**
* File information.
*/
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/ILess/ImportedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use ILess\Exception\Exception;
use ILess\Node\RulesetNode;
use ILess\Util\Serializer;

/**
* Import.
Expand Down Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions lib/ILess/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,5 @@ public function compileFirst()
{
return $this->compileFirst;
}

}
4 changes: 2 additions & 2 deletions lib/ILess/Node/ImportNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 4 additions & 6 deletions lib/ILess/Node/RulesetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ILess\Exception\ParserException;
use ILess\Node;
use ILess\Output\OutputInterface;
use ILess\Util\Serializer;
use ILess\Visitor\VisitorInterface;

/**
Expand Down Expand Up @@ -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;
};
}
}
}
39 changes: 39 additions & 0 deletions lib/ILess/Util/Serializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Sift PHP framework.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ILess\Util;

final class Serializer
{
/**
* Serializes variables.
*
* @param array $vars
*
* @return string
*/
public static function serialize($vars)
{
return serialize($vars);
}

/**
* @param string $serialized
*
* @return array
*/
public static function unserialize($serialized)
{
$unserialized = @unserialize($serialized);
if ($unserialized === false) {
throw new \RuntimeException('Error unserializing serialized data.');
}

return $unserialized;
}
}

0 comments on commit 28322ce

Please sign in to comment.