xref: /webtrees/app/Registry.php (revision 6fcafd028e511367c3fcdbfaa31aa05a85bf85c0)
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;
39
40/**
41 * Provide access to factory objects and those that represent external entities (filesystems, caches)
42 */
43class Registry
44{
45    private static CacheFactoryInterface $cache_factory;
46
47    private static ElementFactoryInterface $element_factory;
48
49    private static FamilyFactoryInterface $family_factory;
50
51    private static FilesystemFactoryInterface $filesystem_factory;
52
53    private static GedcomRecordFactoryInterface $gedcom_record_factory;
54
55    private static HeaderFactoryInterface $header_factory;
56
57    private static ImageFactoryInterface $image_factory;
58
59    private static IndividualFactoryInterface $individual_factory;
60
61    private static LocationFactoryInterface $location_factory;
62
63    private static MediaFactoryInterface $media_factory;
64
65    private static NoteFactoryInterface $note_factory;
66
67    private static RepositoryFactoryInterface $repository_factory;
68
69    private static SlugFactoryInterface $slug_factory;
70
71    private static SourceFactoryInterface $source_factory;
72
73    private static SubmissionFactoryInterface $submission_factory;
74
75    private static SubmitterFactoryInterface $submitter_factory;
76
77    private static XrefFactoryInterface $xref_factory;
78
79    /**
80     * Store or retrieve a factory object.
81     *
82     * @param CacheFactoryInterface|null $factory
83     *
84     * @return CacheFactoryInterface
85     */
86    public static function cache(CacheFactoryInterface $factory = null): CacheFactoryInterface
87    {
88        if ($factory instanceof CacheFactoryInterface) {
89            self::$cache_factory = $factory;
90        }
91
92        return self::$cache_factory;
93    }
94
95    /**
96     * Store or retrieve a factory object.
97     *
98     * @param ElementFactoryInterface|null $factory
99     *
100     * @return ElementFactoryInterface
101     */
102    public static function elementFactory(ElementFactoryInterface $factory = null): ElementFactoryInterface
103    {
104        if ($factory instanceof ElementFactoryInterface) {
105            self::$element_factory = $factory;
106        }
107
108        return self::$element_factory;
109    }
110
111    /**
112     * Store or retrieve a factory object.
113     *
114     * @param FamilyFactoryInterface|null $factory
115     *
116     * @return FamilyFactoryInterface
117     */
118    public static function familyFactory(FamilyFactoryInterface $factory = null): FamilyFactoryInterface
119    {
120        if ($factory instanceof FamilyFactoryInterface) {
121            self::$family_factory = $factory;
122        }
123
124        return self::$family_factory;
125    }
126
127    /**
128     * Store or retrieve a factory object.
129     *
130     * @param FilesystemFactoryInterface|null $factory
131     *
132     * @return FilesystemFactoryInterface
133     */
134    public static function filesystem(FilesystemFactoryInterface $factory = null): FilesystemFactoryInterface
135    {
136        if ($factory instanceof FilesystemFactoryInterface) {
137            self::$filesystem_factory = $factory;
138        }
139
140        return self::$filesystem_factory;
141    }
142
143    /**
144     * Store or retrieve a factory object.
145     *
146     * @param GedcomRecordFactoryInterface|null $factory
147     *
148     * @return GedcomRecordFactoryInterface
149     */
150    public static function gedcomRecordFactory(GedcomRecordFactoryInterface $factory = null): GedcomRecordFactoryInterface
151    {
152        if ($factory instanceof GedcomRecordFactoryInterface) {
153            self::$gedcom_record_factory = $factory;
154        }
155
156        return self::$gedcom_record_factory;
157    }
158
159    /**
160     * Store or retrieve a factory object.
161     *
162     * @param HeaderFactoryInterface|null $factory
163     *
164     * @return HeaderFactoryInterface
165     */
166    public static function headerFactory(HeaderFactoryInterface $factory = null): HeaderFactoryInterface
167    {
168        if ($factory instanceof HeaderFactoryInterface) {
169            self::$header_factory = $factory;
170        }
171
172        return self::$header_factory;
173    }
174
175    /**
176     * Store or retrieve a factory object.
177     *
178     * @param ImageFactoryInterface|null $factory
179     *
180     * @return ImageFactoryInterface
181     */
182    public static function imageFactory(ImageFactoryInterface $factory = null): ImageFactoryInterface
183    {
184        if ($factory instanceof ImageFactoryInterface) {
185            self::$image_factory = $factory;
186        }
187
188        return self::$image_factory;
189    }
190
191    /**
192     * Store or retrieve a factory object.
193     *
194     * @param IndividualFactoryInterface|null $factory
195     *
196     * @return IndividualFactoryInterface
197     */
198    public static function individualFactory(IndividualFactoryInterface $factory = null): IndividualFactoryInterface
199    {
200        if ($factory instanceof IndividualFactoryInterface) {
201            self::$individual_factory = $factory;
202        }
203
204        return self::$individual_factory;
205    }
206
207    /**
208     * Store or retrieve a factory object.
209     *
210     * @param LocationFactoryInterface|null $factory
211     *
212     * @return LocationFactoryInterface
213     */
214    public static function locationFactory(LocationFactoryInterface $factory = null): LocationFactoryInterface
215    {
216        if ($factory instanceof LocationFactoryInterface) {
217            self::$location_factory = $factory;
218        }
219
220        return self::$location_factory;
221    }
222
223    /**
224     * Store or retrieve a factory object.
225     *
226     * @param MediaFactoryInterface|null $factory
227     *
228     * @return MediaFactoryInterface
229     */
230    public static function mediaFactory(MediaFactoryInterface $factory = null): MediaFactoryInterface
231    {
232        if ($factory instanceof MediaFactoryInterface) {
233            self::$media_factory = $factory;
234        }
235
236        return self::$media_factory;
237    }
238
239    /**
240     * Store or retrieve a factory object.
241     *
242     * @param NoteFactoryInterface|null $factory
243     *
244     * @return NoteFactoryInterface
245     */
246    public static function noteFactory(NoteFactoryInterface $factory = null): NoteFactoryInterface
247    {
248        if ($factory instanceof NoteFactoryInterface) {
249            self::$note_factory = $factory;
250        }
251
252        return self::$note_factory;
253    }
254
255    /**
256     * Store or retrieve a factory object.
257     *
258     * @param RepositoryFactoryInterface|null $factory
259     *
260     * @return RepositoryFactoryInterface
261     */
262    public static function repositoryFactory(RepositoryFactoryInterface $factory = null): RepositoryFactoryInterface
263    {
264        if ($factory instanceof RepositoryFactoryInterface) {
265            self::$repository_factory = $factory;
266        }
267
268        return self::$repository_factory;
269    }
270
271    /**
272     * Store or retrieve a factory object.
273     *
274     * @param SlugFactoryInterface|null $factory
275     *
276     * @return SlugFactoryInterface
277     */
278    public static function slugFactory(SlugFactoryInterface $factory = null): SlugFactoryInterface
279    {
280        if ($factory instanceof SlugFactoryInterface) {
281            self::$slug_factory = $factory;
282        }
283
284        return self::$slug_factory;
285    }
286
287    /**
288     * Store or retrieve a factory object.
289     *
290     * @param SourceFactoryInterface|null $factory
291     *
292     * @return SourceFactoryInterface
293     */
294    public static function sourceFactory(SourceFactoryInterface $factory = null): SourceFactoryInterface
295    {
296        if ($factory instanceof SourceFactoryInterface) {
297            self::$source_factory = $factory;
298        }
299
300        return self::$source_factory;
301    }
302
303    /**
304     * Store or retrieve a factory object.
305     *
306     * @param SubmissionFactoryInterface|null $factory
307     *
308     * @return SubmissionFactoryInterface
309     */
310    public static function submissionFactory(SubmissionFactoryInterface $factory = null): SubmissionFactoryInterface
311    {
312        if ($factory instanceof SubmissionFactoryInterface) {
313            self::$submission_factory = $factory;
314        }
315
316        return self::$submission_factory;
317    }
318
319    /**
320     * Store or retrieve a factory object.
321     *
322     * @param SubmitterFactoryInterface|null $factory
323     *
324     * @return SubmitterFactoryInterface
325     */
326    public static function submitterFactory(SubmitterFactoryInterface $factory = null): SubmitterFactoryInterface
327    {
328        if ($factory instanceof SubmitterFactoryInterface) {
329            self::$submitter_factory = $factory;
330        }
331
332        return self::$submitter_factory;
333    }
334
335    /**
336     * Store or retrieve a factory object.
337     *
338     * @param XrefFactoryInterface|null $factory
339     *
340     * @return XrefFactoryInterface
341     */
342    public static function xrefFactory(XrefFactoryInterface $factory = null): XrefFactoryInterface
343    {
344        if ($factory instanceof XrefFactoryInterface) {
345            self::$xref_factory = $factory;
346        }
347
348        return self::$xref_factory;
349    }
350}
351