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