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\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;
33 * This class is capable of getting all dynamic created classes
35 * @see https://designpatternsphp.readthedocs.io/en/latest/Structural/Registry/README.html
43 * Initialize the singleton DI container with the Dice instance
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.
51 public static function init(Dice $dice, bool $disableDepByHand = false)
55 if (!$disableDepByHand) {
56 self::setCompositeRootDependencyByHand();
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
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
66 public static function setCompositeRootDependencyByHand()
68 $database = static::dba();
69 $database->setDependency(static::config(), static::profiler(), static::logger());
73 * Returns a clone of the current dice instance
74 * This useful for overloading the current instance with mocked methods during tests
78 public static function getDice()
80 return clone self::$dice;
90 public static function app()
92 return self::$dice->create(App::class);
96 * @return Database\Database
98 public static function dba(): Database\Database
100 return self::$dice->create(Database\Database::class);
104 * @return \Friendica\Database\Definition\DbaDefinition
106 public static function dbaDefinition(): Database\Definition\DbaDefinition
108 return self::$dice->create(Database\Definition\DbaDefinition::class);
112 * @return \Friendica\Database\Definition\ViewDefinition
114 public static function viewDefinition(): Database\Definition\ViewDefinition
116 return self::$dice->create(Database\Definition\ViewDefinition::class);
120 // "App" namespace instances
124 * @return App\Arguments
126 public static function args()
128 return self::$dice->create(App\Arguments::class);
131 public static function baseUrl(): App\BaseURL
133 return self::$dice->create(App\BaseURL::class);
139 public static function mode()
141 return self::$dice->create(App\Mode::class);
147 public static function page()
149 return self::$dice->create(App\Page::class);
155 public static function router()
157 return self::$dice->create(App\Router::class);
161 // "Content" namespace instances
165 * @return Content\Item
167 public static function contentItem()
169 return self::$dice->create(Content\Item::class);
173 * @return Content\Conversation
175 public static function conversation()
177 return self::$dice->create(Content\Conversation::class);
181 * @return Content\Text\BBCode\Video
183 public static function bbCodeVideo()
185 return self::$dice->create(Content\Text\BBCode\Video::class);
189 // "Core" namespace instances
193 * @return Core\Cache\Capability\ICanCache
195 public static function cache()
197 return self::$dice->create(Core\Cache\Capability\ICanCache::class);
201 * @return Core\Config\Capability\IManageConfigValues
203 public static function config()
205 return self::$dice->create(Core\Config\Capability\IManageConfigValues::class);
208 public static function configFileManager(): Core\Config\Util\ConfigFileManager
210 return self::$dice->create(Core\Config\Util\ConfigFileManager::class);
213 public static function keyValue(): Core\KeyValueStorage\Capability\IManageKeyValuePairs
215 return self::$dice->create(Core\KeyValueStorage\Capability\IManageKeyValuePairs::class);
219 * @return Core\PConfig\Capability\IManagePersonalConfigValues
221 public static function pConfig()
223 return self::$dice->create(Core\PConfig\Capability\IManagePersonalConfigValues::class);
227 * @return Core\Lock\Capability\ICanLock
229 public static function lock()
231 return self::$dice->create(Core\Lock\Capability\ICanLock::class);
237 public static function l10n()
239 return self::$dice->create(Core\L10n::class);
243 * @return Core\Worker\Repository\Process
245 public static function process()
247 return self::$dice->create(Core\Worker\Repository\Process::class);
250 public static function session(): IHandleSessions
252 return self::$dice->create(Core\Session\Capability\IHandleSessions::class);
255 public static function userSession(): IHandleUserSessions
257 return self::$dice->create(Core\Session\Capability\IHandleUserSessions::class);
261 * @return \Friendica\Core\Storage\Repository\StorageManager
263 public static function storageManager()
265 return self::$dice->create(Core\Storage\Repository\StorageManager::class);
269 * @return \Friendica\Core\System
271 public static function system()
273 return self::$dice->create(Core\System::class);
277 * @return \Friendica\Navigation\SystemMessages
279 public static function sysmsg()
281 return self::$dice->create(SystemMessages::class);
285 // "LoggerInterface" instances
289 * Flushes the Logger instance, so the factory is called again
290 * (creates a new id and retrieves the current PID)
292 public static function flushLogger()
294 $flushDice = self::$dice
295 ->addRule(LoggerInterface::class, self::$dice->getRule(LoggerInterface::class))
296 ->addRule('$devLogger', self::$dice->getRule('$devLogger'));
297 static::init($flushDice);
300 public static function logCheck(): ICheckLoggerSettings
302 return self::$dice->create(LoggerSettingsCheck::class);
306 * @return LoggerInterface
308 public static function logger()
310 return self::$dice->create(LoggerInterface::class);
314 * @return LoggerInterface
316 public static function devLogger()
318 return self::$dice->create('$devLogger');
322 * @return LoggerInterface
324 public static function workerLogger()
326 return self::$dice->create(Core\Logger\Type\WorkerLogger::class);
330 // "Factory" namespace instances
334 * @return Factory\Api\Mastodon\Account
336 public static function mstdnAccount()
338 return self::$dice->create(Factory\Api\Mastodon\Account::class);
342 * @return Factory\Api\Mastodon\Application
344 public static function mstdnApplication()
346 return self::$dice->create(Factory\Api\Mastodon\Application::class);
350 * @return Factory\Api\Mastodon\Attachment
352 public static function mstdnAttachment()
354 return self::$dice->create(Factory\Api\Mastodon\Attachment::class);
358 * @return Factory\Api\Mastodon\Card
360 public static function mstdnCard()
362 return self::$dice->create(Factory\Api\Mastodon\Card::class);
366 * @return Factory\Api\Mastodon\Conversation
368 public static function mstdnConversation()
370 return self::$dice->create(Factory\Api\Mastodon\Conversation::class);
374 * @return Factory\Api\Mastodon\Emoji
376 public static function mstdnEmoji()
378 return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
382 * @return Factory\Api\Mastodon\Error
384 public static function mstdnError()
386 return self::$dice->create(Factory\Api\Mastodon\Error::class);
390 * @return Factory\Api\Mastodon\Poll
392 public static function mstdnPoll()
394 return self::$dice->create(Factory\Api\Mastodon\Poll::class);
398 * @return Factory\Api\Mastodon\Relationship
400 public static function mstdnRelationship()
402 return self::$dice->create(Factory\Api\Mastodon\Relationship::class);
406 * @return Factory\Api\Mastodon\Status
408 public static function mstdnStatus()
410 return self::$dice->create(Factory\Api\Mastodon\Status::class);
414 * @return Factory\Api\Mastodon\StatusSource
416 public static function mstdnStatusSource()
418 return self::$dice->create(Factory\Api\Mastodon\StatusSource::class);
422 * @return Factory\Api\Mastodon\ScheduledStatus
424 public static function mstdnScheduledStatus()
426 return self::$dice->create(Factory\Api\Mastodon\ScheduledStatus::class);
430 * @return Factory\Api\Mastodon\Subscription
432 public static function mstdnSubscription()
434 return self::$dice->create(Factory\Api\Mastodon\Subscription::class);
438 * @return Factory\Api\Mastodon\ListEntity
440 public static function mstdnList()
442 return self::$dice->create(Factory\Api\Mastodon\ListEntity::class);
446 * @return Factory\Api\Mastodon\Notification
448 public static function mstdnNotification()
450 return self::$dice->create(Factory\Api\Mastodon\Notification::class);
454 * @return Factory\Api\Twitter\Status
456 public static function twitterStatus()
458 return self::$dice->create(Factory\Api\Twitter\Status::class);
462 * @return Factory\Api\Twitter\User
464 public static function twitterUser()
466 return self::$dice->create(Factory\Api\Twitter\User::class);
469 public static function notificationIntro(): Navigation\Notifications\Factory\Introduction
471 return self::$dice->create(Navigation\Notifications\Factory\Introduction::class);
475 // "Model" namespace instances
478 * @return \Friendica\Core\Worker\Repository\Process
480 public static function modelProcess()
482 return self::$dice->create(Core\Worker\Repository\Process::class);
486 * @return Model\User\Cookie
488 public static function cookie()
490 return self::$dice->create(Model\User\Cookie::class);
494 * @return Core\Storage\Capability\ICanWriteToStorage
496 public static function storage()
498 return self::$dice->create(Core\Storage\Capability\ICanWriteToStorage::class);
502 * @return Model\Log\ParsedLogIterator
504 public static function parsedLogIterator()
506 return self::$dice->create(Model\Log\ParsedLogIterator::class);
510 // "Module" namespace
513 public static function apiResponse(): Module\Api\ApiResponse
515 return self::$dice->create(Module\Api\ApiResponse::class);
519 // "Network" namespace
523 * @return Network\HTTPClient\Capability\ICanSendHttpRequests
525 public static function httpClient()
527 return self::$dice->create(Network\HTTPClient\Capability\ICanSendHttpRequests::class);
531 // "Repository" namespace
535 * @return Contact\FriendSuggest\Repository\FriendSuggest;
537 public static function fsuggest()
539 return self::$dice->create(Contact\FriendSuggest\Repository\FriendSuggest::class);
543 * @return Contact\FriendSuggest\Factory\FriendSuggest;
545 public static function fsuggestFactory()
547 return self::$dice->create(Contact\FriendSuggest\Factory\FriendSuggest::class);
551 * @return Content\Conversation\Factory\Timeline
553 public static function TimelineFactory()
555 return self::$dice->create(Content\Conversation\Factory\Timeline::class);
559 * @return Contact\Introduction\Repository\Introduction
561 public static function intro()
563 return self::$dice->create(Contact\Introduction\Repository\Introduction::class);
567 * @return Contact\Introduction\Factory\Introduction
569 public static function introFactory()
571 return self::$dice->create(Contact\Introduction\Factory\Introduction::class);
574 public static function report(): Moderation\Repository\Report
576 return self::$dice->create(Moderation\Repository\Report::class);
579 public static function reportFactory(): Moderation\Factory\Report
581 return self::$dice->create(Moderation\Factory\Report::class);
584 public static function localRelationship(): Contact\LocalRelationship\Repository\LocalRelationship
586 return self::$dice->create(Contact\LocalRelationship\Repository\LocalRelationship::class);
589 public static function permissionSet(): Security\PermissionSet\Repository\PermissionSet
591 return self::$dice->create(Security\PermissionSet\Repository\PermissionSet::class);
594 public static function permissionSetFactory(): Security\PermissionSet\Factory\PermissionSet
596 return self::$dice->create(Security\PermissionSet\Factory\PermissionSet::class);
599 public static function profileField(): Profile\ProfileField\Repository\ProfileField
601 return self::$dice->create(Profile\ProfileField\Repository\ProfileField::class);
604 public static function profileFieldFactory(): Profile\ProfileField\Factory\ProfileField
606 return self::$dice->create(Profile\ProfileField\Factory\ProfileField::class);
609 public static function notification(): Navigation\Notifications\Repository\Notification
611 return self::$dice->create(Navigation\Notifications\Repository\Notification::class);
614 public static function notificationFactory(): Navigation\Notifications\Factory\Notification
616 return self::$dice->create(Navigation\Notifications\Factory\Notification::class);
619 public static function notify(): Navigation\Notifications\Repository\Notify
621 return self::$dice->create(Navigation\Notifications\Repository\Notify::class);
624 public static function notifyFactory(): Navigation\Notifications\Factory\Notify
626 return self::$dice->create(Navigation\Notifications\Factory\Notify::class);
629 public static function formattedNotificationFactory(): Navigation\Notifications\Factory\FormattedNotify
631 return self::$dice->create(Navigation\Notifications\Factory\FormattedNotify::class);
634 public static function formattedNavNotificationFactory(): Navigation\Notifications\Factory\FormattedNavNotification
636 return self::$dice->create(Navigation\Notifications\Factory\FormattedNavNotification::class);
640 // "Federation" namespace instances
643 public static function deliveryQueueItemFactory(): Federation\Factory\DeliveryQueueItem
645 return self::$dice->create(Federation\Factory\DeliveryQueueItem::class);
648 public static function deliveryQueueItemRepo(): Federation\Repository\DeliveryQueueItem
650 return self::$dice->create(Federation\Repository\DeliveryQueueItem::class);
654 // "Protocol" namespace instances
658 * @return Protocol\Activity
660 public static function activity()
662 return self::$dice->create(Protocol\Activity::class);
665 public static function dsprContact(): Protocol\Diaspora\Repository\DiasporaContact
667 return self::$dice->create(Protocol\Diaspora\Repository\DiasporaContact::class);
671 // "Security" namespace instances
675 * @return \Friendica\Security\Authentication
677 public static function auth()
679 return self::$dice->create(Security\Authentication::class);
683 // "User" namespace instances
686 public static function userGServer(): User\Settings\Repository\UserGServer
688 return self::$dice->create(User\Settings\Repository\UserGServer::class);
692 // "Util" namespace instances
696 * @return Util\ACLFormatter
698 public static function aclFormatter()
700 return self::$dice->create(Util\ACLFormatter::class);
706 public static function basePath()
708 return self::$dice->create('$basepath');
712 * @return Util\DateTimeFormat
714 public static function dtFormat()
716 return self::$dice->create(Util\DateTimeFormat::class);
720 * @return Util\Profiler
722 public static function profiler()
724 return self::$dice->create(Util\Profiler::class);
728 * @return Util\Emailer
730 public static function emailer()
732 return self::$dice->create(Util\Emailer::class);