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