1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the DS Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Dan Smith <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Ds\Router\Serializer; |
11
|
|
|
|
12
|
|
|
use Ds\Router\Exceptions\RouteException; |
13
|
|
|
use Ds\Router\Interfaces\SerializerInterface; |
14
|
|
|
use SuperClosure\Serializer as SuperClosureSerializer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class SuperClosure |
18
|
|
|
* |
19
|
|
|
* SuperClosure\Serializer Wrapper. Used to Serialize closures. |
20
|
|
|
* |
21
|
|
|
* @package Ds\Router\Serializer |
22
|
|
|
* @author Dan Smith <[email protected]> |
23
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
24
|
|
|
* @link https://red-sqr.co.uk/Framework/Router/wikis/ |
25
|
|
|
* |
26
|
|
|
* @see "jeremeamia/SuperClosure" |
27
|
|
|
*/ |
28
|
|
|
class SuperClosure implements SerializerInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var SuperClosureSerializer |
32
|
|
|
*/ |
33
|
|
|
public $serializer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* SuperClosure constructor. |
37
|
|
|
* |
38
|
|
|
* @param SuperClosureSerializer $serializer |
39
|
|
|
*/ |
40
|
4 |
|
public function __construct(SuperClosureSerializer $serializer) |
41
|
|
|
{ |
42
|
4 |
|
$this->serializer = $serializer; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
2 |
|
public function serialize($data) |
50
|
|
|
{ |
51
|
2 |
|
$classRef = new \ReflectionClass($data); |
52
|
2 |
|
if ($classRef->getName() && $classRef->getName() !== 'Closure') { |
|
|
|
|
53
|
1 |
|
throw new RouteException('Class: ' . $classRef->getName() . ' can not be used as a handler. Must be a valid closure or string'); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return \base64_encode($this->serializer->serialize($data)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
* @throws \SuperClosure\Exception\ClosureUnserializationException |
62
|
|
|
*/ |
63
|
1 |
|
public function unserialize(string $string) |
64
|
|
|
{ |
65
|
1 |
|
return $this->serializer->unserialize(\base64_decode($string)); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|