1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-object |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Object |
8
|
|
|
* @subpackage Apparat\Object\Ports |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Object\Ports\Facades; |
38
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
40
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
41
|
|
|
use Apparat\Object\Domain\Model\Uri\LocatorInterface; |
42
|
|
|
use Apparat\Object\Domain\Model\Uri\RepositoryLocator; |
43
|
|
|
use Apparat\Object\Domain\Repository\RepositoryInterface; |
44
|
|
|
use Apparat\Object\Infrastructure\Factory\ApparatObjectFactory; |
45
|
|
|
use Apparat\Object\Ports\Contract\ApparatObjectInterface; |
46
|
|
|
use Apparat\Object\Ports\Factory\SelectorFactory; |
47
|
|
|
use Apparat\Object\Ports\Repository\SelectorInterface; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Repository facade |
51
|
|
|
* |
52
|
|
|
* @package Apparat\Object |
53
|
|
|
* @subpackage Apparat\Object\Ports |
54
|
|
|
*/ |
55
|
|
|
class RepositoryFacade implements FacadeInterface |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* Repository |
59
|
|
|
* |
60
|
|
|
* @var RepositoryInterface |
61
|
|
|
*/ |
62
|
|
|
protected $repository; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Repository facade constructor |
66
|
|
|
* |
67
|
|
|
* @param RepositoryInterface $repository |
68
|
|
|
*/ |
69
|
10 |
|
protected function __construct(RepositoryInterface $repository) |
70
|
|
|
{ |
71
|
10 |
|
$this->repository = $repository; |
72
|
10 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Register a repository |
76
|
|
|
* |
77
|
|
|
* @param string $url Repository URL (relative or absolute including the apparat base URL) |
78
|
|
|
* @param array $config Repository configuration |
79
|
|
|
* @return RepositoryFacade Repository facade |
80
|
|
|
* @api |
81
|
|
|
*/ |
82
|
1 |
|
public static function register($url, array $config) |
83
|
|
|
{ |
84
|
1 |
|
return new static(\Apparat\Object\Infrastructure\Repository\Repository::register($url, $config)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Instantiate and return an object repository |
89
|
|
|
* |
90
|
|
|
* @param string $url Repository URL (relative or absolute including the apparat base URL) |
91
|
|
|
* @return RepositoryFacade Repository facade |
92
|
|
|
* @api |
93
|
|
|
*/ |
94
|
8 |
|
public static function instance($url) |
95
|
|
|
{ |
96
|
8 |
|
return new static(\Apparat\Object\Infrastructure\Repository\Repository::instance($url)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create a repository |
101
|
|
|
* |
102
|
|
|
* @param string $url Repository URL (relative or absolute including the apparat base URL) |
103
|
|
|
* @param array $config Repository configuration |
104
|
|
|
* @return RepositoryFacade Repository facade |
105
|
|
|
* @api |
106
|
|
|
*/ |
107
|
1 |
|
public static function create($url, array $config) |
108
|
|
|
{ |
109
|
1 |
|
return new static(\Apparat\Object\Infrastructure\Repository\Repository::create($url, $config)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Find objects by selector |
114
|
|
|
* |
115
|
|
|
* @param string|SelectorInterface $selector Object selector |
116
|
|
|
* @return array Objects |
117
|
|
|
*/ |
118
|
1 |
|
public function findObjects($selector) |
119
|
|
|
{ |
120
|
1 |
|
if (!($selector instanceof SelectorInterface)) { |
121
|
1 |
|
$selector = SelectorFactory::createFromString($selector); |
122
|
1 |
|
} |
123
|
1 |
|
$objects = []; |
124
|
|
|
/** @var ObjectInterface $object */ |
125
|
1 |
|
foreach ($this->repository->findObjects($selector) as $object) { |
126
|
1 |
|
$objects[] = ApparatObjectFactory::create($object); |
127
|
1 |
|
} |
128
|
1 |
|
return $objects; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Load an object from this repository |
133
|
|
|
* |
134
|
|
|
* @param string $locator Object locator |
135
|
|
|
* @param int $visibility Object visibility |
136
|
|
|
* @return ApparatObjectInterface Object |
137
|
|
|
*/ |
138
|
7 |
|
public function loadObject($locator, $visibility = SelectorInterface::ALL) |
139
|
|
|
{ |
140
|
|
|
/** @var LocatorInterface $objectLocator */ |
141
|
7 |
|
$objectLocator = Kernel::create(RepositoryLocator::class, [$this->repository, $locator]); |
142
|
7 |
|
return ApparatObjectFactory::create($this->repository->loadObject($objectLocator, $visibility)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|