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\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();
}
/**

View file

@ -1,10 +1,11 @@
<?php
/* Copyright (C) 2010-2024, the Friendica project
* SPDX-FileCopyrightText: 2010-2024 the Friendica project
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* The configuration defines "complex" dependencies inside Friendica
* So this classes shouldn't be simple or their dependencies are already defined here.
*
@ -19,34 +20,15 @@
*
* - $a = new ClassA($creationPassedVariable);
*
* @link https://r.je/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;
/**
* @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
@ -56,138 +38,122 @@ return [
\Friendica\Core\Addon\Capability\ICanLoadAddons::class => [
'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class,
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
$basepath,
[Dice::INSTANCE => Dice::SELF],
],
],
'$basepath' => [
'instanceOf' => Util\BasePath::class,
'call' => [
['getPath', [], Dice::CHAIN_CALL],
],
\Friendica\Util\BasePath::class => [
'constructParams' => [
dirname(__FILE__, 2),
$_SERVER
$basepath,
$serverVars,
]
],
Util\BasePath::class => [
'constructParams' => [
dirname(__FILE__, 2),
$_SERVER
]
],
DiceInstanceManager::class => [
\Friendica\Core\Hooks\Model\DiceInstanceManager::class => [
'constructParams' => [
[Dice::INSTANCE => Dice::SELF],
]
],
\Friendica\Core\Hooks\Util\StrategiesFileManager::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
$basepath,
],
'call' => [
['loadConfig'],
],
],
ICanRegisterStrategies::class => [
'instanceOf' => DiceInstanceManager::class,
\Friendica\Core\Hooks\Capability\ICanRegisterStrategies::class => [
'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class,
'constructParams' => [
[Dice::INSTANCE => Dice::SELF],
],
],
AppHelper::class => [
'instanceOf' => AppLegacy::class,
\Friendica\AppHelper::class => [
'instanceOf' => \Friendica\AppLegacy::class,
],
ICanCreateInstances::class => [
'instanceOf' => DiceInstanceManager::class,
\Friendica\Core\Hooks\Capability\ICanCreateInstances::class => [
'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class,
'constructParams' => [
[Dice::INSTANCE => Dice::SELF],
],
],
Config\Util\ConfigFileManager::class => [
'instanceOf' => Config\Factory\Config::class,
\Friendica\Core\Config\Util\ConfigFileManager::class => [
'instanceOf' => \Friendica\Core\Config\Factory\Config::class,
'call' => [
['createConfigFileManager', [
[Dice::INSTANCE => '$basepath'],
$_SERVER,
$basepath,
$serverVars,
], Dice::CHAIN_CALL],
],
],
Config\ValueObject\Cache::class => [
'instanceOf' => Config\Factory\Config::class,
\Friendica\Core\Config\ValueObject\Cache::class => [
'instanceOf' => \Friendica\Core\Config\Factory\Config::class,
'call' => [
['createCache', [], Dice::CHAIN_CALL],
],
],
App\Mode::class => [
\Friendica\App\Mode::class => [
'call' => [
['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL],
['determineRunMode', [true, $serverVars], Dice::CHAIN_CALL],
['determine', [
[Dice::INSTANCE => '$basepath']
$basepath,
], Dice::CHAIN_CALL],
],
],
Config\Capability\IManageConfigValues::class => [
'instanceOf' => Config\Model\DatabaseConfig::class,
\Friendica\Core\Config\Capability\IManageConfigValues::class => [
'instanceOf' => \Friendica\Core\Config\Model\DatabaseConfig::class,
'constructParams' => [
$_SERVER,
$serverVars,
],
],
PConfig\Capability\IManagePersonalConfigValues::class => [
'instanceOf' => PConfig\Factory\PConfig::class,
\Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class => [
'instanceOf' => \Friendica\Core\PConfig\Factory\PConfig::class,
'call' => [
['create', [], Dice::CHAIN_CALL],
]
],
DbaDefinition::class => [
\Friendica\Database\Definition\DbaDefinition::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
$basepath,
],
'call' => [
['load', [false], Dice::CHAIN_CALL],
],
],
ViewDefinition::class => [
\Friendica\Database\Definition\ViewDefinition::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
$basepath,
],
'call' => [
['load', [false], Dice::CHAIN_CALL],
],
],
Database::class => [
\Friendica\Database\Database::class => [
'constructParams' => [
[Dice::INSTANCE => Config\Model\ReadOnlyFileConfig::class],
[Dice::INSTANCE => \Friendica\Core\Config\Model\ReadOnlyFileConfig::class],
],
],
/**
* Creates the App\BaseURL
*
* Same as:
* $baseURL = new App\BaseURL($configuration, $_SERVER);
*/
App\BaseURL::class => [
\Friendica\App\BaseURL::class => [
'constructParams' => [
$_SERVER,
$serverVars,
],
],
'$hostname' => [
'instanceOf' => App\BaseURL::class,
'instanceOf' => \Friendica\App\BaseURL::class,
'constructParams' => [
$_SERVER,
$serverVars,
],
'call' => [
['getHost', [], Dice::CHAIN_CALL],
],
],
Cache\Type\AbstractCache::class => [
\Friendica\Core\Cache\Type\AbstractCache::class => [
'constructParams' => [
[Dice::INSTANCE => '$hostname'],
],
],
App\Page::class => [
\Friendica\App\Page::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
$basepath,
],
],
\Psr\Log\LoggerInterface::class => [
@ -220,65 +186,65 @@ return [
['createDev', [], Dice::CHAIN_CALL],
],
],
Cache\Capability\ICanCache::class => [
'instanceOf' => Cache\Factory\Cache::class,
\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,
\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,
\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,
\Friendica\App\Arguments::class => [
'instanceOf' => \Friendica\App\Arguments::class,
'call' => [
['determine', [$_SERVER, $_GET], Dice::CHAIN_CALL],
['determine', [$serverVars, $getVars], Dice::CHAIN_CALL],
],
],
\Friendica\Core\System::class => [
'constructParams' => [
[Dice::INSTANCE => '$basepath'],
$basepath,
],
],
App\Router::class => [
\Friendica\App\Router::class => [
'constructParams' => [
$_SERVER,
$serverVars,
__DIR__ . '/routes.config.php',
[Dice::INSTANCE => Dice::SELF],
null
],
],
L10n::class => [
\Friendica\Core\L10n::class => [
'constructParams' => [
$_SERVER, $_GET
$serverVars, $getVars
],
],
IHandleSessions::class => [
\Friendica\Core\Session\Capability\IHandleSessions::class => [
'instanceOf' => \Friendica\Core\Session\Factory\Session::class,
'call' => [
['create', [$_SERVER], Dice::CHAIN_CALL],
['create', [$serverVars], Dice::CHAIN_CALL],
['start', [], Dice::CHAIN_CALL],
],
],
IHandleUserSessions::class => [
\Friendica\Core\Session\Capability\IHandleUserSessions::class => [
'instanceOf' => \Friendica\Core\Session\Model\UserSession::class,
],
Cookie::class => [
\Friendica\Model\User\Cookie::class => [
'constructParams' => [
$_COOKIE
$cookieVars,
],
],
ICanWriteToStorage::class => [
'instanceOf' => StorageManager::class,
\Friendica\Core\Storage\Capability\ICanWriteToStorage::class => [
'instanceOf' => \Friendica\Core\Storage\Repository\StorageManager::class,
'call' => [
['getBackend', [], Dice::CHAIN_CALL],
],
@ -289,39 +255,45 @@ return [
['create', [], Dice::CHAIN_CALL],
],
],
Network\HTTPClient\Capability\ICanSendHttpRequests::class => [
'instanceOf' => Network\HTTPClient\Factory\HttpClient::class,
\Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests::class => [
'instanceOf' => \Friendica\Network\HTTPClient\Factory\HttpClient::class,
'call' => [
['createClient', [], Dice::CHAIN_CALL],
],
],
ParsedLogIterator::class => [
\Friendica\Model\Log\ParsedLogIterator::class => [
'constructParams' => [
[Dice::INSTANCE => Util\ReversedFileReader::class],
[Dice::INSTANCE => \Friendica\Util\ReversedFileReader::class],
]
],
\Friendica\Core\Worker\Repository\Process::class => [
'constructParams' => [
$_SERVER
$serverVars
],
],
App\Request::class => [
\Friendica\App\Request::class => [
'constructParams' => [
$_SERVER
$serverVars
],
],
\Psr\Clock\ClockInterface::class => [
'instanceOf' => Util\Clock\SystemClock::class
'instanceOf' => \Friendica\Util\Clock\SystemClock::class
],
\Friendica\Module\Special\HTTPException::class => [
'constructParams' => [
$_SERVER
$serverVars
],
],
\Friendica\Module\Api\ApiResponse::class => [
'constructParams' => [
$_SERVER,
$_GET['callback'] ?? '',
$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
declare(strict_types = 1);
namespace Friendica\Test\Unit\Util;
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();
}
}