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