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);
129 public static function baseUrl(): App\BaseURL
131 return self::$dice->create(App\BaseURL::class);
137 public static function mode()
139 return self::$dice->create(App\Mode::class);
145 public static function page()
147 return self::$dice->create(App\Page::class);
153 public static function router()
155 return self::$dice->create(App\Router::class);
159 // "Content" namespace instances
163 * @return Content\Item
165 public static function contentItem()
167 return self::$dice->create(Content\Item::class);
171 * @return Content\Conversation
173 public static function conversation()
175 return self::$dice->create(Content\Conversation::class);
179 * @return Content\Text\BBCode\Video
181 public static function bbCodeVideo()
183 return self::$dice->create(Content\Text\BBCode\Video::class);
187 // "Core" namespace instances
191 * @return Core\Cache\Capability\ICanCache
193 public static function cache()
195 return self::$dice->create(Core\Cache\Capability\ICanCache::class);
199 * @return Core\Config\Capability\IManageConfigValues
201 public static function config()
203 return self::$dice->create(Core\Config\Capability\IManageConfigValues::class);
206 public static function configFileManager(): Core\Config\Util\ConfigFileManager
208 return self::$dice->create(Core\Config\Util\ConfigFileManager::class);
211 public static function keyValue(): Core\KeyValueStorage\Capabilities\IManageKeyValuePairs
213 return self::$dice->create(Core\KeyValueStorage\Capabilities\IManageKeyValuePairs::class);
217 * @return Core\PConfig\Capability\IManagePersonalConfigValues
219 public static function pConfig()
221 return self::$dice->create(Core\PConfig\Capability\IManagePersonalConfigValues::class);
225 * @return Core\Lock\Capability\ICanLock
227 public static function lock()
229 return self::$dice->create(Core\Lock\Capability\ICanLock::class);
235 public static function l10n()
237 return self::$dice->create(Core\L10n::class);
241 * @return Core\Worker\Repository\Process
243 public static function process()
245 return self::$dice->create(Core\Worker\Repository\Process::class);
248 public static function session(): IHandleSessions
250 return self::$dice->create(Core\Session\Capability\IHandleSessions::class);
253 public static function userSession(): IHandleUserSessions
255 return self::$dice->create(Core\Session\Capability\IHandleUserSessions::class);
259 * @return \Friendica\Core\Storage\Repository\StorageManager
261 public static function storageManager()
263 return self::$dice->create(Core\Storage\Repository\StorageManager::class);
267 * @return \Friendica\Core\System
269 public static function system()
271 return self::$dice->create(Core\System::class);
275 * @return \Friendica\Navigation\SystemMessages
277 public static function sysmsg()
279 return self::$dice->create(SystemMessages::class);
283 // "LoggerInterface" instances
287 * Flushes the Logger instance, so the factory is called again
288 * (creates a new id and retrieves the current PID)
290 public static function flushLogger()
292 $flushDice = self::$dice
293 ->addRule(LoggerInterface::class, self::$dice->getRule(LoggerInterface::class))
294 ->addRule('$devLogger', self::$dice->getRule('$devLogger'));
295 static::init($flushDice);
299 * @return LoggerInterface
301 public static function logger()
303 return self::$dice->create(LoggerInterface::class);
307 * @return LoggerInterface
309 public static function devLogger()
311 return self::$dice->create('$devLogger');
315 * @return LoggerInterface
317 public static function workerLogger()
319 return self::$dice->create(Core\Logger\Type\WorkerLogger::class);
323 // "Factory" namespace instances
327 * @return Factory\Api\Mastodon\Account
329 public static function mstdnAccount()
331 return self::$dice->create(Factory\Api\Mastodon\Account::class);
335 * @return Factory\Api\Mastodon\Application
337 public static function mstdnApplication()
339 return self::$dice->create(Factory\Api\Mastodon\Application::class);
343 * @return Factory\Api\Mastodon\Attachment
345 public static function mstdnAttachment()
347 return self::$dice->create(Factory\Api\Mastodon\Attachment::class);
351 * @return Factory\Api\Mastodon\Card
353 public static function mstdnCard()
355 return self::$dice->create(Factory\Api\Mastodon\Card::class);
359 * @return Factory\Api\Mastodon\Conversation
361 public static function mstdnConversation()
363 return self::$dice->create(Factory\Api\Mastodon\Conversation::class);
367 * @return Factory\Api\Mastodon\Emoji
369 public static function mstdnEmoji()
371 return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
375 * @return Factory\Api\Mastodon\Error
377 public static function mstdnError()
379 return self::$dice->create(Factory\Api\Mastodon\Error::class);
383 * @return Factory\Api\Mastodon\Poll
385 public static function mstdnPoll()
387 return self::$dice->create(Factory\Api\Mastodon\Poll::class);
391 * @return Factory\Api\Mastodon\Relationship
393 public static function mstdnRelationship()
395 return self::$dice->create(Factory\Api\Mastodon\Relationship::class);
399 * @return Factory\Api\Mastodon\Status
401 public static function mstdnStatus()
403 return self::$dice->create(Factory\Api\Mastodon\Status::class);
407 * @return Factory\Api\Mastodon\StatusSource
409 public static function mstdnStatusSource()
411 return self::$dice->create(Factory\Api\Mastodon\StatusSource::class);
415 * @return Factory\Api\Mastodon\ScheduledStatus
417 public static function mstdnScheduledStatus()
419 return self::$dice->create(Factory\Api\Mastodon\ScheduledStatus::class);
423 * @return Factory\Api\Mastodon\Subscription
425 public static function mstdnSubscription()
427 return self::$dice->create(Factory\Api\Mastodon\Subscription::class);
431 * @return Factory\Api\Mastodon\ListEntity
433 public static function mstdnList()
435 return self::$dice->create(Factory\Api\Mastodon\ListEntity::class);
439 * @return Factory\Api\Mastodon\Notification
441 public static function mstdnNotification()
443 return self::$dice->create(Factory\Api\Mastodon\Notification::class);
447 * @return Factory\Api\Twitter\Status
449 public static function twitterStatus()
451 return self::$dice->create(Factory\Api\Twitter\Status::class);
455 * @return Factory\Api\Twitter\User
457 public static function twitterUser()
459 return self::$dice->create(Factory\Api\Twitter\User::class);
462 public static function notificationIntro(): Navigation\Notifications\Factory\Introduction
464 return self::$dice->create(Navigation\Notifications\Factory\Introduction::class);
468 // "Model" namespace instances
471 * @return \Friendica\Core\Worker\Repository\Process
473 public static function modelProcess()
475 return self::$dice->create(Core\Worker\Repository\Process::class);
479 * @return Model\User\Cookie
481 public static function cookie()
483 return self::$dice->create(Model\User\Cookie::class);
487 * @return Core\Storage\Capability\ICanWriteToStorage
489 public static function storage()
491 return self::$dice->create(Core\Storage\Capability\ICanWriteToStorage::class);
495 * @return Model\Log\ParsedLogIterator
497 public static function parsedLogIterator()
499 return self::$dice->create(Model\Log\ParsedLogIterator::class);
503 // "Module" namespace
506 public static function apiResponse(): Module\Api\ApiResponse
508 return self::$dice->create(Module\Api\ApiResponse::class);
512 // "Network" namespace
516 * @return Network\HTTPClient\Capability\ICanSendHttpRequests
518 public static function httpClient()
520 return self::$dice->create(Network\HTTPClient\Capability\ICanSendHttpRequests::class);
524 // "Repository" namespace
528 * @return Contact\FriendSuggest\Repository\FriendSuggest;
530 public static function fsuggest()
532 return self::$dice->create(Contact\FriendSuggest\Repository\FriendSuggest::class);
536 * @return Contact\FriendSuggest\Factory\FriendSuggest;
538 public static function fsuggestFactory()
540 return self::$dice->create(Contact\FriendSuggest\Factory\FriendSuggest::class);
544 * @return Contact\Introduction\Repository\Introduction
546 public static function intro()
548 return self::$dice->create(Contact\Introduction\Repository\Introduction::class);
552 * @return Contact\Introduction\Factory\Introduction
554 public static function introFactory()
556 return self::$dice->create(Contact\Introduction\Factory\Introduction::class);
559 public static function report(): Moderation\Repository\Report
561 return self::$dice->create(Moderation\Repository\Report::class);
564 public static function reportFactory(): Moderation\Factory\Report
566 return self::$dice->create(Moderation\Factory\Report::class);
569 public static function localRelationship(): Contact\LocalRelationship\Repository\LocalRelationship
571 return self::$dice->create(Contact\LocalRelationship\Repository\LocalRelationship::class);
574 public static function permissionSet(): Security\PermissionSet\Repository\PermissionSet
576 return self::$dice->create(Security\PermissionSet\Repository\PermissionSet::class);
579 public static function permissionSetFactory(): Security\PermissionSet\Factory\PermissionSet
581 return self::$dice->create(Security\PermissionSet\Factory\PermissionSet::class);
584 public static function profileField(): Profile\ProfileField\Repository\ProfileField
586 return self::$dice->create(Profile\ProfileField\Repository\ProfileField::class);
589 public static function profileFieldFactory(): Profile\ProfileField\Factory\ProfileField
591 return self::$dice->create(Profile\ProfileField\Factory\ProfileField::class);
594 public static function notification(): Navigation\Notifications\Repository\Notification
596 return self::$dice->create(Navigation\Notifications\Repository\Notification::class);
599 public static function notificationFactory(): Navigation\Notifications\Factory\Notification
601 return self::$dice->create(Navigation\Notifications\Factory\Notification::class);
604 public static function notify(): Navigation\Notifications\Repository\Notify
606 return self::$dice->create(Navigation\Notifications\Repository\Notify::class);
609 public static function notifyFactory(): Navigation\Notifications\Factory\Notify
611 return self::$dice->create(Navigation\Notifications\Factory\Notify::class);
614 public static function formattedNotificationFactory(): Navigation\Notifications\Factory\FormattedNotify
616 return self::$dice->create(Navigation\Notifications\Factory\FormattedNotify::class);
619 public static function formattedNavNotificationFactory(): Navigation\Notifications\Factory\FormattedNavNotification
621 return self::$dice->create(Navigation\Notifications\Factory\FormattedNavNotification::class);
625 // "Federation" namespace instances
628 public static function deliveryQueueItemFactory(): Federation\Factory\DeliveryQueueItem
630 return self::$dice->create(Federation\Factory\DeliveryQueueItem::class);
633 public static function deliveryQueueItemRepo(): Federation\Repository\DeliveryQueueItem
635 return self::$dice->create(Federation\Repository\DeliveryQueueItem::class);
639 // "Protocol" namespace instances
643 * @return Protocol\Activity
645 public static function activity()
647 return self::$dice->create(Protocol\Activity::class);
650 public static function dsprContact(): Protocol\Diaspora\Repository\DiasporaContact
652 return self::$dice->create(Protocol\Diaspora\Repository\DiasporaContact::class);
656 // "Security" namespace instances
660 * @return \Friendica\Security\Authentication
662 public static function auth()
664 return self::$dice->create(Security\Authentication::class);
668 // "Util" namespace instances
672 * @return Util\ACLFormatter
674 public static function aclFormatter()
676 return self::$dice->create(Util\ACLFormatter::class);
682 public static function basePath()
684 return self::$dice->create('$basepath');
688 * @return Util\DateTimeFormat
690 public static function dtFormat()
692 return self::$dice->create(Util\DateTimeFormat::class);
696 * @return Util\FileSystem
698 public static function fs()
700 return self::$dice->create(Util\FileSystem::class);
704 * @return Util\Profiler
706 public static function profiler()
708 return self::$dice->create(Util\Profiler::class);
712 * @return Util\Emailer
714 public static function emailer()
716 return self::$dice->create(Util\Emailer::class);