diff --git a/src/DI.php b/src/DI.php index 18d74dd23a..9381d6a782 100644 --- a/src/DI.php +++ b/src/DI.php @@ -14,6 +14,7 @@ use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Capability\IHandleUserSessions; use Friendica\Navigation\SystemMessages; use Friendica\Protocol\ATProtocol; +use Friendica\Util\BasePath; use Psr\Log\LoggerInterface; /** @@ -746,7 +747,10 @@ abstract class DI */ public static function basePath() { - return self::$dice->create('$basepath'); + /** @var BasePath */ + $basePath = self::$dice->create(BasePath::class); + + return $basePath->getPath(); } /** diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 615319336b..93d9de81ac 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -1,10 +1,11 @@ [ - // marks all class result as shared for other creations, so there's just - // one instance for the whole execution - 'shared' => true, - ], - \Friendica\Core\Addon\Capability\ICanLoadAddons::class => [ - 'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class, - 'constructParams' => [ - [Dice::INSTANCE => '$basepath'], - [Dice::INSTANCE => Dice::SELF], +/** + * @param string $basepath The base path of the Friendica installation without trailing slash + */ +return (function(string $basepath, array $getVars, array $serverVars, array $cookieVars): array { + return [ + '*' => [ + // marks all class result as shared for other creations, so there's just + // one instance for the whole execution + 'shared' => true, ], - ], - '$basepath' => [ - 'instanceOf' => Util\BasePath::class, - 'call' => [ - ['getPath', [], Dice::CHAIN_CALL], + \Friendica\Core\Addon\Capability\ICanLoadAddons::class => [ + 'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class, + 'constructParams' => [ + $basepath, + [Dice::INSTANCE => Dice::SELF], + ], ], - 'constructParams' => [ - dirname(__FILE__, 2), - $_SERVER - ] - ], - 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'], + \Friendica\Util\BasePath::class => [ + 'constructParams' => [ + $basepath, + $serverVars, + ] ], - 'call' => [ - ['loadConfig'], + \Friendica\Core\Hooks\Model\DiceInstanceManager::class => [ + 'constructParams' => [ + [Dice::INSTANCE => Dice::SELF], + ] ], - ], - ICanRegisterStrategies::class => [ - 'instanceOf' => DiceInstanceManager::class, - 'constructParams' => [ - [Dice::INSTANCE => Dice::SELF], + \Friendica\Core\Hooks\Util\StrategiesFileManager::class => [ + 'constructParams' => [ + $basepath, + ], + 'call' => [ + ['loadConfig'], + ], ], - ], - AppHelper::class => [ - 'instanceOf' => AppLegacy::class, - ], - ICanCreateInstances::class => [ - 'instanceOf' => DiceInstanceManager::class, - 'constructParams' => [ - [Dice::INSTANCE => Dice::SELF], + \Friendica\Core\Hooks\Capability\ICanRegisterStrategies::class => [ + 'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class, + 'constructParams' => [ + [Dice::INSTANCE => Dice::SELF], + ], ], - ], - Config\Util\ConfigFileManager::class => [ - 'instanceOf' => Config\Factory\Config::class, - 'call' => [ - ['createConfigFileManager', [ - [Dice::INSTANCE => '$basepath'], - $_SERVER, - ], Dice::CHAIN_CALL], + \Friendica\AppHelper::class => [ + 'instanceOf' => \Friendica\AppLegacy::class, ], - ], - Config\ValueObject\Cache::class => [ - 'instanceOf' => Config\Factory\Config::class, - 'call' => [ - ['createCache', [], Dice::CHAIN_CALL], + \Friendica\Core\Hooks\Capability\ICanCreateInstances::class => [ + 'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class, + 'constructParams' => [ + [Dice::INSTANCE => Dice::SELF], + ], ], - ], - App\Mode::class => [ - 'call' => [ - ['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL], - ['determine', [ - [Dice::INSTANCE => '$basepath'] - ], Dice::CHAIN_CALL], + \Friendica\Core\Config\Util\ConfigFileManager::class => [ + 'instanceOf' => \Friendica\Core\Config\Factory\Config::class, + 'call' => [ + ['createConfigFileManager', [ + $basepath, + $serverVars, + ], Dice::CHAIN_CALL], + ], ], - ], - Config\Capability\IManageConfigValues::class => [ - 'instanceOf' => Config\Model\DatabaseConfig::class, - 'constructParams' => [ - $_SERVER, + \Friendica\Core\Config\ValueObject\Cache::class => [ + 'instanceOf' => \Friendica\Core\Config\Factory\Config::class, + 'call' => [ + ['createCache', [], Dice::CHAIN_CALL], + ], ], - ], - PConfig\Capability\IManagePersonalConfigValues::class => [ - 'instanceOf' => PConfig\Factory\PConfig::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], - ] - ], - DbaDefinition::class => [ - 'constructParams' => [ - [Dice::INSTANCE => '$basepath'], + \Friendica\App\Mode::class => [ + 'call' => [ + ['determineRunMode', [true, $serverVars], Dice::CHAIN_CALL], + ['determine', [ + $basepath, + ], Dice::CHAIN_CALL], + ], ], - 'call' => [ - ['load', [false], Dice::CHAIN_CALL], + \Friendica\Core\Config\Capability\IManageConfigValues::class => [ + 'instanceOf' => \Friendica\Core\Config\Model\DatabaseConfig::class, + 'constructParams' => [ + $serverVars, + ], ], - ], - ViewDefinition::class => [ - 'constructParams' => [ - [Dice::INSTANCE => '$basepath'], + \Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class => [ + 'instanceOf' => \Friendica\Core\PConfig\Factory\PConfig::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ] ], - 'call' => [ - ['load', [false], Dice::CHAIN_CALL], + \Friendica\Database\Definition\DbaDefinition::class => [ + 'constructParams' => [ + $basepath, + ], + 'call' => [ + ['load', [false], Dice::CHAIN_CALL], + ], ], - ], - Database::class => [ - 'constructParams' => [ - [Dice::INSTANCE => Config\Model\ReadOnlyFileConfig::class], + \Friendica\Database\Definition\ViewDefinition::class => [ + 'constructParams' => [ + $basepath, + ], + 'call' => [ + ['load', [false], Dice::CHAIN_CALL], + ], ], - ], - /** - * Creates the App\BaseURL - * - * Same as: - * $baseURL = new App\BaseURL($configuration, $_SERVER); - */ - App\BaseURL::class => [ - 'constructParams' => [ - $_SERVER, + \Friendica\Database\Database::class => [ + 'constructParams' => [ + [Dice::INSTANCE => \Friendica\Core\Config\Model\ReadOnlyFileConfig::class], + ], ], - ], - '$hostname' => [ - 'instanceOf' => App\BaseURL::class, - 'constructParams' => [ - $_SERVER, + \Friendica\App\BaseURL::class => [ + 'constructParams' => [ + $serverVars, + ], ], - 'call' => [ - ['getHost', [], Dice::CHAIN_CALL], + '$hostname' => [ + 'instanceOf' => \Friendica\App\BaseURL::class, + 'constructParams' => [ + $serverVars, + ], + 'call' => [ + ['getHost', [], Dice::CHAIN_CALL], + ], ], - ], - Cache\Type\AbstractCache::class => [ - 'constructParams' => [ - [Dice::INSTANCE => '$hostname'], + \Friendica\Core\Cache\Type\AbstractCache::class => [ + 'constructParams' => [ + [Dice::INSTANCE => '$hostname'], + ], ], - ], - App\Page::class => [ - 'constructParams' => [ - [Dice::INSTANCE => '$basepath'], + \Friendica\App\Page::class => [ + 'constructParams' => [ + $basepath, + ], ], - ], - \Psr\Log\LoggerInterface::class => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\Logger::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], + \Psr\Log\LoggerInterface::class => [ + 'instanceOf' => \Friendica\Core\Logger\Factory\Logger::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], ], - ], - \Friendica\Core\Logger\Type\SyslogLogger::class => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], + \Friendica\Core\Logger\Type\SyslogLogger::class => [ + 'instanceOf' => \Friendica\Core\Logger\Factory\SyslogLogger::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], ], - ], - \Friendica\Core\Logger\Type\StreamLogger::class => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], + \Friendica\Core\Logger\Type\StreamLogger::class => [ + 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], ], - ], - \Friendica\Core\Logger\Capability\IHaveCallIntrospections::class => [ - 'instanceOf' => \Friendica\Core\Logger\Util\Introspection::class, - 'constructParams' => [ - \Friendica\Core\Logger\Capability\IHaveCallIntrospections::IGNORE_CLASS_LIST, + \Friendica\Core\Logger\Capability\IHaveCallIntrospections::class => [ + 'instanceOf' => \Friendica\Core\Logger\Util\Introspection::class, + 'constructParams' => [ + \Friendica\Core\Logger\Capability\IHaveCallIntrospections::IGNORE_CLASS_LIST, + ], ], - ], - '$devLogger' => [ - 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, - 'call' => [ - ['createDev', [], Dice::CHAIN_CALL], + '$devLogger' => [ + 'instanceOf' => \Friendica\Core\Logger\Factory\StreamLogger::class, + 'call' => [ + ['createDev', [], Dice::CHAIN_CALL], + ], ], - ], - Cache\Capability\ICanCache::class => [ - 'instanceOf' => Cache\Factory\Cache::class, - 'call' => [ - ['createLocal', [], Dice::CHAIN_CALL], + \Friendica\Core\Cache\Capability\ICanCache::class => [ + 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class, + 'call' => [ + ['createLocal', [], Dice::CHAIN_CALL], + ], ], - ], - Cache\Capability\ICanCacheInMemory::class => [ - 'instanceOf' => Cache\Factory\Cache::class, - 'call' => [ - ['createLocal', [], Dice::CHAIN_CALL], + \Friendica\Core\Cache\Capability\ICanCacheInMemory::class => [ + 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class, + 'call' => [ + ['createLocal', [], Dice::CHAIN_CALL], + ], ], - ], - Lock\Capability\ICanLock::class => [ - 'instanceOf' => Lock\Factory\Lock::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], + \Friendica\Core\Lock\Capability\ICanLock::class => [ + 'instanceOf' => \Friendica\Core\Lock\Factory\Lock::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], ], - ], - App\Arguments::class => [ - 'instanceOf' => App\Arguments::class, - 'call' => [ - ['determine', [$_SERVER, $_GET], Dice::CHAIN_CALL], + \Friendica\App\Arguments::class => [ + 'instanceOf' => \Friendica\App\Arguments::class, + 'call' => [ + ['determine', [$serverVars, $getVars], Dice::CHAIN_CALL], + ], ], - ], - \Friendica\Core\System::class => [ - 'constructParams' => [ - [Dice::INSTANCE => '$basepath'], + \Friendica\Core\System::class => [ + 'constructParams' => [ + $basepath, + ], ], - ], - App\Router::class => [ - 'constructParams' => [ - $_SERVER, - __DIR__ . '/routes.config.php', - [Dice::INSTANCE => Dice::SELF], - null + \Friendica\App\Router::class => [ + 'constructParams' => [ + $serverVars, + __DIR__ . '/routes.config.php', + [Dice::INSTANCE => Dice::SELF], + null + ], ], - ], - L10n::class => [ - 'constructParams' => [ - $_SERVER, $_GET + \Friendica\Core\L10n::class => [ + 'constructParams' => [ + $serverVars, $getVars + ], ], - ], - IHandleSessions::class => [ - 'instanceOf' => \Friendica\Core\Session\Factory\Session::class, - 'call' => [ - ['create', [$_SERVER], Dice::CHAIN_CALL], - ['start', [], Dice::CHAIN_CALL], + \Friendica\Core\Session\Capability\IHandleSessions::class => [ + 'instanceOf' => \Friendica\Core\Session\Factory\Session::class, + 'call' => [ + ['create', [$serverVars], Dice::CHAIN_CALL], + ['start', [], Dice::CHAIN_CALL], + ], ], - ], - IHandleUserSessions::class => [ - 'instanceOf' => \Friendica\Core\Session\Model\UserSession::class, - ], - Cookie::class => [ - 'constructParams' => [ - $_COOKIE + \Friendica\Core\Session\Capability\IHandleUserSessions::class => [ + 'instanceOf' => \Friendica\Core\Session\Model\UserSession::class, ], - ], - ICanWriteToStorage::class => [ - 'instanceOf' => StorageManager::class, - 'call' => [ - ['getBackend', [], Dice::CHAIN_CALL], + \Friendica\Model\User\Cookie::class => [ + 'constructParams' => [ + $cookieVars, + ], ], - ], - \Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs::class => [ - 'instanceOf' => \Friendica\Core\KeyValueStorage\Factory\KeyValueStorage::class, - 'call' => [ - ['create', [], Dice::CHAIN_CALL], + \Friendica\Core\Storage\Capability\ICanWriteToStorage::class => [ + 'instanceOf' => \Friendica\Core\Storage\Repository\StorageManager::class, + 'call' => [ + ['getBackend', [], Dice::CHAIN_CALL], + ], ], - ], - Network\HTTPClient\Capability\ICanSendHttpRequests::class => [ - 'instanceOf' => Network\HTTPClient\Factory\HttpClient::class, - 'call' => [ - ['createClient', [], Dice::CHAIN_CALL], + \Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs::class => [ + 'instanceOf' => \Friendica\Core\KeyValueStorage\Factory\KeyValueStorage::class, + 'call' => [ + ['create', [], Dice::CHAIN_CALL], + ], ], - ], - ParsedLogIterator::class => [ - 'constructParams' => [ - [Dice::INSTANCE => Util\ReversedFileReader::class], - ] - ], - \Friendica\Core\Worker\Repository\Process::class => [ - 'constructParams' => [ - $_SERVER + \Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests::class => [ + 'instanceOf' => \Friendica\Network\HTTPClient\Factory\HttpClient::class, + 'call' => [ + ['createClient', [], Dice::CHAIN_CALL], + ], ], - ], - App\Request::class => [ - 'constructParams' => [ - $_SERVER + \Friendica\Model\Log\ParsedLogIterator::class => [ + 'constructParams' => [ + [Dice::INSTANCE => \Friendica\Util\ReversedFileReader::class], + ] ], - ], - \Psr\Clock\ClockInterface::class => [ - 'instanceOf' => Util\Clock\SystemClock::class - ], - \Friendica\Module\Special\HTTPException::class => [ - 'constructParams' => [ - $_SERVER + \Friendica\Core\Worker\Repository\Process::class => [ + 'constructParams' => [ + $serverVars + ], ], - ], - \Friendica\Module\Api\ApiResponse::class => [ - 'constructParams' => [ - $_SERVER, - $_GET['callback'] ?? '', + \Friendica\App\Request::class => [ + 'constructParams' => [ + $serverVars + ], ], - ], -]; + \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 +); diff --git a/tests/Unit/Util/BasePathTest.php b/tests/Unit/Util/BasePathTest.php new file mode 100644 index 0000000000..26c495b7fe --- /dev/null +++ b/tests/Unit/Util/BasePathTest.php @@ -0,0 +1,88 @@ + [ + '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(); + } +} diff --git a/tests/Unit/Util/CryptoTest.php b/tests/Unit/Util/CryptoTest.php index 1e728746f2..9dbffb29b4 100644 --- a/tests/Unit/Util/CryptoTest.php +++ b/tests/Unit/Util/CryptoTest.php @@ -5,6 +5,8 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later +declare(strict_types = 1); + namespace Friendica\Test\Unit\Util; use Friendica\Util\Crypto; diff --git a/tests/src/Util/BasePathTest.php b/tests/src/Util/BasePathTest.php deleted file mode 100644 index bbcaeb359b..0000000000 --- a/tests/src/Util/BasePathTest.php +++ /dev/null @@ -1,82 +0,0 @@ - [ - '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(); - } -}