1 | <?php |
||
51 | class Service |
||
52 | { |
||
53 | /** |
||
54 | * Registered repositories |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $registry = []; |
||
59 | /** |
||
60 | * Repository auto-connector service |
||
61 | * |
||
62 | * @var AutoConnectorInterface |
||
63 | */ |
||
64 | protected $autoConnector = null; |
||
65 | /** |
||
66 | * Adapter strategy factory |
||
67 | * |
||
68 | * @var AdapterStrategyFactoryInterface |
||
69 | */ |
||
70 | protected $adapterStrategyFactory = null; |
||
71 | /** |
||
72 | * Object manager |
||
73 | * |
||
74 | * @var ManagerInterface |
||
75 | */ |
||
76 | protected $objectManager = null; |
||
77 | /** |
||
78 | * Auto-connect to repositories |
||
79 | * |
||
80 | * @var bool |
||
81 | */ |
||
82 | protected $autoConnectEnabled = true; |
||
83 | |||
84 | /******************************************************************************* |
||
85 | * PUBLIC METHODS |
||
86 | *******************************************************************************/ |
||
87 | |||
88 | /** |
||
89 | * Repository service constructor |
||
90 | * |
||
91 | * @param AutoConnectorInterface $autoConnector Auto-connector |
||
92 | * @param AdapterStrategyFactoryInterface $adapterStrategyFactory Adapter strategy factory |
||
93 | * @param ManagerInterface $objectManager Object manager |
||
94 | */ |
||
95 | public function __construct( |
||
104 | |||
105 | /** |
||
106 | * Pre-register a repository |
||
107 | * |
||
108 | * The purpose of repository pre-registration is to provide custom arguments (like a base |
||
109 | * directory or basic authentication credentials. |
||
110 | * The repository URL may be local or remote, relative or absolute, with Apparat or HTTP scheme. |
||
111 | * |
||
112 | * @param string|Url $url Repository URL |
||
113 | * @param RepositoryInterface $repository Repository |
||
114 | */ |
||
115 | 6 | public function register($url, RepositoryInterface $repository) |
|
121 | |||
122 | /** |
||
123 | * Return an object repository by URL |
||
124 | * |
||
125 | * If a repository URL hasn't been pre-registered, the method tries to perform an adhoc registration |
||
126 | * based on the URL given. |
||
127 | * The repository URL may be local or remote, relative or absolute, with Apparat or HTTP scheme. |
||
128 | * |
||
129 | * @param string|Url $url Repository URL |
||
130 | * @return \Apparat\Object\Domain\Repository\Repository Object repository |
||
131 | * @throws InvalidArgumentException If the repository URL is invalid |
||
132 | * @throws InvalidArgumentException If the repository URL is unknown |
||
133 | */ |
||
134 | 9 | public function get($url) |
|
149 | |||
150 | /** |
||
151 | * Test whether a repository URL is registered |
||
152 | * |
||
153 | * @param string $url Repository URL |
||
154 | * @return bool Repository URL is registered |
||
155 | */ |
||
156 | 3 | public function isRegistered($url) |
|
161 | |||
162 | /** |
||
163 | * Return the adapter strategy factory |
||
164 | * |
||
165 | * @return AdapterStrategyFactoryInterface Adapter strategy factory |
||
166 | */ |
||
167 | 10 | public function getAdapterStrategyFactory() |
|
171 | |||
172 | /** |
||
173 | * Return the object manager |
||
174 | * |
||
175 | * @return ManagerInterface Object manager |
||
176 | */ |
||
177 | 2 | public function getObjectManager() |
|
181 | |||
182 | /** |
||
183 | * Normalize a repository URL |
||
184 | * |
||
185 | * @param string|Url $url Repository URL |
||
186 | * @return string Normalized repository URL |
||
187 | * @throws InvalidArgumentException If the repository URL is invalid |
||
188 | */ |
||
189 | 17 | public static function normalizeRepositoryUrl($url) |
|
190 | { |
||
191 | // If it's an apparat URL |
||
192 | 17 | if ($url instanceof ApparatUrl) { |
|
193 | $url = $url->getNormalizedRepositoryUrl(); |
||
194 | |||
195 | // Else: If it's an object URL |
||
196 | 17 | } elseif ($url instanceof ObjectUrl) { |
|
197 | $url = $url->getRepositoryUrl(); |
||
198 | |||
199 | // Else: If it's a simple URL |
||
200 | 17 | } elseif ($url instanceof Url) { |
|
201 | $url = $url->getPath(); |
||
202 | |||
203 | // Else: If it's an empty URL |
||
204 | 17 | } elseif ($url === null) { |
|
205 | 1 | return ''; |
|
206 | } |
||
207 | |||
208 | // If the URL is a string |
||
209 | 16 | if (is_string($url)) { |
|
210 | |||
211 | // Strip the leading apparat base URL |
||
212 | 16 | $apparatBaseUrl = getenv('APPARAT_BASE_URL'); |
|
213 | 16 | if (strpos($url, $apparatBaseUrl) === 0) { |
|
214 | $url = strval(substr($url, strlen($apparatBaseUrl))); |
||
215 | } |
||
216 | |||
217 | // Ensure this is a bare URL (without query and fragment) |
||
218 | 16 | if (Module::isAbsoluteBareUrl($apparatBaseUrl.$url)) { |
|
219 | 15 | return ltrim($url, '/'); |
|
220 | } |
||
221 | } |
||
222 | |||
223 | // The URL is invalid, throw an error |
||
224 | throw new InvalidArgumentException( |
||
225 | sprintf('Invalid repository URL "%s"', $url), |
||
226 | InvalidArgumentException::INVALID_REPOSITORY_URL |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Enable repository auto-connections |
||
232 | * |
||
233 | * If the method is called without any arguments it will just return the current auto-connection state. |
||
234 | * |
||
235 | * @param null|boolean $autoConnect Enable / disable auto-connections |
||
236 | * @return bool Status of repository auto-connection |
||
237 | */ |
||
238 | public function useAutoConnect($autoConnect = null) |
||
239 | { |
||
240 | if ($autoConnect !== null) { |
||
241 | $this->autoConnectEnabled = (boolean)$autoConnect; |
||
242 | } |
||
243 | return $this->autoConnectEnabled; |
||
244 | } |
||
245 | |||
246 | /******************************************************************************* |
||
247 | * PRIVATE METHODS |
||
248 | *******************************************************************************/ |
||
249 | |||
250 | /** |
||
251 | * Test whether a repository URL registered or can be auto-connected |
||
252 | * |
||
253 | * @param string $url Repository URL |
||
254 | * @return bool Repository is connected |
||
255 | */ |
||
256 | 9 | protected function connected($url) |
|
266 | } |
||
267 |