|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Runalyze BSON. |
|
5
|
|
|
* (c) RUNALYZE <[email protected]> |
|
6
|
|
|
* This source file is subject to the MIT license that is bundled |
|
7
|
|
|
* with this source code in the file LICENSE. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Runalyze\BSON\LazyFilesystemObject; |
|
11
|
|
|
|
|
12
|
|
|
use League\Flysystem\Filesystem; |
|
13
|
|
|
use ProxyManager\Configuration; |
|
14
|
|
|
use ProxyManager\Factory\LazyLoadingValueHolderFactory; |
|
15
|
|
|
use Runalyze\BSON\BsonSerializable\BsonSerializableInterface; |
|
16
|
|
|
|
|
17
|
|
|
class LazyFilesystemObjectStorage |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Filesystem */ |
|
20
|
|
|
protected $Filesystem; |
|
21
|
|
|
|
|
22
|
|
|
/** @var HashToPathMapperInterface */ |
|
23
|
|
|
protected $PathMapper; |
|
24
|
|
|
|
|
25
|
|
|
/** @var LazyLoadingValueHolderFactory */ |
|
26
|
|
|
protected $ProxyFactory; |
|
27
|
|
|
|
|
28
|
1 |
|
public function __construct( |
|
29
|
|
|
Filesystem $filesystem, |
|
30
|
|
|
HashToPathMapperInterface $pathMapper, |
|
31
|
|
|
Configuration $proxyConfiguration = null |
|
32
|
|
|
) { |
|
33
|
1 |
|
$this->Filesystem = $filesystem; |
|
34
|
1 |
|
$this->PathMapper = $pathMapper; |
|
35
|
1 |
|
$this->ProxyFactory = new LazyLoadingValueHolderFactory($proxyConfiguration); |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $hash |
|
40
|
|
|
* @param string $class |
|
41
|
|
|
* @return BsonSerializableInterface |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function getObject($hash, $class) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$path = $this->PathMapper->hashToPath($hash); |
|
46
|
|
|
|
|
47
|
1 |
|
return $this->ProxyFactory->createProxy( |
|
|
|
|
|
|
48
|
1 |
|
$class, |
|
49
|
1 |
|
function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ($path, $class) { |
|
50
|
|
|
try { |
|
51
|
1 |
|
$contents = $this->Filesystem->read($path); |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
throw $e; // TODO |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
1 |
|
$wrappedObject = \MongoDB\BSON\toPHP($contents, ['root' => $class]); |
|
|
|
|
|
|
58
|
|
|
} catch (\Exception $e) { |
|
59
|
|
|
throw $e; // TODO |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
$initializer = null; |
|
63
|
1 |
|
} |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $hash |
|
69
|
|
|
* @param BsonSerializableInterface $object |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function storeObject($hash, BsonSerializableInterface $object) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$path = $this->PathMapper->hashToPath($hash); |
|
74
|
|
|
|
|
75
|
1 |
|
$this->Filesystem->put($path, $object->toBinary()); |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|