-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from zumba/multiple-closure-serializers
Multiple closure serializers - With opis/closure support
- Loading branch information
Showing
13 changed files
with
424 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/JsonSerializer/ClosureSerializer/ClosureSerializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Zumba\JsonSerializer\ClosureSerializer; | ||
|
||
use Closure; | ||
|
||
interface ClosureSerializer { | ||
|
||
/** | ||
* Serialize a closure | ||
* | ||
* @param Closure $closure | ||
* @return string | ||
*/ | ||
public function serialize(Closure $closure); | ||
|
||
/** | ||
* Unserialize a closure | ||
* | ||
* @param string $serialized | ||
* @return Closure | ||
*/ | ||
public function unserialize($serialized); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/JsonSerializer/ClosureSerializer/ClosureSerializerManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Zumba\JsonSerializer\ClosureSerializer; | ||
|
||
class ClosureSerializerManager { | ||
|
||
/** | ||
* Closure serializer instances | ||
* | ||
* @var array | ||
*/ | ||
protected $closureSerializer = array(); | ||
|
||
/** | ||
* Prefered closure serializer | ||
*/ | ||
protected $preferred = array( | ||
OpisClosureSerializer::class, | ||
SuperClosureSerializer::class | ||
); | ||
|
||
/** | ||
* Set closure engine | ||
* | ||
* @param ClosureSerializer $closureSerializer | ||
* @return self | ||
*/ | ||
public function addSerializer(ClosureSerializer $closureSerializer) | ||
{ | ||
$classname = $closureSerializer::class; | ||
$this->closureSerializer[$classname] = $closureSerializer; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get preferred closure serializer | ||
* | ||
* @return ClosureSerializer|null | ||
*/ | ||
public function getPreferredSerializer() | ||
{ | ||
if (empty($this->closureSerializer)) { | ||
return null; | ||
} | ||
|
||
foreach ($this->preferred as $preferred) { | ||
if (isset($this->closureSerializer[$preferred])) { | ||
return $this->closureSerializer[$preferred]; | ||
} | ||
} | ||
return current($this->closureSerializer); | ||
} | ||
|
||
/** | ||
* Get closure serializer | ||
* | ||
* @param string $classname | ||
* @return ClosureSerializer|null | ||
*/ | ||
public function getSerializer(string $classname) | ||
{ | ||
if (isset($this->closureSerializer[$classname])) { | ||
return $this->closureSerializer[$classname]; | ||
} | ||
return null; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/JsonSerializer/ClosureSerializer/OpisClosureSerializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Zumba\JsonSerializer\ClosureSerializer; | ||
|
||
use Closure; | ||
use Opis\Closure\SerializableClosure as OpisSerializableClosure; | ||
|
||
class OpisClosureSerializer implements ClosureSerializer { | ||
|
||
/** | ||
* Serialize a closure | ||
* | ||
* @param Closure $closure | ||
* @return string | ||
*/ | ||
public function serialize(Closure $closure) | ||
{ | ||
return serialize(new OpisSerializableClosure($closure)); | ||
} | ||
|
||
/** | ||
* Unserialize a closure | ||
* | ||
* @param string $serialized | ||
* @return Closure | ||
*/ | ||
public function unserialize($serialized) | ||
{ | ||
return unserialize($serialized)->getClosure(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/JsonSerializer/ClosureSerializer/SuperClosureSerializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Zumba\JsonSerializer\ClosureSerializer; | ||
|
||
use Closure; | ||
use SuperClosure\SerializerInterface as SuperClosureSerializerInterface; | ||
|
||
class SuperClosureSerializer implements ClosureSerializer { | ||
|
||
/** | ||
* Closure serializer instance | ||
* | ||
* @var SuperClosureSerializerInterface | ||
*/ | ||
protected $serializer; | ||
|
||
/** | ||
* Closure serializer instance | ||
* | ||
* @var SuperClosureSerializerInterface | ||
*/ | ||
public function __construct(SuperClosureSerializerInterface $serializer) | ||
{ | ||
$this->serializer = $serializer; | ||
} | ||
|
||
/** | ||
* Serialize a closure | ||
* | ||
* @param Closure $closure | ||
* @return string | ||
*/ | ||
public function serialize(Closure $closure) | ||
{ | ||
return $this->serializer->serialize($closure); | ||
} | ||
|
||
/** | ||
* Unserialize a closure | ||
* | ||
* @param string $serialized | ||
* @return Closure | ||
*/ | ||
public function unserialize($serialized) | ||
{ | ||
return $this->serializer->unserialize($serialized); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Zumba\JsonSerializer\Test\ClosureSerializer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zumba\JsonSerializer\ClosureSerializer\ClosureSerializerManager; | ||
use Zumba\JsonSerializer\ClosureSerializer\SuperClosureSerializer; | ||
|
||
class ClosureSerializerManagerTest extends TestCase | ||
{ | ||
public function testAddSerializer() { | ||
$manager = new ClosureSerializerManager(); | ||
$this->assertEmpty($manager->getSerializer('foo')); | ||
$manager->addSerializer(new SuperClosureSerializer(new \SuperClosure\Serializer())); | ||
$this->assertNotEmpty($manager->getSerializer(SuperClosureSerializer::class)); | ||
} | ||
|
||
public function testGetPreferredSerializer() { | ||
$manager = new ClosureSerializerManager(); | ||
$this->assertNull($manager->getPreferredSerializer()); | ||
|
||
$serializer = new SuperClosureSerializer(new \SuperClosure\Serializer()); | ||
$manager->addSerializer($serializer); | ||
$this->assertSame($serializer, $manager->getPreferredSerializer()); | ||
} | ||
} |
Oops, something went wrong.