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

This commit is contained in:
Art4 2024-12-27 11:19:17 +00:00
commit 030905fafa
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,35 +20,16 @@
* *
* - $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
*/
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 // marks all class result as shared for other creations, so there's just
// one instance for the whole execution // one instance for the whole execution
@ -56,138 +38,122 @@ return [
\Friendica\Core\Addon\Capability\ICanLoadAddons::class => [ \Friendica\Core\Addon\Capability\ICanLoadAddons::class => [
'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class, 'instanceOf' => \Friendica\Core\Addon\Model\AddonLoader::class,
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$basepath'], $basepath,
[Dice::INSTANCE => Dice::SELF], [Dice::INSTANCE => Dice::SELF],
], ],
], ],
'$basepath' => [ \Friendica\Util\BasePath::class => [
'instanceOf' => Util\BasePath::class,
'call' => [
['getPath', [], Dice::CHAIN_CALL],
],
'constructParams' => [ 'constructParams' => [
dirname(__FILE__, 2), $basepath,
$_SERVER $serverVars,
] ]
], ],
Util\BasePath::class => [ \Friendica\Core\Hooks\Model\DiceInstanceManager::class => [
'constructParams' => [
dirname(__FILE__, 2),
$_SERVER
]
],
DiceInstanceManager::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => Dice::SELF], [Dice::INSTANCE => Dice::SELF],
] ]
], ],
\Friendica\Core\Hooks\Util\StrategiesFileManager::class => [ \Friendica\Core\Hooks\Util\StrategiesFileManager::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$basepath'], $basepath,
], ],
'call' => [ 'call' => [
['loadConfig'], ['loadConfig'],
], ],
], ],
ICanRegisterStrategies::class => [ \Friendica\Core\Hooks\Capability\ICanRegisterStrategies::class => [
'instanceOf' => DiceInstanceManager::class, 'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class,
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => Dice::SELF], [Dice::INSTANCE => Dice::SELF],
], ],
], ],
AppHelper::class => [ \Friendica\AppHelper::class => [
'instanceOf' => AppLegacy::class, 'instanceOf' => \Friendica\AppLegacy::class,
], ],
ICanCreateInstances::class => [ \Friendica\Core\Hooks\Capability\ICanCreateInstances::class => [
'instanceOf' => DiceInstanceManager::class, 'instanceOf' => \Friendica\Core\Hooks\Model\DiceInstanceManager::class,
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => Dice::SELF], [Dice::INSTANCE => Dice::SELF],
], ],
], ],
Config\Util\ConfigFileManager::class => [ \Friendica\Core\Config\Util\ConfigFileManager::class => [
'instanceOf' => Config\Factory\Config::class, 'instanceOf' => \Friendica\Core\Config\Factory\Config::class,
'call' => [ 'call' => [
['createConfigFileManager', [ ['createConfigFileManager', [
[Dice::INSTANCE => '$basepath'], $basepath,
$_SERVER, $serverVars,
], Dice::CHAIN_CALL], ], Dice::CHAIN_CALL],
], ],
], ],
Config\ValueObject\Cache::class => [ \Friendica\Core\Config\ValueObject\Cache::class => [
'instanceOf' => Config\Factory\Config::class, 'instanceOf' => \Friendica\Core\Config\Factory\Config::class,
'call' => [ 'call' => [
['createCache', [], Dice::CHAIN_CALL], ['createCache', [], Dice::CHAIN_CALL],
], ],
], ],
App\Mode::class => [ \Friendica\App\Mode::class => [
'call' => [ 'call' => [
['determineRunMode', [true, $_SERVER], Dice::CHAIN_CALL], ['determineRunMode', [true, $serverVars], Dice::CHAIN_CALL],
['determine', [ ['determine', [
[Dice::INSTANCE => '$basepath'] $basepath,
], Dice::CHAIN_CALL], ], Dice::CHAIN_CALL],
], ],
], ],
Config\Capability\IManageConfigValues::class => [ \Friendica\Core\Config\Capability\IManageConfigValues::class => [
'instanceOf' => Config\Model\DatabaseConfig::class, 'instanceOf' => \Friendica\Core\Config\Model\DatabaseConfig::class,
'constructParams' => [ 'constructParams' => [
$_SERVER, $serverVars,
], ],
], ],
PConfig\Capability\IManagePersonalConfigValues::class => [ \Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class => [
'instanceOf' => PConfig\Factory\PConfig::class, 'instanceOf' => \Friendica\Core\PConfig\Factory\PConfig::class,
'call' => [ 'call' => [
['create', [], Dice::CHAIN_CALL], ['create', [], Dice::CHAIN_CALL],
] ]
], ],
DbaDefinition::class => [ \Friendica\Database\Definition\DbaDefinition::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$basepath'], $basepath,
], ],
'call' => [ 'call' => [
['load', [false], Dice::CHAIN_CALL], ['load', [false], Dice::CHAIN_CALL],
], ],
], ],
ViewDefinition::class => [ \Friendica\Database\Definition\ViewDefinition::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$basepath'], $basepath,
], ],
'call' => [ 'call' => [
['load', [false], Dice::CHAIN_CALL], ['load', [false], Dice::CHAIN_CALL],
], ],
], ],
Database::class => [ \Friendica\Database\Database::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => Config\Model\ReadOnlyFileConfig::class], [Dice::INSTANCE => \Friendica\Core\Config\Model\ReadOnlyFileConfig::class],
], ],
], ],
/** \Friendica\App\BaseURL::class => [
* Creates the App\BaseURL
*
* Same as:
* $baseURL = new App\BaseURL($configuration, $_SERVER);
*/
App\BaseURL::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER, $serverVars,
], ],
], ],
'$hostname' => [ '$hostname' => [
'instanceOf' => App\BaseURL::class, 'instanceOf' => \Friendica\App\BaseURL::class,
'constructParams' => [ 'constructParams' => [
$_SERVER, $serverVars,
], ],
'call' => [ 'call' => [
['getHost', [], Dice::CHAIN_CALL], ['getHost', [], Dice::CHAIN_CALL],
], ],
], ],
Cache\Type\AbstractCache::class => [ \Friendica\Core\Cache\Type\AbstractCache::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$hostname'], [Dice::INSTANCE => '$hostname'],
], ],
], ],
App\Page::class => [ \Friendica\App\Page::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$basepath'], $basepath,
], ],
], ],
\Psr\Log\LoggerInterface::class => [ \Psr\Log\LoggerInterface::class => [
@ -220,65 +186,65 @@ return [
['createDev', [], Dice::CHAIN_CALL], ['createDev', [], Dice::CHAIN_CALL],
], ],
], ],
Cache\Capability\ICanCache::class => [ \Friendica\Core\Cache\Capability\ICanCache::class => [
'instanceOf' => Cache\Factory\Cache::class, 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class,
'call' => [ 'call' => [
['createLocal', [], Dice::CHAIN_CALL], ['createLocal', [], Dice::CHAIN_CALL],
], ],
], ],
Cache\Capability\ICanCacheInMemory::class => [ \Friendica\Core\Cache\Capability\ICanCacheInMemory::class => [
'instanceOf' => Cache\Factory\Cache::class, 'instanceOf' => \Friendica\Core\Cache\Factory\Cache::class,
'call' => [ 'call' => [
['createLocal', [], Dice::CHAIN_CALL], ['createLocal', [], Dice::CHAIN_CALL],
], ],
], ],
Lock\Capability\ICanLock::class => [ \Friendica\Core\Lock\Capability\ICanLock::class => [
'instanceOf' => Lock\Factory\Lock::class, 'instanceOf' => \Friendica\Core\Lock\Factory\Lock::class,
'call' => [ 'call' => [
['create', [], Dice::CHAIN_CALL], ['create', [], Dice::CHAIN_CALL],
], ],
], ],
App\Arguments::class => [ \Friendica\App\Arguments::class => [
'instanceOf' => App\Arguments::class, 'instanceOf' => \Friendica\App\Arguments::class,
'call' => [ 'call' => [
['determine', [$_SERVER, $_GET], Dice::CHAIN_CALL], ['determine', [$serverVars, $getVars], Dice::CHAIN_CALL],
], ],
], ],
\Friendica\Core\System::class => [ \Friendica\Core\System::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => '$basepath'], $basepath,
], ],
], ],
App\Router::class => [ \Friendica\App\Router::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER, $serverVars,
__DIR__ . '/routes.config.php', __DIR__ . '/routes.config.php',
[Dice::INSTANCE => Dice::SELF], [Dice::INSTANCE => Dice::SELF],
null null
], ],
], ],
L10n::class => [ \Friendica\Core\L10n::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER, $_GET $serverVars, $getVars
], ],
], ],
IHandleSessions::class => [ \Friendica\Core\Session\Capability\IHandleSessions::class => [
'instanceOf' => \Friendica\Core\Session\Factory\Session::class, 'instanceOf' => \Friendica\Core\Session\Factory\Session::class,
'call' => [ 'call' => [
['create', [$_SERVER], Dice::CHAIN_CALL], ['create', [$serverVars], Dice::CHAIN_CALL],
['start', [], Dice::CHAIN_CALL], ['start', [], Dice::CHAIN_CALL],
], ],
], ],
IHandleUserSessions::class => [ \Friendica\Core\Session\Capability\IHandleUserSessions::class => [
'instanceOf' => \Friendica\Core\Session\Model\UserSession::class, 'instanceOf' => \Friendica\Core\Session\Model\UserSession::class,
], ],
Cookie::class => [ \Friendica\Model\User\Cookie::class => [
'constructParams' => [ 'constructParams' => [
$_COOKIE $cookieVars,
], ],
], ],
ICanWriteToStorage::class => [ \Friendica\Core\Storage\Capability\ICanWriteToStorage::class => [
'instanceOf' => StorageManager::class, 'instanceOf' => \Friendica\Core\Storage\Repository\StorageManager::class,
'call' => [ 'call' => [
['getBackend', [], Dice::CHAIN_CALL], ['getBackend', [], Dice::CHAIN_CALL],
], ],
@ -289,39 +255,45 @@ return [
['create', [], Dice::CHAIN_CALL], ['create', [], Dice::CHAIN_CALL],
], ],
], ],
Network\HTTPClient\Capability\ICanSendHttpRequests::class => [ \Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests::class => [
'instanceOf' => Network\HTTPClient\Factory\HttpClient::class, 'instanceOf' => \Friendica\Network\HTTPClient\Factory\HttpClient::class,
'call' => [ 'call' => [
['createClient', [], Dice::CHAIN_CALL], ['createClient', [], Dice::CHAIN_CALL],
], ],
], ],
ParsedLogIterator::class => [ \Friendica\Model\Log\ParsedLogIterator::class => [
'constructParams' => [ 'constructParams' => [
[Dice::INSTANCE => Util\ReversedFileReader::class], [Dice::INSTANCE => \Friendica\Util\ReversedFileReader::class],
] ]
], ],
\Friendica\Core\Worker\Repository\Process::class => [ \Friendica\Core\Worker\Repository\Process::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER $serverVars
], ],
], ],
App\Request::class => [ \Friendica\App\Request::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER $serverVars
], ],
], ],
\Psr\Clock\ClockInterface::class => [ \Psr\Clock\ClockInterface::class => [
'instanceOf' => Util\Clock\SystemClock::class 'instanceOf' => \Friendica\Util\Clock\SystemClock::class
], ],
\Friendica\Module\Special\HTTPException::class => [ \Friendica\Module\Special\HTTPException::class => [
'constructParams' => [ 'constructParams' => [
$_SERVER $serverVars
], ],
], ],
\Friendica\Module\Api\ApiResponse::class => [ \Friendica\Module\Api\ApiResponse::class => [
'constructParams' => [ 'constructParams' => [
$serverVars,
$getVars['callback'] ?? '',
],
],
];
})(
dirname(__FILE__, 2),
$_GET,
$_SERVER, $_SERVER,
$_GET['callback'] ?? '', $_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();
}
}