Skip to content

Commit

Permalink
Merge pull request #20 from laravel/feat/minor-refactors
Browse files Browse the repository at this point in the history
[1.x] Minor refactors
  • Loading branch information
taylorotwell authored Sep 29, 2021
2 parents 64f91a7 + 10e2614 commit 20333bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
37 changes: 10 additions & 27 deletions src/Serializers/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Laravel\SerializableClosure\Contracts\Serializable;
use Laravel\SerializableClosure\SerializableClosure;
use Laravel\SerializableClosure\Support\ClosureScope;
use Laravel\SerializableClosure\Support\ClosureStream;
use Laravel\SerializableClosure\Support\ReflectionClosure;
Expand Down Expand Up @@ -43,7 +44,7 @@ class Native implements Serializable
/**
* The closure's code.
*
* @var string
* @var array|null
*/
protected $code;

Expand All @@ -57,17 +58,10 @@ class Native implements Serializable
/**
* The closure's scope.
*
* @var string
* @var \Laravel\SerializableClosure\Support\ClosureScope|null
*/
protected $scope;

/**
* Holds the context during serialization/unserialization.
*
* @var \Laravel\SerializableClosure\Support\ClosureContext
*/
protected static $context;

/**
* The "key" that marks an array as recursive.
*/
Expand All @@ -82,11 +76,6 @@ class Native implements Serializable
public function __construct(Closure $closure)
{
$this->closure = $closure;

if (static::$context !== null) {
$this->scope = static::$context->scope;
$this->scope->toSerialize++;
}
}

/**
Expand Down Expand Up @@ -168,6 +157,7 @@ public function __serialize()
/**
* Restore the closure after serialization.
*
* @param array $data
* @return void
*/
public function __unserialize($data)
Expand Down Expand Up @@ -214,15 +204,11 @@ public function __unserialize($data)
* Ensures the given closures are serializable.
*
* @param mixed $data
* @param \Laravel\SerializableClosure\Support\ClosureContext $storage
* @param \Laravel\SerializableClosure\Support\ClosureScope $storage
* @return void
*/
public static function wrapClosures(&$data, $storage = null)
public static function wrapClosures(&$data, $storage)
{
if ($storage === null) {
$storage = static::$context->scope;
}

if ($data instanceof Closure) {
$data = static::from($data);
} elseif (is_array($data)) {
Expand Down Expand Up @@ -304,7 +290,7 @@ public static function wrapClosures(&$data, $storage = null)
/**
* Gets the closure's reflector.
*
* @return \Laravel\SerializableClosure\ReflectionClosure
* @return \Laravel\SerializableClosure\Support\ReflectionClosure
*/
public function getReflector()
{
Expand Down Expand Up @@ -409,7 +395,8 @@ protected function mapPointers(&$data)
/**
* Internal method used to map closures by reference.
*
* @param mixed &$data
* @param mixed $data
* @return void
*/
protected function mapByReference(&$data)
{
Expand All @@ -428,11 +415,7 @@ protected function mapByReference(&$data)

$instance = new static($data);

if (static::$context !== null) {
static::$context->scope->toSerialize--;
} else {
$instance->scope = $this->scope;
}
$instance->scope = $this->scope;

$data = $this->scope[$data] = $instance;
} elseif (is_array($data)) {
Expand Down
41 changes: 41 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Laravel\SerializableClosure\SerializableClosure;
use Laravel\SerializableClosure\Serializers\Signed;
use Laravel\SerializableClosure\Support\ReflectionClosure;

test('closure use return value', function () {
Expand Down Expand Up @@ -326,6 +327,46 @@ function () {
expect($r[1])->toEqual($b);
})->with('serializers');

test('serialization string content dont change', function () {
$a = 100;

SerializableClosure::setSecretKey('foo');

$c = new SerializableClosure(function () use ($a) {
return $a;
});

$actual = explode('s:32:', serialize($c))[0];

expect($actual)->toBe(<<<OEF
O:47:"Laravel\SerializableClosure\SerializableClosure":1:{s:12:"serializable";O:46:"Laravel\SerializableClosure\Serializers\Signed":2:{s:12:"serializable";s:264:"O:46:"Laravel\SerializableClosure\Serializers\Native":5:{s:3:"use";a:1:{s:1:"a";i:100;}s:8:"function";s:47:"function () use (\$a) {
return \$a;
}";s:5:"scope";s:22:"P\Tests\SerializerTest";s:4:"this";N;s:4:"self";
OEF
);
});

test('use objects with serializable closures properties', function () {
$a = new stdClass();

if ($this->serializer == Signed::class) {
SerializableClosure::setSecretKey('secret');
}

$a->b = new SerializableClosure(function () {
return 'Hi';
});

$closure = function () use ($a) {
return ($a->b)();
};

$u = s($closure);
$r = $u();

expect($r)->toEqual('Hi');
})->with('serializers');

class A
{
protected static function aStaticProtected()
Expand Down

0 comments on commit 20333bb

Please sign in to comment.