3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
25 use Friendica\Core\Session\Capability\IHandleSessions;
26 use Friendica\Core\Session\Capability\IHandleUserSessions;
27 use Friendica\Navigation\SystemMessages;
28 use Psr\Log\LoggerInterface;
31 * This class is capable of getting all dynamic created classes
33 * @see https://designpatternsphp.readthedocs.io/en/latest/Structural/Registry/README.html
41 * Initialize the singleton DI container with the Dice instance
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.
49 public static function init(Dice $dice, bool $disableDepByHand = false)
53 if (!$disableDepByHand) {
54 self::setCompositeRootDependencyByHand();
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
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
64 public static function setCompositeRootDependencyByHand()
66 $database = static::dba();
67 $database->setDependency(static::config(), static::profiler(), static::logger());
71 * Returns a clone of the current dice instance
72 * This usefull for overloading the current instance with mocked methods during tests
76 public static function getDice()
78 return clone self::$dice;
88 public static function app()
90 return self::$dice->create(App::class);
94 * @return Database\Database
96 public static function dba(): Database\Database
98 return self::$dice->create(Database\Database::class);
102 * @return \Friendica\Database\Definition\DbaDefinition
104 public static function dbaDefinition(): Database\Definition\DbaDefinition
106 return self::$dice->create(Database\Definition\DbaDefinition::class);
110 * @return \Friendica\Database\Definition\ViewDefinition
112 public static function viewDefinition(): Database\Definition\ViewDefinition
114 return self::$dice->create(Database\Definition\ViewDefinition::class);
118 // "App" namespace instances
122 * @return App\Arguments
124 public static function args()
126 return self::$dice->create(App\Arguments::class);
130 * @return App\BaseURL
132 public static function baseUrl()
134 return self::$dice->create(App\BaseURL::class);
140 public static function mode()
142 return self::$dice->create(App\Mode::class);
148 public static function page()
150 return self::$dice->create(App\Page::class);
156 public static function router()
158 return self::$dice->create(App\Router::class);
162 // "Content" namespace instances
166 * @return Content\Item
168 public static function contentItem()
170 return self::$dice->create(Content\Item::class);
174 * @return Content\Conversation
176 public static function conversation()
178 return self::$dice->create(Content\Conversation::class);
182 * @return Content\Text\BBCode\Video
184 public static function bbCodeVideo()
186 return self::$dice->create(Content\Text\BBCode\Video::class);
190 // "Core" namespace instances
194 * @return Core\Cache\Capability\ICanCache
196 public static function cache()
198 return self::$dice->create(Core\Cache\Capability\ICanCache::class);
202 * @return Core\Config\Capability\IManageConfigValues
204 public static function config()
206 return self::$dice->create(Core\Config\Capability\IManageConfigValues::class);
209 public static function configFileManager(): Core\Config\Util\ConfigFileManager
211 return self::$dice->create(Core\Config\Util\ConfigFileManager::class);
214 public static function keyValue(): Core\KeyValueStorage\Capabilities\IManageKeyValuePairs
216 return self::$dice->create(Core\KeyValueStorage\Capabilities\IManageKeyValuePairs::class);
220 * @return Core\PConfig\Capability\IManagePersonalConfigValues
222 public static function pConfig()
224 return self::$dice->create(Core\PConfig\Capability\IManagePersonalConfigValues::class);
228 * @return Core\Lock\Capability\ICanLock
230 public static function lock()
232 return self::$dice->create(Core\Lock\Capability\ICanLock::class);
238 public static function l10n()
240 return self::$dice->create(Core\L10n::class);
244 * @return Core\Worker\Repository\Process
246 public static function process()
248 return self::$dice->create(Core\Worker\Repository\Process::class);
251 public static function session(): IHandleSessions
253 return self::$dice->create(Core\Session\Capability\IHandleSessions::class);
256 public static function userSession(): IHandleUserSessions
258 return self::$dice->create(Core\Session\Capability\IHandleUserSessions::class);
262 * @return \Friendica\Core\Storage\Repository\StorageManager
264 public static function storageManager()
266 return self::$dice->create(Core\Storage\Repository\StorageManager::class);
270 * @return \Friendica\Core\System
272 public static function system()
274 return self::$dice->create(Core\System::class);
278 * @return \Friendica\Navigation\SystemMessages
280 public static function sysmsg()
282 return self::$dice->create(SystemMessages::class);
286 // "LoggerInterface" instances
290 * Flushes the Logger instance, so the factory is called again
291 * (creates a new id and retrieves the current PID)
293 public static function flushLogger()
295 $flushDice = self::$dice
296 ->addRule(LoggerInterface::class, self::$dice->getRule(LoggerInterface::class))
297 ->addRule('$devLogger', self::$dice->getRule('$devLogger'));
298 static::init($flushDice);
302 * @return LoggerInterface
304 public static function logger()
306 return self::$dice->create(LoggerInterface::class);
310 * @return LoggerInterface
312 public static function devLogger()
314 return self::$dice->create('$devLogger');
318 * @return LoggerInterface
320 public static function workerLogger()
322 return self::$dice->create(Core\Logger\Type\WorkerLogger::class);
326 // "Factory" namespace instances
330 * @return Factory\Api\Mastodon\Account
332 public static function mstdnAccount()
334 return self::$dice->create(Factory\Api\Mastodon\Account::class);
338 * @return Factory\Api\Mastodon\Application
340 public static function mstdnApplication()
342 return self::$dice->create(Factory\Api\Mastodon\Application::class);
346 * @return Factory\Api\Mastodon\Attachment
348 public static function mstdnAttachment()
350 return self::$dice->create(Factory\Api\Mastodon\Attachment::class);
354 * @return Factory\Api\Mastodon\Card
356 public static function mstdnCard()
358 return self::$dice->create(Factory\Api\Mastodon\Card::class);
362 * @return Factory\Api\Mastodon\Conversation
364 public static function mstdnConversation()
366 return self::$dice->create(Factory\Api\Mastodon\Conversation::class);
370 * @return Factory\Api\Mastodon\Emoji
372 public static function mstdnEmoji()
374 return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
378 * @return Factory\Api\Mastodon\Error
380 public static function mstdnError()
382 return self::$dice->create(Factory\Api\Mastodon\Error::class);
386 * @return Factory\Api\Mastodon\Poll
388 public static function mstdnPoll()
390 return self::$dice->create(Factory\Api\Mastodon\Poll::class);
394 * @return Factory\Api\Mastodon\Relationship
396 public static function mstdnRelationship()
398 return self::$dice->create(Factory\Api\Mastodon\Relationship::class);
402 * @return Factory\Api\Mastodon\Status
404 public static function mstdnStatus()
406 return self::$dice->create(Factory\Api\Mastodon\Status::class);
410 * @return Factory\Api\Mastodon\StatusSource
412 public static function mstdnStatusSource()
414 return self::$dice->create(Factory\Api\Mastodon\StatusSource::class);
418 * @return Factory\Api\Mastodon\ScheduledStatus
420 public static function mstdnScheduledStatus()
422 return self::$dice->create(Factory\Api\Mastodon\ScheduledStatus::class);
426 * @return Factory\Api\Mastodon\Subscription
428 public static function mstdnSubscription()
430 return self::$dice->create(Factory\Api\Mastodon\Subscription::class);
434 * @return Factory\Api\Mastodon\ListEntity
436 public static function mstdnList()
438 return self::$dice->create(Factory\Api\Mastodon\ListEntity::class);
442 * @return Factory\Api\Mastodon\Notification
444 public static function mstdnNotification()
446 return self::$dice->create(Factory\Api\Mastodon\Notification::class);
450 * @return Factory\Api\Twitter\Status
452 public static function twitterStatus()
454 return self::$dice->create(Factory\Api\Twitter\Status::class);
458 * @return Factory\Api\Twitter\User
460 public static function twitterUser()
462 return self::$dice->create(Factory\Api\Twitter\User::class);
465 public static function notificationIntro(): Navigation\Notifications\Factory\Introduction
467 return self::$dice->create(Navigation\Notifications\Factory\Introduction::class);
471 // "Model" namespace instances
474 * @return \Friendica\Core\Worker\Repository\Process
476 public static function modelProcess()
478 return self::$dice->create(Core\Worker\Repository\Process::class);
482 * @return Model\User\Cookie
484 public static function cookie()
486 return self::$dice->create(Model\User\Cookie::class);
490 * @return Core\Storage\Capability\ICanWriteToStorage
492 public static function storage()
494 return self::$dice->create(Core\Storage\Capability\ICanWriteToStorage::class);
498 * @return Model\Log\ParsedLogIterator
500 public static function parsedLogIterator()
502 return self::$dice->create(Model\Log\ParsedLogIterator::class);
506 // "Module" namespace
509 public static function apiResponse(): Module\Api\ApiResponse
511 return self::$dice->create(Module\Api\ApiResponse::class);
515 // "Network" namespace
519 * @return Network\HTTPClient\Capability\ICanSendHttpRequests
521 public static function httpClient()
523 return self::$dice->create(Network\HTTPClient\Capability\ICanSendHttpRequests::class);
527 // "Repository" namespace
531 * @return Contact\FriendSuggest\Repository\FriendSuggest;
533 public static function fsuggest()
535 return self::$dice->create(Contact\FriendSuggest\Repository\FriendSuggest::class);
539 * @return Contact\FriendSuggest\Factory\FriendSuggest;
541 public static function fsuggestFactory()
543 return self::$dice->create(Contact\FriendSuggest\Factory\FriendSuggest::class);
547 * @return Contact\Introduction\Repository\Introduction
549 public static function intro()
551 return self::$dice->create(Contact\Introduction\Repository\Introduction::class);
555 * @return Contact\Introduction\Factory\Introduction
557 public static function introFactory()
559 return self::$dice->create(Contact\Introduction\Factory\Introduction::class);
562 public static function report(): Moderation\Repository\Report
564 return self::$dice->create(Moderation\Repository\Report::class);
567 public static function reportFactory(): Moderation\Factory\Report
569 return self::$dice->create(Moderation\Factory\Report::class);
572 public static function localRelationship(): Contact\LocalRelationship\Repository\LocalRelationship
574 return self::$dice->create(Contact\LocalRelationship\Repository\LocalRelationship::class);
577 public static function permissionSet(): Security\PermissionSet\Repository\PermissionSet
579 return self::$dice->create(Security\PermissionSet\Repository\PermissionSet::class);
582 public static function permissionSetFactory(): Security\PermissionSet\Factory\PermissionSet
584 return self::$dice->create(Security\PermissionSet\Factory\PermissionSet::class);
587 public static function profileField(): Profile\ProfileField\Repository\ProfileField
589 return self::$dice->create(Profile\ProfileField\Repository\ProfileField::class);
592 public static function profileFieldFactory(): Profile\ProfileField\Factory\ProfileField
594 return self::$dice->create(Profile\ProfileField\Factory\ProfileField::class);
597 public static function notification(): Navigation\Notifications\Repository\Notification
599 return self::$dice->create(Navigation\Notifications\Repository\Notification::class);
602 public static function notificationFactory(): Navigation\Notifications\Factory\Notification
604 return self::$dice->create(Navigation\Notifications\Factory\Notification::class);
607 public static function notify(): Navigation\Notifications\Repository\Notify
609 return self::$dice->create(Navigation\Notifications\Repository\Notify::class);
612 public static function notifyFactory(): Navigation\Notifications\Factory\Notify
614 return self::$dice->create(Navigation\Notifications\Factory\Notify::class);
617 public static function formattedNotificationFactory(): Navigation\Notifications\Factory\FormattedNotify
619 return self::$dice->create(Navigation\Notifications\Factory\FormattedNotify::class);
622 public static function formattedNavNotificationFactory(): Navigation\Notifications\Factory\FormattedNavNotification
624 return self::$dice->create(Navigation\Notifications\Factory\FormattedNavNotification::class);
628 // "Federation" namespace instances
631 public static function deliveryQueueItemFactory(): Federation\Factory\DeliveryQueueItem
633 return self::$dice->create(Federation\Factory\DeliveryQueueItem::class);
636 public static function deliveryQueueItemRepo(): Federation\Repository\DeliveryQueueItem
638 return self::$dice->create(Federation\Repository\DeliveryQueueItem::class);
642 // "Protocol" namespace instances
646 * @return Protocol\Activity
648 public static function activity()
650 return self::$dice->create(Protocol\Activity::class);
653 public static function dsprContact(): Protocol\Diaspora\Repository\DiasporaContact
655 return self::$dice->create(Protocol\Diaspora\Repository\DiasporaContact::class);
659 // "Security" namespace instances
663 * @return \Friendica\Security\Authentication
665 public static function auth()
667 return self::$dice->create(Security\Authentication::class);
671 // "Util" namespace instances
675 * @return Util\ACLFormatter
677 public static function aclFormatter()
679 return self::$dice->create(Util\ACLFormatter::class);
685 public static function basePath()
687 return self::$dice->create('$basepath');
691 * @return Util\DateTimeFormat
693 public static function dtFormat()
695 return self::$dice->create(Util\DateTimeFormat::class);
699 * @return Util\FileSystem
701 public static function fs()
703 return self::$dice->create(Util\FileSystem::class);
707 * @return Util\Profiler
709 public static function profiler()
711 return self::$dice->create(Util\Profiler::class);
715 * @return Util\Emailer
717 public static function emailer()
719 return self::$dice->create(Util\Emailer::class);