Merge branch 'refactor-entrypoint-console.php' into refactor-entrypoint-daemon.php

This commit is contained in:
Art4 2024-12-27 11:19:00 +00:00
commit 82d987cb1d
5 changed files with 333 additions and 349 deletions

View file

@ -14,6 +14,7 @@ use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Navigation\SystemMessages; use Friendica\Navigation\SystemMessages;
use Friendica\Protocol\ATProtocol; use Friendica\Protocol\ATProtocol;
use Friendica\Util\BasePath;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -746,7 +747,10 @@ abstract class DI
*/ */
public static function basePath() public static function basePath()
{ {
return self::$dice->create('$basepath'); /** @var BasePath */
$basePath = self::$dice->create(BasePath::class);
return $basePath->getPath();
} }
/** /**

View file

@ -1,10 +1,11 @@
<?php <?php
/* Copyright (C) 2010-2024, the Friendica project // Copyright (C) 2010-2024, the Friendica project
* SPDX-FileCopyrightText: 2010-2024 the Friendica project // SPDX-FileCopyrightText: 2010-2024 the Friendica project
* //
* SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
*
/**
* The configuration defines "complex" dependencies inside Friendica * The configuration defines "complex" dependencies inside Friendica
* So this classes shouldn't be simple or their dependencies are already defined here. * So this classes shouldn't be simple or their dependencies are already defined here.
* *
@ -19,309 +20,280 @@
* *
* - $a = new ClassA($creationPassedVariable); * - $a = new ClassA($creationPassedVariable);
* *
* @link https://r.je/dice
*/ */
use Dice\Dice; use Dice\Dice;
use Friendica\App;
use Friendica\AppHelper;
use Friendica\AppLegacy;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Hooks\Capability\ICanCreateInstances;
use Friendica\Core\Hooks\Capability\ICanRegisterStrategies;
use Friendica\Core\Hooks\Model\DiceInstanceManager;
use Friendica\Core\PConfig;
use Friendica\Core\L10n;
use Friendica\Core\Lock;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\Storage\Repository\StorageManager;
use Friendica\Database\Database;
use Friendica\Database\Definition\DbaDefinition;
use Friendica\Database\Definition\ViewDefinition;
use Friendica\Factory;
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Model\User\Cookie;
use Friendica\Model\Log\ParsedLogIterator;
use Friendica\Network;
use Friendica\Util;
use Psr\Log\LoggerInterface;
return [ /**
'*' => [ * @param string $basepath The base path of the Friendica installation without trailing slash
// marks all class result as shared for other creations, so there's just */
// one instance for the whole execution return (function(string $basepath, array $getVars, array $serverVars, array $cookieVars): array {
'shared' => true, return [
], '*' => [
\Friendica\Core\Addon\Capability\ICanLoadAddons::class => [ // marks all class result as shared for other creations, so there's just
'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class, // one instance for the whole execution
'constructParams' => [ 'shared' => true,
[Dice::INSTANCE => '$basepath'],
[Dice::INSTANCE => Dice::SELF],
], ],
], \Friendica\Core\Addon\Capability\ICanLoadAddons::class => [
'$basepath' => [ 'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class,
'instanceOf' => Util\BasePath::class, 'constructParams' => [
'call' => [ $basepath,
['getPath', [], Dice::CHAIN_CALL], [Dice::INSTANCE => Dice::SELF],
],
], ],
'constructParams' => [ \Friendica\Util\BasePath::class => [
dirname(__FILE__, 2), 'constructParams' => [
$_SERVER $basepath,
] $serverVars,
], ]
Util\BasePath::class => [
'constructParams' => [
dirname(__FILE__, 2),
$_SERVER
]
],
DiceInstanceManager::class => [
'constructParams' => [
[Dice::INSTANCE => Dice::SELF],
]
],
\Friendica\Core\Hooks\Util\StrategiesFileManager::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
], ],
'call' => [ \Friendica\Core\Hooks\Model\DiceInstanceManager::class => [
['loadConfig'], 'constructParams' => [
[Dice::INSTANCE => Dice::SELF],
]
], ],
], \Friendica\Core\Hooks\Util\StrategiesFileManager::class => [
ICanRegisterStrategies::class => [ 'constructParams' => [
'instanceOf' => DiceInstanceManager::class, $basepath,
'constructParams' => [ ],
[Dice::INSTANCE => Dice::SELF], 'call' => [
['loadConfig'],
],
], ],
], \Friendica\Core\Hooks\Capability\ICanRegisterStrategies::class => [
AppHelper::class => [ 'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class,
'instanceOf' => AppLegacy::class, 'constructParams' => [
], [Dice::INSTANCE => Dice::SELF],
ICanCreateInstances::class => [ ],
'instanceOf' => DiceInstanceManager::class,
'constructParams' => [
[Dice::INSTANCE => Dice::SELF],
], ],
], \Friendica\AppHelper::class => [
Config\Util\ConfigFileManager::class => [ 'instanceOf' => \Friendica\AppLegacy::class,
'instanceOf' => Config\Factory\Config::class,
'call' => [
['createConfigFileManager', [
[Dice::INSTANCE => '$basepath'],
$_SERVER,
], Dice::CHAIN_CALL],
], ],
], \Friendica\Core\Hooks\Capability\ICanCreateInstances::class => [
Config\ValueObject\Cache::class => [ 'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class,
'instanceOf' => Config\Factory\Config::class, 'constructParams' => [
'call' => [ [Dice::INSTANCE => Dice::SELF],
['createCache', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Config\Util\ConfigFileManager::class => [
App\Mode::class => [ 'instanceOf' => \Friendica\Core\Config\Factory\Config::class,
'call' => [ 'call' => [
['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL], ['createConfigFileManager', [
['determine', [ $basepath,
[Dice::INSTANCE => '$basepath'] $serverVars,
], Dice::CHAIN_CALL], ], Dice::CHAIN_CALL],
],
], ],
], \Friendica\Core\Config\ValueObject\Cache::class => [
Config\Capability\IManageConfigValues::class => [ 'instanceOf' => \Friendica\Core\Config\Factory\Config::class,
'instanceOf' => Config\Model\DatabaseConfig::class, 'call' => [
'constructParams' => [ ['createCache', [], Dice::CHAIN_CALL],
$_SERVER, ],
], ],
], \Friendica\App\Mode::class => [
PConfig\Capability\IManagePersonalConfigValues::class => [ 'call' => [
'instanceOf' => PConfig\Factory\PConfig::class, ['determineRunMode', [true, $serverVars], Dice::CHAIN_CALL],
'call' => [ ['determine', [
['create', [], Dice::CHAIN_CALL], $basepath,
] ], Dice::CHAIN_CALL],
], ],
DbaDefinition::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
], ],
'call' => [ \Friendica\Core\Config\Capability\IManageConfigValues::class => [
['load', [false], Dice::CHAIN_CALL], 'instanceOf' => \Friendica\Core\Config\Model\DatabaseConfig::class,
'constructParams' => [
$serverVars,
],
], ],
], \Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class => [
ViewDefinition::class => [ 'instanceOf' => \Friendica\Core\PConfig\Factory\PConfig::class,
'constructParams' => [ 'call' => [
[Dice::INSTANCE => '$basepath'], ['create', [], Dice::CHAIN_CALL],
]
], ],
'call' => [ \Friendica\Database\Definition\DbaDefinition::class => [
['load', [false], Dice::CHAIN_CALL], 'constructParams' => [
$basepath,
],
'call' => [
['load', [false], Dice::CHAIN_CALL],
],
], ],
], \Friendica\Database\Definition\ViewDefinition::class => [
Database::class => [ 'constructParams' => [
'constructParams' => [ $basepath,
[Dice::INSTANCE => Config\Model\ReadOnlyFileConfig::class], ],
'call' => [
['load', [false], Dice::CHAIN_CALL],
],
], ],
], \Friendica\Database\Database::class => [
/** 'constructParams' => [
* Creates the App\BaseURL [Dice::INSTANCE => \Friendica\Core\Config\Model\ReadOnlyFileConfig::class],
* ],
* Same as:
* $baseURL = new App\BaseURL($configuration, $_SERVER);
*/
App\BaseURL::class => [
'constructParams' => [
$_SERVER,
], ],
], \Friendica\App\BaseURL::class => [
'$hostname' => [ 'constructParams' => [
'instanceOf' => App\BaseURL::class, $serverVars,
'constructParams' => [ ],
$_SERVER,
], ],
'call' => [ '$hostname' => [
['getHost', [], Dice::CHAIN_CALL], 'instanceOf' => \Friendica\App\BaseURL::class,
'constructParams' => [
$serverVars,
],
'call' => [
['getHost', [], Dice::CHAIN_CALL],
],
], ],
], \Friendica\Core\Cache\Type\AbstractCache::class => [
Cache\Type\AbstractCache::class => [ 'constructParams' => [
'constructParams' => [ [Dice::INSTANCE => '$hostname'],
[Dice::INSTANCE => '$hostname'], ],
], ],
], \Friendica\App\Page::class => [
App\Page::class => [ 'constructParams' => [
'constructParams' => [ $basepath,
[Dice::INSTANCE => '$basepath'], ],
], ],
], \Psr\Log\LoggerInterface::class => [
\Psr\Log\LoggerInterface::class => [ 'instanceOf' => \Friendica\Core\Logger\Factory\Logger::class,
'instanceOf' => \Friendica\Core\Logger\Factory\Logger::class, 'call' => [
'call' => [ ['create', [], Dice::CHAIN_CALL],
['create', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Logger\Type\SyslogLogger::class => [
\Friendica\Core\Logger\Type\SyslogLogger::class => [ 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class,
'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, 'call' => [
'call' => [ ['create', [], Dice::CHAIN_CALL],
['create', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Logger\Type\StreamLogger::class => [
\Friendica\Core\Logger\Type\StreamLogger::class => [ 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class,
'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, 'call' => [
'call' => [ ['create', [], Dice::CHAIN_CALL],
['create', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Logger\Capability\IHaveCallIntrospections::class => [
\Friendica\Core\Logger\Capability\IHaveCallIntrospections::class => [ 'instanceOf' => \Friendica\Core\Logger\Util\Introspection::class,
'instanceOf' => \Friendica\Core\Logger\Util\Introspection::class, 'constructParams' => [
'constructParams' => [ \Friendica\Core\Logger\Capability\IHaveCallIntrospections::IGNORE_CLASS_LIST,
\Friendica\Core\Logger\Capability\IHaveCallIntrospections::IGNORE_CLASS_LIST, ],
], ],
], '$devLogger' => [
'$devLogger' => [ 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class,
'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, 'call' => [
'call' => [ ['createDev', [], Dice::CHAIN_CALL],
['createDev', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Cache\Capability\ICanCache::class => [
Cache\Capability\ICanCache::class => [ 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class,
'instanceOf' => Cache\Factory\Cache::class, 'call' => [
'call' => [ ['createLocal', [], Dice::CHAIN_CALL],
['createLocal', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Cache\Capability\ICanCacheInMemory::class => [
Cache\Capability\ICanCacheInMemory::class => [ 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class,
'instanceOf' => Cache\Factory\Cache::class, 'call' => [
'call' => [ ['createLocal', [], Dice::CHAIN_CALL],
['createLocal', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Lock\Capability\ICanLock::class => [
Lock\Capability\ICanLock::class => [ 'instanceOf' => \Friendica\Core\Lock\Factory\Lock::class,
'instanceOf' => Lock\Factory\Lock::class, 'call' => [
'call' => [ ['create', [], Dice::CHAIN_CALL],
['create', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\App\Arguments::class => [
App\Arguments::class => [ 'instanceOf' => \Friendica\App\Arguments::class,
'instanceOf' => App\Arguments::class, 'call' => [
'call' => [ ['determine', [$serverVars, $getVars], Dice::CHAIN_CALL],
['determine', [$_SERVER, $_GET], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\System::class => [
\Friendica\Core\System::class => [ 'constructParams' => [
'constructParams' => [ $basepath,
[Dice::INSTANCE => '$basepath'], ],
], ],
], \Friendica\App\Router::class => [
App\Router::class => [ 'constructParams' => [
'constructParams' => [ $serverVars,
$_SERVER, __DIR__ . '/routes.config.php',
__DIR__ . '/routes.config.php', [Dice::INSTANCE => Dice::SELF],
[Dice::INSTANCE => Dice::SELF], null
null ],
], ],
], \Friendica\Core\L10n::class => [
L10n::class => [ 'constructParams' => [
'constructParams' => [ $serverVars, $getVars
$_SERVER, $_GET ],
], ],
], \Friendica\Core\Session\Capability\IHandleSessions::class => [
IHandleSessions::class => [ 'instanceOf' => \Friendica\Core\Session\Factory\Session::class,
'instanceOf' => \Friendica\Core\Session\Factory\Session::class, 'call' => [
'call' => [ ['create', [$serverVars], Dice::CHAIN_CALL],
['create', [$_SERVER], Dice::CHAIN_CALL], ['start', [], Dice::CHAIN_CALL],
['start', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\Session\Capability\IHandleUserSessions::class => [
IHandleUserSessions::class => [ 'instanceOf' => \Friendica\Core\Session\Model\UserSession::class,
'instanceOf' => \Friendica\Core\Session\Model\UserSession::class,
],
Cookie::class => [
'constructParams' => [
$_COOKIE
], ],
], \Friendica\Model\User\Cookie::class => [
ICanWriteToStorage::class => [ 'constructParams' => [
'instanceOf' => StorageManager::class, $cookieVars,
'call' => [ ],
['getBackend', [], Dice::CHAIN_CALL],
], ],
], \Friendica\Core\Storage\Capability\ICanWriteToStorage::class => [
\Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs::class => [ 'instanceOf' => \Friendica\Core\Storage\Repository\StorageManager::class,
'instanceOf' => \Friendica\Core\KeyValueStorage\Factory\KeyValueStorage::class, 'call' => [
'call' => [ ['getBackend', [], Dice::CHAIN_CALL],
['create', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs::class => [
Network\HTTPClient\Capability\ICanSendHttpRequests::class => [ 'instanceOf' => \Friendica\Core\KeyValueStorage\Factory\KeyValueStorage::class,
'instanceOf' => Network\HTTPClient\Factory\HttpClient::class, 'call' => [
'call' => [ ['create', [], Dice::CHAIN_CALL],
['createClient', [], Dice::CHAIN_CALL], ],
], ],
], \Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests::class => [
ParsedLogIterator::class => [ 'instanceOf' => \Friendica\Network\HTTPClient\Factory\HttpClient::class,
'constructParams' => [ 'call' => [
[Dice::INSTANCE => Util\ReversedFileReader::class], ['createClient', [], Dice::CHAIN_CALL],
] ],
],
\Friendica\Core\Worker\Repository\Process::class => [
'constructParams' => [
$_SERVER
], ],
], \Friendica\Model\Log\ParsedLogIterator::class => [
App\Request::class => [ 'constructParams' => [
'constructParams' => [ [Dice::INSTANCE => \Friendica\Util\ReversedFileReader::class],
$_SERVER ]
], ],
], \Friendica\Core\Worker\Repository\Process::class => [
\Psr\Clock\ClockInterface::class => [ 'constructParams' => [
'instanceOf' => Util\Clock\SystemClock::class $serverVars
], ],
\Friendica\Module\Special\HTTPException::class => [
'constructParams' => [
$_SERVER
], ],
], \Friendica\App\Request::class => [
\Friendica\Module\Api\ApiResponse::class => [ 'constructParams' => [
'constructParams' => [ $serverVars
$_SERVER, ],
$_GET['callback'] ?? '',
], ],
], \Psr\Clock\ClockInterface::class => [
]; 'instanceOf' => \Friendica\Util\Clock\SystemClock::class
],
\Friendica\Module\Special\HTTPException::class => [
'constructParams' => [
$serverVars
],
],
\Friendica\Module\Api\ApiResponse::class => [
'constructParams' => [
$serverVars,
$getVars['callback'] ?? '',
],
],
];
})(
dirname(__FILE__, 2),
$_GET,
$_SERVER,
$_COOKIE
);

View file

@ -0,0 +1,88 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types = 1);
namespace Friendica\Test\Unit\Util;
use Friendica\Util\BasePath;
use PHPUnit\Framework\TestCase;
class BasePathTest extends TestCase
{
public static function getDataPaths(): array
{
$basePath = dirname(__DIR__, 3);
$configPath = $basePath . DIRECTORY_SEPARATOR . 'config';
return [
'fullPath' => [
'server' => [],
'baseDir' => $configPath,
'expected' => $configPath,
],
'relative' => [
'server' => [],
'baseDir' => 'config',
'expected' => $configPath,
],
'document_root' => [
'server' => [
'DOCUMENT_ROOT' => $configPath,
],
'baseDir' => '/noooop',
'expected' => $configPath,
],
'pwd' => [
'server' => [
'PWD' => $configPath,
],
'baseDir' => '/noooop',
'expected' => $configPath,
],
'no_overwrite' => [
'server' => [
'DOCUMENT_ROOT' => $basePath,
'PWD' => $basePath,
],
'baseDir' => 'config',
'expected' => $configPath,
],
'no_overwrite_if_invalid' => [
'server' => [
'DOCUMENT_ROOT' => '/nopopop',
'PWD' => $configPath,
],
'baseDir' => '/noatgawe22fafa',
'expected' => $configPath,
]
];
}
/**
* Test the basepath determination
* @dataProvider getDataPaths
*/
public function testDetermineBasePath(array $server, string $baseDir, string $expected): void
{
$basepath = new BasePath($baseDir, $server);
self::assertEquals($expected, $basepath->getPath());
}
/**
* Test the basepath determination with a complete wrong path
*/
public function testFailedBasePath(): void
{
$basepath = new BasePath('/now23452sgfgas', []);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('\'/now23452sgfgas\' is not a valid basepath');
$basepath->getPath();
}
}

View file

@ -5,6 +5,8 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types = 1);
namespace Friendica\Test\Unit\Util; namespace Friendica\Test\Unit\Util;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;

View file

@ -1,82 +0,0 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Friendica\Test\src\Util;
use Friendica\Test\MockedTestCase;
use Friendica\Util\BasePath;
class BasePathTest extends MockedTestCase
{
public function dataPaths()
{
return [
'fullPath' => [
'server' => [],
'input' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
'output' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'relative' => [
'server' => [],
'input' => 'config',
'output' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'document_root' => [
'server' => [
'DOCUMENT_ROOT' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'input' => '/noooop',
'output' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'pwd' => [
'server' => [
'PWD' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'input' => '/noooop',
'output' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'no_overwrite' => [
'server' => [
'DOCUMENT_ROOT' => dirname(__DIR__, 3),
'PWD' => dirname(__DIR__, 3),
],
'input' => 'config',
'output' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'no_overwrite_if_invalid' => [
'server' => [
'DOCUMENT_ROOT' => '/nopopop',
'PWD' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
],
'input' => '/noatgawe22fafa',
'output' => dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'config',
]
];
}
/**
* Test the basepath determination
* @dataProvider dataPaths
*/
public function testDetermineBasePath(array $server, $input, $output)
{
$basepath = new BasePath($input, $server);
self::assertEquals($output, $basepath->getPath());
}
/**
* Test the basepath determination with a complete wrong path
*/
public function testFailedBasePath()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessageMatches("/(.*) is not a valid basepath/");
$basepath = new BasePath('/now23452sgfgas', []);
$basepath->getPath();
}
}