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