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