1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object\Domain |
8
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
9
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/*********************************************************************************** |
14
|
|
|
* The MIT License (MIT) |
15
|
|
|
* |
16
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
19
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
20
|
|
|
* the Software without restriction, including without limitation the rights to |
21
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
22
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
23
|
|
|
* subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
30
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
31
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
32
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
33
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
34
|
|
|
***********************************************************************************/ |
35
|
|
|
|
36
|
|
|
namespace Apparat\Object\Domain\Repository; |
37
|
|
|
|
38
|
|
|
use Apparat\Kernel\Ports\Kernel; |
39
|
|
|
use Apparat\Object\Domain\Model\Object\Collection; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
41
|
|
|
use Apparat\Object\Domain\Model\Path\PathInterface; |
42
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPath; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Abstract object repository |
46
|
|
|
* |
47
|
|
|
* @package Apparat\Object\Domain\Repository |
48
|
|
|
*/ |
49
|
|
|
class Repository implements RepositoryInterface |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Apparat base URL |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $url = null; |
57
|
|
|
/** |
58
|
|
|
* Adapter strategy |
59
|
|
|
* |
60
|
|
|
* @var AdapterStrategyInterface |
61
|
|
|
*/ |
62
|
|
|
protected $adapterStrategy = null; |
63
|
|
|
/** |
64
|
|
|
* Instance specific object cache |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
protected $objectCache = []; |
69
|
|
|
|
70
|
|
|
/******************************************************************************* |
71
|
|
|
* PUBLIC METHODS |
72
|
|
|
*******************************************************************************/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Repository constructor |
76
|
|
|
* |
77
|
|
|
* @param string $url Apparat base URL |
78
|
|
|
* @param array $config Adapter strategy configuration |
79
|
|
|
*/ |
80
|
12 |
|
public function __construct( |
81
|
|
|
$url, |
82
|
|
|
array $config |
83
|
|
|
) { |
84
|
12 |
|
$this->url = rtrim('/'.$url, '/'); |
85
|
12 |
|
$this->adapterStrategy = Kernel::create(Service::class)->getAdapterStrategyFactory()->createFromConfig($config); |
86
|
8 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Find objects by selector |
90
|
|
|
* |
91
|
|
|
* @param SelectorInterface $selector Object selector |
92
|
|
|
* @return Collection Object collection |
93
|
|
|
*/ |
94
|
7 |
|
public function findObjects(SelectorInterface $selector) |
95
|
|
|
{ |
96
|
7 |
|
return new Collection($this->adapterStrategy->findObjectPaths($selector, $this)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add an object to the repository |
101
|
|
|
* |
102
|
|
|
* @param ObjectInterface $object Object |
103
|
|
|
* @return boolean Success |
104
|
|
|
*/ |
105
|
|
|
public function addObject(ObjectInterface $object) |
106
|
|
|
{ |
107
|
|
|
// TODO: Implement addObject() method. |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete and object from the repository |
112
|
|
|
* |
113
|
|
|
* @param ObjectInterface $object Object |
114
|
|
|
* @return boolean Success |
115
|
|
|
*/ |
116
|
|
|
public function deleteObject(ObjectInterface $object) |
117
|
|
|
{ |
118
|
|
|
// TODO: Implement deleteObject() method. |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Update an object in the repository |
123
|
|
|
* |
124
|
|
|
* @param ObjectInterface $object Object |
125
|
|
|
* @return bool Success |
126
|
|
|
*/ |
127
|
|
|
public function updateObject(ObjectInterface $object) |
128
|
|
|
{ |
129
|
|
|
// TODO: Implement updateObject() method. |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Load an object from this repository |
134
|
|
|
* |
135
|
|
|
* @param PathInterface $path Object path |
136
|
|
|
* @return ObjectInterface Object |
137
|
|
|
*/ |
138
|
16 |
|
public function loadObject(PathInterface $path) |
139
|
|
|
{ |
140
|
|
|
// TODO: Really OK to cache? (Immutability ...) |
141
|
16 |
|
if (empty($this->objectCache[$path->getId()->getId()])) { |
142
|
6 |
|
$this->objectCache[$path->getId()->getId()] = |
143
|
6 |
|
Kernel::create(Service::class)->getObjectManager()->loadObject( |
144
|
6 |
|
new RepositoryPath( |
145
|
6 |
|
$this, |
146
|
|
|
$path |
147
|
6 |
|
) |
148
|
6 |
|
); |
149
|
4 |
|
} |
150
|
|
|
|
151
|
14 |
|
return $this->objectCache[$path->getId()->getId()]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return the repository's adapter strategy |
156
|
|
|
* |
157
|
|
|
* @return AdapterStrategyInterface Adapter strategy |
158
|
|
|
*/ |
159
|
6 |
|
public function getAdapterStrategy() |
160
|
|
|
{ |
161
|
6 |
|
return $this->adapterStrategy; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return the repository URL (relative to Apparat base URL) |
166
|
|
|
* |
167
|
|
|
* @return string Repository URL |
168
|
|
|
*/ |
169
|
2 |
|
public function getUrl() |
170
|
|
|
{ |
171
|
2 |
|
return $this->url; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|