]> git.mxchange.org Git - friendica.git/blob - src/DI.php
Merge pull request #12604 from nupplaphil/bug/marco_r
[friendica.git] / src / DI.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica;
23
24 use Dice\Dice;
25 use Friendica\Core\Session\Capability\IHandleSessions;
26 use Friendica\Core\Session\Capability\IHandleUserSessions;
27 use Friendica\Navigation\SystemMessages;
28 use Psr\Log\LoggerInterface;
29
30 /**
31  * This class is capable of getting all dynamic created classes
32  *
33  * @see https://designpatternsphp.readthedocs.io/en/latest/Structural/Registry/README.html
34  */
35 abstract class DI
36 {
37         /** @var Dice */
38         private static $dice;
39
40         public static function init(Dice $dice)
41         {
42                 self::$dice = $dice;
43         }
44
45         /**
46          * Returns a clone of the current dice instance
47          * This usefull for overloading the current instance with mocked methods during tests
48          *
49          * @return Dice
50          */
51         public static function getDice()
52         {
53                 return clone self::$dice;
54         }
55
56         //
57         // common instances
58         //
59
60         /**
61          * @return App
62          */
63         public static function app()
64         {
65                 return self::$dice->create(App::class);
66         }
67
68         /**
69          * @return Database\Database
70          */
71         public static function dba(): Database\Database
72         {
73                 return self::$dice->create(Database\Database::class);
74         }
75
76         /**
77          * @return \Friendica\Database\Definition\DbaDefinition
78          */
79         public static function dbaDefinition(): Database\Definition\DbaDefinition
80         {
81                 return self::$dice->create(Database\Definition\DbaDefinition::class);
82         }
83
84         /**
85          * @return \Friendica\Database\Definition\ViewDefinition
86          */
87         public static function viewDefinition(): Database\Definition\ViewDefinition
88         {
89                 return self::$dice->create(Database\Definition\ViewDefinition::class);
90         }
91
92         //
93         // "App" namespace instances
94         //
95
96         /**
97          * @return App\Arguments
98          */
99         public static function args()
100         {
101                 return self::$dice->create(App\Arguments::class);
102         }
103
104         /**
105          * @return App\BaseURL
106          */
107         public static function baseUrl()
108         {
109                 return self::$dice->create(App\BaseURL::class);
110         }
111
112         /**
113          * @return App\Mode
114          */
115         public static function mode()
116         {
117                 return self::$dice->create(App\Mode::class);
118         }
119
120         /**
121          * @return App\Page
122          */
123         public static function page()
124         {
125                 return self::$dice->create(App\Page::class);
126         }
127
128         /**
129          * @return App\Router
130          */
131         public static function router()
132         {
133                 return self::$dice->create(App\Router::class);
134         }
135
136         //
137         // "Content" namespace instances
138         //
139
140         /**
141          * @return Content\Item
142          */
143         public static function contentItem()
144         {
145                 return self::$dice->create(Content\Item::class);
146         }
147
148         /**
149          * @return Content\Conversation
150          */
151         public static function conversation()
152         {
153                 return self::$dice->create(Content\Conversation::class);
154         }
155
156         /**
157          * @return Content\Text\BBCode\Video
158          */
159         public static function bbCodeVideo()
160         {
161                 return self::$dice->create(Content\Text\BBCode\Video::class);
162         }
163
164         //
165         // "Core" namespace instances
166         //
167
168         /**
169          * @return Core\Cache\Capability\ICanCache
170          */
171         public static function cache()
172         {
173                 return self::$dice->create(Core\Cache\Capability\ICanCache::class);
174         }
175
176         /**
177          * @return Core\Config\Capability\IManageConfigValues
178          */
179         public static function config()
180         {
181                 return self::$dice->create(Core\Config\Capability\IManageConfigValues::class);
182         }
183
184         public static function keyValue(): Core\KeyValueStorage\Capabilities\IManageKeyValuePairs
185         {
186                 return self::$dice->create(Core\KeyValueStorage\Capabilities\IManageKeyValuePairs::class);
187         }
188
189         /**
190          * @return Core\PConfig\Capability\IManagePersonalConfigValues
191          */
192         public static function pConfig()
193         {
194                 return self::$dice->create(Core\PConfig\Capability\IManagePersonalConfigValues::class);
195         }
196
197         /**
198          * @return Core\Lock\Capability\ICanLock
199          */
200         public static function lock()
201         {
202                 return self::$dice->create(Core\Lock\Capability\ICanLock::class);
203         }
204
205         /**
206          * @return Core\L10n
207          */
208         public static function l10n()
209         {
210                 return self::$dice->create(Core\L10n::class);
211         }
212
213         /**
214          * @return Core\Worker\Repository\Process
215          */
216         public static function process()
217         {
218                 return self::$dice->create(Core\Worker\Repository\Process::class);
219         }
220
221         public static function session(): IHandleSessions
222         {
223                 return self::$dice->create(Core\Session\Capability\IHandleSessions::class);
224         }
225
226         public static function userSession(): IHandleUserSessions
227         {
228                 return self::$dice->create(Core\Session\Capability\IHandleUserSessions::class);
229         }
230
231         /**
232          * @return \Friendica\Core\Storage\Repository\StorageManager
233          */
234         public static function storageManager()
235         {
236                 return self::$dice->create(Core\Storage\Repository\StorageManager::class);
237         }
238
239         /**
240          * @return \Friendica\Core\System
241          */
242         public static function system()
243         {
244                 return self::$dice->create(Core\System::class);
245         }
246
247         /**
248          * @return \Friendica\Navigation\SystemMessages
249          */
250         public static function sysmsg()
251         {
252                 return self::$dice->create(SystemMessages::class);
253         }
254
255         //
256         // "LoggerInterface" instances
257         //
258
259         /**
260          * Flushes the Logger instance, so the factory is called again
261          * (creates a new id and retrieves the current PID)
262          */
263         public static function flushLogger()
264         {
265                 $flushDice = self::$dice
266                         ->addRule(LoggerInterface::class, self::$dice->getRule(LoggerInterface::class))
267                         ->addRule('$devLogger', self::$dice->getRule('$devLogger'));
268                 static::init($flushDice);
269         }
270
271         /**
272          * @return LoggerInterface
273          */
274         public static function logger()
275         {
276                 return self::$dice->create(LoggerInterface::class);
277         }
278
279         /**
280          * @return LoggerInterface
281          */
282         public static function devLogger()
283         {
284                 return self::$dice->create('$devLogger');
285         }
286
287         /**
288          * @return LoggerInterface
289          */
290         public static function workerLogger()
291         {
292                 return self::$dice->create(Core\Logger\Type\WorkerLogger::class);
293         }
294
295         //
296         // "Factory" namespace instances
297         //
298
299         /**
300          * @return Factory\Api\Mastodon\Account
301          */
302         public static function mstdnAccount()
303         {
304                 return self::$dice->create(Factory\Api\Mastodon\Account::class);
305         }
306
307         /**
308          * @return Factory\Api\Mastodon\Application
309          */
310         public static function mstdnApplication()
311         {
312                 return self::$dice->create(Factory\Api\Mastodon\Application::class);
313         }
314
315         /**
316          * @return Factory\Api\Mastodon\Attachment
317          */
318         public static function mstdnAttachment()
319         {
320                 return self::$dice->create(Factory\Api\Mastodon\Attachment::class);
321         }
322
323         /**
324          * @return Factory\Api\Mastodon\Card
325          */
326         public static function mstdnCard()
327         {
328                 return self::$dice->create(Factory\Api\Mastodon\Card::class);
329         }
330
331         /**
332          * @return Factory\Api\Mastodon\Conversation
333          */
334         public static function mstdnConversation()
335         {
336                 return self::$dice->create(Factory\Api\Mastodon\Conversation::class);
337         }
338
339         /**
340          * @return Factory\Api\Mastodon\Emoji
341          */
342         public static function mstdnEmoji()
343         {
344                 return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
345         }
346
347         /**
348          * @return Factory\Api\Mastodon\Error
349          */
350         public static function mstdnError()
351         {
352                 return self::$dice->create(Factory\Api\Mastodon\Error::class);
353         }
354
355         /**
356          * @return Factory\Api\Mastodon\FollowRequest
357          */
358         public static function mstdnFollowRequest()
359         {
360                 return self::$dice->create(Factory\Api\Mastodon\FollowRequest::class);
361         }
362
363         /**
364          * @return Factory\Api\Mastodon\Poll
365          */
366         public static function mstdnPoll()
367         {
368                 return self::$dice->create(Factory\Api\Mastodon\Poll::class);
369         }
370
371         /**
372          * @return Factory\Api\Mastodon\Relationship
373          */
374         public static function mstdnRelationship()
375         {
376                 return self::$dice->create(Factory\Api\Mastodon\Relationship::class);
377         }
378
379         /**
380          * @return Factory\Api\Mastodon\Status
381          */
382         public static function mstdnStatus()
383         {
384                 return self::$dice->create(Factory\Api\Mastodon\Status::class);
385         }
386
387         /**
388          * @return Factory\Api\Mastodon\StatusSource
389          */
390         public static function mstdnStatusSource()
391         {
392                 return self::$dice->create(Factory\Api\Mastodon\StatusSource::class);
393         }
394
395         /**
396          * @return Factory\Api\Mastodon\ScheduledStatus
397          */
398         public static function mstdnScheduledStatus()
399         {
400                 return self::$dice->create(Factory\Api\Mastodon\ScheduledStatus::class);
401         }
402
403         /**
404          * @return Factory\Api\Mastodon\Subscription
405          */
406         public static function mstdnSubscription()
407         {
408                 return self::$dice->create(Factory\Api\Mastodon\Subscription::class);
409         }
410
411         /**
412          * @return Factory\Api\Mastodon\ListEntity
413          */
414         public static function mstdnList()
415         {
416                 return self::$dice->create(Factory\Api\Mastodon\ListEntity::class);
417         }
418
419         /**
420          * @return Factory\Api\Mastodon\Notification
421          */
422         public static function mstdnNotification()
423         {
424                 return self::$dice->create(Factory\Api\Mastodon\Notification::class);
425         }
426
427         /**
428          * @return Factory\Api\Twitter\Status
429          */
430         public static function twitterStatus()
431         {
432                 return self::$dice->create(Factory\Api\Twitter\Status::class);
433         }
434
435         /**
436          * @return Factory\Api\Twitter\User
437          */
438         public static function twitterUser()
439         {
440                 return self::$dice->create(Factory\Api\Twitter\User::class);
441         }
442
443         public static function notificationIntro(): Navigation\Notifications\Factory\Introduction
444         {
445                 return self::$dice->create(Navigation\Notifications\Factory\Introduction::class);
446         }
447
448         //
449         // "Model" namespace instances
450         //
451         /**
452          * @return \Friendica\Core\Worker\Repository\Process
453          */
454         public static function modelProcess()
455         {
456                 return self::$dice->create(Core\Worker\Repository\Process::class);
457         }
458
459         /**
460          * @return Model\User\Cookie
461          */
462         public static function cookie()
463         {
464                 return self::$dice->create(Model\User\Cookie::class);
465         }
466
467         /**
468          * @return Core\Storage\Capability\ICanWriteToStorage
469          */
470         public static function storage()
471         {
472                 return self::$dice->create(Core\Storage\Capability\ICanWriteToStorage::class);
473         }
474
475         /**
476          * @return Model\Log\ParsedLogIterator
477          */
478         public static function parsedLogIterator()
479         {
480                 return self::$dice->create(Model\Log\ParsedLogIterator::class);
481         }
482
483         //
484         // "Module" namespace
485         //
486
487         public static function apiResponse(): Module\Api\ApiResponse
488         {
489                 return self::$dice->create(Module\Api\ApiResponse::class);
490         }
491
492         //
493         // "Network" namespace
494         //
495
496         /**
497          * @return Network\HTTPClient\Capability\ICanSendHttpRequests
498          */
499         public static function httpClient()
500         {
501                 return self::$dice->create(Network\HTTPClient\Capability\ICanSendHttpRequests::class);
502         }
503
504         //
505         // "Repository" namespace
506         //
507
508         /**
509          * @return Contact\FriendSuggest\Repository\FriendSuggest;
510          */
511         public static function fsuggest()
512         {
513                 return self::$dice->create(Contact\FriendSuggest\Repository\FriendSuggest::class);
514         }
515
516         /**
517          * @return Contact\FriendSuggest\Factory\FriendSuggest;
518          */
519         public static function fsuggestFactory()
520         {
521                 return self::$dice->create(Contact\FriendSuggest\Factory\FriendSuggest::class);
522         }
523
524         /**
525          * @return Contact\Introduction\Repository\Introduction
526          */
527         public static function intro()
528         {
529                 return self::$dice->create(Contact\Introduction\Repository\Introduction::class);
530         }
531
532         /**
533          * @return Contact\Introduction\Factory\Introduction
534          */
535         public static function introFactory()
536         {
537                 return self::$dice->create(Contact\Introduction\Factory\Introduction::class);
538         }
539
540         public static function report(): Moderation\Repository\Report
541         {
542                 return self::$dice->create(Moderation\Repository\Report::class);
543         }
544
545         public static function reportFactory(): Moderation\Factory\Report
546         {
547                 return self::$dice->create(Moderation\Factory\Report::class);
548         }
549
550         public static function localRelationship(): Contact\LocalRelationship\Repository\LocalRelationship
551         {
552                 return self::$dice->create(Contact\LocalRelationship\Repository\LocalRelationship::class);
553         }
554
555         public static function permissionSet(): Security\PermissionSet\Repository\PermissionSet
556         {
557                 return self::$dice->create(Security\PermissionSet\Repository\PermissionSet::class);
558         }
559
560         public static function permissionSetFactory(): Security\PermissionSet\Factory\PermissionSet
561         {
562                 return self::$dice->create(Security\PermissionSet\Factory\PermissionSet::class);
563         }
564
565         public static function profileField(): Profile\ProfileField\Repository\ProfileField
566         {
567                 return self::$dice->create(Profile\ProfileField\Repository\ProfileField::class);
568         }
569
570         public static function profileFieldFactory(): Profile\ProfileField\Factory\ProfileField
571         {
572                 return self::$dice->create(Profile\ProfileField\Factory\ProfileField::class);
573         }
574
575         public static function notification(): Navigation\Notifications\Repository\Notification
576         {
577                 return self::$dice->create(Navigation\Notifications\Repository\Notification::class);
578         }
579
580         public static function notificationFactory(): Navigation\Notifications\Factory\Notification
581         {
582                 return self::$dice->create(Navigation\Notifications\Factory\Notification::class);
583         }
584
585         public static function notify(): Navigation\Notifications\Repository\Notify
586         {
587                 return self::$dice->create(Navigation\Notifications\Repository\Notify::class);
588         }
589
590         public static function notifyFactory(): Navigation\Notifications\Factory\Notify
591         {
592                 return self::$dice->create(Navigation\Notifications\Factory\Notify::class);
593         }
594
595         public static function formattedNotificationFactory(): Navigation\Notifications\Factory\FormattedNotify
596         {
597                 return self::$dice->create(Navigation\Notifications\Factory\FormattedNotify::class);
598         }
599
600         public static function formattedNavNotificationFactory(): Navigation\Notifications\Factory\FormattedNavNotification
601         {
602                 return self::$dice->create(Navigation\Notifications\Factory\FormattedNavNotification::class);
603         }
604
605         //
606         // "Federation" namespace instances
607         //
608
609         public static function deliveryQueueItemFactory(): Federation\Factory\DeliveryQueueItem
610         {
611                 return self::$dice->create(Federation\Factory\DeliveryQueueItem::class);
612         }
613
614         public static function deliveryQueueItemRepo(): Federation\Repository\DeliveryQueueItem
615         {
616                 return self::$dice->create(Federation\Repository\DeliveryQueueItem::class);
617         }
618
619         //
620         // "Protocol" namespace instances
621         //
622
623         /**
624          * @return Protocol\Activity
625          */
626         public static function activity()
627         {
628                 return self::$dice->create(Protocol\Activity::class);
629         }
630
631         public static function dsprContact(): Protocol\Diaspora\Repository\DiasporaContact
632         {
633                 return self::$dice->create(Protocol\Diaspora\Repository\DiasporaContact::class);
634         }
635
636         //
637         // "Security" namespace instances
638         //
639
640         /**
641          * @return \Friendica\Security\Authentication
642          */
643         public static function auth()
644         {
645                 return self::$dice->create(Security\Authentication::class);
646         }
647
648         //
649         // "Util" namespace instances
650         //
651
652         /**
653          * @return Util\ACLFormatter
654          */
655         public static function aclFormatter()
656         {
657                 return self::$dice->create(Util\ACLFormatter::class);
658         }
659
660         /**
661          * @return string
662          */
663         public static function basePath()
664         {
665                 return self::$dice->create('$basepath');
666         }
667
668         /**
669          * @return Util\DateTimeFormat
670          */
671         public static function dtFormat()
672         {
673                 return self::$dice->create(Util\DateTimeFormat::class);
674         }
675
676         /**
677          * @return Util\FileSystem
678          */
679         public static function fs()
680         {
681                 return self::$dice->create(Util\FileSystem::class);
682         }
683
684         /**
685          * @return Util\Profiler
686          */
687         public static function profiler()
688         {
689                 return self::$dice->create(Util\Profiler::class);
690         }
691
692         /**
693          * @return Util\Emailer
694          */
695         public static function emailer()
696         {
697                 return self::$dice->create(Util\Emailer::class);
698         }
699 }