1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees; 21 22use Fisharebest\Webtrees\Contracts\CacheFactoryInterface; 23use Fisharebest\Webtrees\Contracts\FamilyFactoryInterface; 24use Fisharebest\Webtrees\Contracts\FilesystemFactoryInterface; 25use Fisharebest\Webtrees\Contracts\ElementFactoryInterface; 26use Fisharebest\Webtrees\Contracts\GedcomRecordFactoryInterface; 27use Fisharebest\Webtrees\Contracts\HeaderFactoryInterface; 28use Fisharebest\Webtrees\Contracts\ImageFactoryInterface; 29use Fisharebest\Webtrees\Contracts\IndividualFactoryInterface; 30use Fisharebest\Webtrees\Contracts\LocationFactoryInterface; 31use Fisharebest\Webtrees\Contracts\MediaFactoryInterface; 32use Fisharebest\Webtrees\Contracts\NoteFactoryInterface; 33use Fisharebest\Webtrees\Contracts\RepositoryFactoryInterface; 34use Fisharebest\Webtrees\Contracts\SlugFactoryInterface; 35use Fisharebest\Webtrees\Contracts\SourceFactoryInterface; 36use Fisharebest\Webtrees\Contracts\SubmissionFactoryInterface; 37use Fisharebest\Webtrees\Contracts\SubmitterFactoryInterface; 38use Fisharebest\Webtrees\Contracts\XrefFactoryInterface; 39use Psr\Http\Message\ResponseFactoryInterface; 40use Psr\Http\Message\ServerRequestFactoryInterface; 41use Psr\Http\Message\StreamFactoryInterface; 42use Psr\Http\Message\UploadedFileFactoryInterface; 43use Psr\Http\Message\UriFactoryInterface; 44 45/** 46 * Provide access to factory objects and those that represent external entities (filesystems, caches) 47 */ 48class Registry 49{ 50 // Factories for webtrees objects 51 private static CacheFactoryInterface $cache_factory; 52 private static ElementFactoryInterface $element_factory; 53 private static FamilyFactoryInterface $family_factory; 54 private static FilesystemFactoryInterface $filesystem_factory; 55 private static GedcomRecordFactoryInterface $gedcom_record_factory; 56 private static HeaderFactoryInterface $header_factory; 57 private static ImageFactoryInterface $image_factory; 58 private static IndividualFactoryInterface $individual_factory; 59 private static LocationFactoryInterface $location_factory; 60 private static MediaFactoryInterface $media_factory; 61 private static NoteFactoryInterface $note_factory; 62 private static RepositoryFactoryInterface $repository_factory; 63 private static SlugFactoryInterface $slug_factory; 64 private static SourceFactoryInterface $source_factory; 65 private static SubmissionFactoryInterface $submission_factory; 66 private static SubmitterFactoryInterface $submitter_factory; 67 private static XrefFactoryInterface $xref_factory; 68 69 // Factories for PSR7 /PSR17 messages 70 private static ResponseFactoryInterface $response_factory; 71 private static ServerRequestFactoryInterface $server_request_factory; 72 private static StreamFactoryInterface $stream_factory; 73 private static UploadedFileFactoryInterface $uploaded_file_factory; 74 private static UriFactoryInterface $uri_factory; 75 76 /** 77 * Store or retrieve a factory object. 78 * 79 * @param CacheFactoryInterface|null $factory 80 * 81 * @return CacheFactoryInterface 82 */ 83 public static function cache(CacheFactoryInterface $factory = null): CacheFactoryInterface 84 { 85 if ($factory instanceof CacheFactoryInterface) { 86 self::$cache_factory = $factory; 87 } 88 89 return self::$cache_factory; 90 } 91 92 /** 93 * Store or retrieve a factory object. 94 * 95 * @param ElementFactoryInterface|null $factory 96 * 97 * @return ElementFactoryInterface 98 */ 99 public static function elementFactory(ElementFactoryInterface $factory = null): ElementFactoryInterface 100 { 101 if ($factory instanceof ElementFactoryInterface) { 102 self::$element_factory = $factory; 103 } 104 105 return self::$element_factory; 106 } 107 108 /** 109 * Store or retrieve a factory object. 110 * 111 * @param FamilyFactoryInterface|null $factory 112 * 113 * @return FamilyFactoryInterface 114 */ 115 public static function familyFactory(FamilyFactoryInterface $factory = null): FamilyFactoryInterface 116 { 117 if ($factory instanceof FamilyFactoryInterface) { 118 self::$family_factory = $factory; 119 } 120 121 return self::$family_factory; 122 } 123 124 /** 125 * Store or retrieve a factory object. 126 * 127 * @param FilesystemFactoryInterface|null $factory 128 * 129 * @return FilesystemFactoryInterface 130 */ 131 public static function filesystem(FilesystemFactoryInterface $factory = null): FilesystemFactoryInterface 132 { 133 if ($factory instanceof FilesystemFactoryInterface) { 134 self::$filesystem_factory = $factory; 135 } 136 137 return self::$filesystem_factory; 138 } 139 140 /** 141 * Store or retrieve a factory object. 142 * 143 * @param GedcomRecordFactoryInterface|null $factory 144 * 145 * @return GedcomRecordFactoryInterface 146 */ 147 public static function gedcomRecordFactory(GedcomRecordFactoryInterface $factory = null): GedcomRecordFactoryInterface 148 { 149 if ($factory instanceof GedcomRecordFactoryInterface) { 150 self::$gedcom_record_factory = $factory; 151 } 152 153 return self::$gedcom_record_factory; 154 } 155 156 /** 157 * Store or retrieve a factory object. 158 * 159 * @param HeaderFactoryInterface|null $factory 160 * 161 * @return HeaderFactoryInterface 162 */ 163 public static function headerFactory(HeaderFactoryInterface $factory = null): HeaderFactoryInterface 164 { 165 if ($factory instanceof HeaderFactoryInterface) { 166 self::$header_factory = $factory; 167 } 168 169 return self::$header_factory; 170 } 171 172 /** 173 * Store or retrieve a factory object. 174 * 175 * @param ImageFactoryInterface|null $factory 176 * 177 * @return ImageFactoryInterface 178 */ 179 public static function imageFactory(ImageFactoryInterface $factory = null): ImageFactoryInterface 180 { 181 if ($factory instanceof ImageFactoryInterface) { 182 self::$image_factory = $factory; 183 } 184 185 return self::$image_factory; 186 } 187 188 /** 189 * Store or retrieve a factory object. 190 * 191 * @param IndividualFactoryInterface|null $factory 192 * 193 * @return IndividualFactoryInterface 194 */ 195 public static function individualFactory(IndividualFactoryInterface $factory = null): IndividualFactoryInterface 196 { 197 if ($factory instanceof IndividualFactoryInterface) { 198 self::$individual_factory = $factory; 199 } 200 201 return self::$individual_factory; 202 } 203 204 /** 205 * Store or retrieve a factory object. 206 * 207 * @param LocationFactoryInterface|null $factory 208 * 209 * @return LocationFactoryInterface 210 */ 211 public static function locationFactory(LocationFactoryInterface $factory = null): LocationFactoryInterface 212 { 213 if ($factory instanceof LocationFactoryInterface) { 214 self::$location_factory = $factory; 215 } 216 217 return self::$location_factory; 218 } 219 220 /** 221 * Store or retrieve a factory object. 222 * 223 * @param MediaFactoryInterface|null $factory 224 * 225 * @return MediaFactoryInterface 226 */ 227 public static function mediaFactory(MediaFactoryInterface $factory = null): MediaFactoryInterface 228 { 229 if ($factory instanceof MediaFactoryInterface) { 230 self::$media_factory = $factory; 231 } 232 233 return self::$media_factory; 234 } 235 236 /** 237 * Store or retrieve a factory object. 238 * 239 * @param NoteFactoryInterface|null $factory 240 * 241 * @return NoteFactoryInterface 242 */ 243 public static function noteFactory(NoteFactoryInterface $factory = null): NoteFactoryInterface 244 { 245 if ($factory instanceof NoteFactoryInterface) { 246 self::$note_factory = $factory; 247 } 248 249 return self::$note_factory; 250 } 251 252 /** 253 * Store or retrieve a factory object. 254 * 255 * @param RepositoryFactoryInterface|null $factory 256 * 257 * @return RepositoryFactoryInterface 258 */ 259 public static function repositoryFactory(RepositoryFactoryInterface $factory = null): RepositoryFactoryInterface 260 { 261 if ($factory instanceof RepositoryFactoryInterface) { 262 self::$repository_factory = $factory; 263 } 264 265 return self::$repository_factory; 266 } 267 268 /** 269 * Store or retrieve a factory object. 270 * 271 * @param ResponseFactoryInterface|null $factory 272 * 273 * @return ResponseFactoryInterface 274 */ 275 public static function responseFactory(ResponseFactoryInterface $factory = null): ResponseFactoryInterface 276 { 277 if ($factory instanceof ResponseFactoryInterface) { 278 self::$response_factory = $factory; 279 } 280 281 return self::$response_factory; 282 } 283 284 /** 285 * Store or retrieve a factory object. 286 * 287 * @param SlugFactoryInterface|null $factory 288 * 289 * @return SlugFactoryInterface 290 */ 291 public static function slugFactory(SlugFactoryInterface $factory = null): SlugFactoryInterface 292 { 293 if ($factory instanceof SlugFactoryInterface) { 294 self::$slug_factory = $factory; 295 } 296 297 return self::$slug_factory; 298 } 299 300 /** 301 * Store or retrieve a factory object. 302 * 303 * @param ServerRequestFactoryInterface|null $factory 304 * 305 * @return ServerRequestFactoryInterface 306 */ 307 public static function serverRequestFactory(ServerRequestFactoryInterface $factory = null): ServerRequestFactoryInterface 308 { 309 if ($factory instanceof ServerRequestFactoryInterface) { 310 self::$server_request_factory = $factory; 311 } 312 313 return self::$server_request_factory; 314 } 315 316 /** 317 * Store or retrieve a factory object. 318 * 319 * @param SourceFactoryInterface|null $factory 320 * 321 * @return SourceFactoryInterface 322 */ 323 public static function sourceFactory(SourceFactoryInterface $factory = null): SourceFactoryInterface 324 { 325 if ($factory instanceof SourceFactoryInterface) { 326 self::$source_factory = $factory; 327 } 328 329 return self::$source_factory; 330 } 331 332 /** 333 * Store or retrieve a factory object. 334 * 335 * @param StreamFactoryInterface|null $factory 336 * 337 * @return StreamFactoryInterface 338 */ 339 public static function streamFactory(StreamFactoryInterface $factory = null): StreamFactoryInterface 340 { 341 if ($factory instanceof StreamFactoryInterface) { 342 self::$stream_factory = $factory; 343 } 344 345 return self::$stream_factory; 346 } 347 348 /** 349 * Store or retrieve a factory object. 350 * 351 * @param SubmissionFactoryInterface|null $factory 352 * 353 * @return SubmissionFactoryInterface 354 */ 355 public static function submissionFactory(SubmissionFactoryInterface $factory = null): SubmissionFactoryInterface 356 { 357 if ($factory instanceof SubmissionFactoryInterface) { 358 self::$submission_factory = $factory; 359 } 360 361 return self::$submission_factory; 362 } 363 364 /** 365 * Store or retrieve a factory object. 366 * 367 * @param SubmitterFactoryInterface|null $factory 368 * 369 * @return SubmitterFactoryInterface 370 */ 371 public static function submitterFactory(SubmitterFactoryInterface $factory = null): SubmitterFactoryInterface 372 { 373 if ($factory instanceof SubmitterFactoryInterface) { 374 self::$submitter_factory = $factory; 375 } 376 377 return self::$submitter_factory; 378 } 379 380 /** 381 * Store or retrieve a factory object. 382 * 383 * @param UploadedFileFactoryInterface|null $factory 384 * 385 * @return UploadedFileFactoryInterface 386 */ 387 public static function uploadedFileFactory(UploadedFileFactoryInterface $factory = null): UploadedFileFactoryInterface 388 { 389 if ($factory instanceof UploadedFileFactoryInterface) { 390 self::$uploaded_file_factory = $factory; 391 } 392 393 return self::$uploaded_file_factory; 394 } 395 396 /** 397 * Store or retrieve a factory object. 398 * 399 * @param UriFactoryInterface|null $factory 400 * 401 * @return UriFactoryInterface 402 */ 403 public static function uriFactory(UriFactoryInterface $factory = null): UriFactoryInterface 404 { 405 if ($factory instanceof UriFactoryInterface) { 406 self::$uri_factory = $factory; 407 } 408 409 return self::$uri_factory; 410 } 411 412 /** 413 * Store or retrieve a factory object. 414 * 415 * @param XrefFactoryInterface|null $factory 416 * 417 * @return XrefFactoryInterface 418 */ 419 public static function xrefFactory(XrefFactoryInterface $factory = null): XrefFactoryInterface 420 { 421 if ($factory instanceof XrefFactoryInterface) { 422 self::$xref_factory = $factory; 423 } 424 425 return self::$xref_factory; 426 } 427} 428