From 4835f1185f7aa028948c3817c51f1aac6e9cbfb1 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Mon, 16 Dec 2019 01:35:26 +0100 Subject: [PATCH] Remove deprecated App::module - replace with DI::module()->getName() --- include/api.php | 26 +++++---- src/App.php | 5 +- src/App/Authentication.php | 3 +- src/Content/Nav.php | 4 +- src/Core/ACL.php | 8 +-- tests/include/ApiTest.php | 70 +++++++++++++++-------- tests/src/Core/Lock/SemaphoreLockTest.php | 3 +- view/theme/frio/php/default.php | 2 +- 8 files changed, 72 insertions(+), 49 deletions(-) diff --git a/include/api.php b/include/api.php index e4a47cf42a..048d07d0a2 100644 --- a/include/api.php +++ b/include/api.php @@ -283,30 +283,35 @@ function api_check_method($method) * @brief Main API entry point * * @param App $a App + * @param App\Arguments $args The app arguments (optional, will retrieved by the DI-Container in case of missing) * @return string|array API call result * @throws Exception */ -function api_call(App $a) +function api_call(App $a, App\Arguments $args = null) { global $API, $called_api; + if ($args == null) { + $args = DI::args(); + } + $type = "json"; - if (strpos(DI::args()->getQueryString(), ".xml") > 0) { + if (strpos($args->getQueryString(), ".xml") > 0) { $type = "xml"; } - if (strpos(DI::args()->getQueryString(), ".json") > 0) { + if (strpos($args->getQueryString(), ".json") > 0) { $type = "json"; } - if (strpos(DI::args()->getQueryString(), ".rss") > 0) { + if (strpos($args->getQueryString(), ".rss") > 0) { $type = "rss"; } - if (strpos(DI::args()->getQueryString(), ".atom") > 0) { + if (strpos($args->getQueryString(), ".atom") > 0) { $type = "atom"; } try { foreach ($API as $p => $info) { - if (strpos(DI::args()->getQueryString(), $p) === 0) { + if (strpos($args->getQueryString(), $p) === 0) { if (!api_check_method($info['method'])) { throw new MethodNotAllowedException(); } @@ -369,7 +374,7 @@ function api_call(App $a) throw new NotImplementedException(); } catch (HTTPException $e) { header("HTTP/1.1 {$e->getCode()} {$e->httpdesc}"); - return api_error($type, $e); + return api_error($type, $e, $args); } } @@ -378,18 +383,17 @@ function api_call(App $a) * * @param string $type Return type (xml, json, rss, as) * @param object $e HTTPException Error object + * @param App\Arguments $args The App arguments * @return string|array error message formatted as $type */ -function api_error($type, $e) +function api_error($type, $e, App\Arguments $args) { - $a = \get_app(); - $error = ($e->getMessage() !== "" ? $e->getMessage() : $e->httpdesc); /// @TODO: https://dev.twitter.com/overview/api/response-codes $error = ["error" => $error, "code" => $e->getCode() . " " . $e->httpdesc, - "request" => DI::args()->getQueryString()]; + "request" => $args->getQueryString()]; $return = api_format_data('status', $type, ['status' => $error]); diff --git a/src/App.php b/src/App.php index 892039dc5a..75fa4f197b 100644 --- a/src/App.php +++ b/src/App.php @@ -57,8 +57,6 @@ class App public $argv; /** @deprecated 2019.09 - use App\Arguments->getArgc() */ public $argc; - /** @deprecated 2019.09 - Use App\Module->getName() instead */ - public $module; public $timezone; public $interactive = true; public $identities; @@ -156,7 +154,7 @@ class App * @param App\Arguments $args The Friendica Arguments of the call * @param Core\Process $process The process methods */ - public function __construct(Database $database, Configuration $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, App\Module $module, App\Page $page, Core\Process $process) + public function __construct(Database $database, Configuration $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, App\Page $page, Core\Process $process) { $this->database = $database; $this->config = $config; @@ -170,7 +168,6 @@ class App $this->argv = $args->getArgv(); $this->argc = $args->getArgc(); - $this->module = $module->getName(); $this->page = $page; $this->load(); diff --git a/src/App/Authentication.php b/src/App/Authentication.php index b8073698f2..22d429c76b 100644 --- a/src/App/Authentication.php +++ b/src/App/Authentication.php @@ -15,6 +15,7 @@ use Friendica\Core\Session; use Friendica\Core\System; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\DI; use Friendica\Model\User; use Friendica\Network\HTTPException; use Friendica\Util\DateTimeFormat; @@ -378,7 +379,7 @@ class Authentication if ($login_initial) { Hook::callAll('logged_in', $a->user); - if ($a->module !== 'home' && $this->session->exists('return_path')) { + if (DI::module()->getName() !== 'home' && $this->session->exists('return_path')) { $this->baseUrl->redirect($this->session->get('return_path')); } } diff --git a/src/Content/Nav.php b/src/Content/Nav.php index 6fca7beb91..3594bae50e 100644 --- a/src/Content/Nav.php +++ b/src/Content/Nav.php @@ -153,7 +153,7 @@ class Nav if (Session::isAuthenticated()) { $nav['logout'] = ['logout', L10n::t('Logout'), '', L10n::t('End this session')]; } else { - $nav['login'] = ['login', L10n::t('Login'), ($a->module == 'login' ? 'selected' : ''), L10n::t('Sign in')]; + $nav['login'] = ['login', L10n::t('Login'), (DI::module()->getName() == 'login' ? 'selected' : ''), L10n::t('Sign in')]; } if (local_user()) { @@ -179,7 +179,7 @@ class Nav $homelink = Session::get('visitor_home', ''); } - if (($a->module != 'home') && (! (local_user()))) { + if ((DI::module()->getName() != 'home') && (! (local_user()))) { $nav['home'] = [$homelink, L10n::t('Home'), '', L10n::t('Home Page')]; } diff --git a/src/Core/ACL.php b/src/Core/ACL.php index d48ef3fa51..a4b4a99190 100644 --- a/src/Core/ACL.php +++ b/src/Core/ACL.php @@ -109,7 +109,7 @@ class ACL $arr = ['contact' => $contacts, 'entry' => $o]; // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow' - Hook::callAll($a->module . '_pre_' . $selname, $arr); + Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr); if (DBA::isResult($contacts)) { foreach ($contacts as $contact) { @@ -127,7 +127,7 @@ class ACL $o .= '' . PHP_EOL; - Hook::callAll($a->module . '_post_' . $selname, $o); + Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o); return $o; } @@ -175,7 +175,7 @@ class ACL $arr = ['contact' => $contacts, 'entry' => $o]; // e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow' - Hook::callAll($a->module . '_pre_' . $selname, $arr); + Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr); $receiverlist = []; @@ -201,7 +201,7 @@ class ACL $o .= implode(', ', $receiverlist); } - Hook::callAll($a->module . '_post_' . $selname, $o); + Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o); return $o; } diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index dcd98fcce9..74c220d849 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -115,8 +115,7 @@ class ApiTest extends DatabaseTest // User ID that we know is not in the database $this->wrongUserId = 666; - $session = DI::session(); - $session->start(); + DI::session()->start(); // Most API require login so we force the session $_SESSION = [ @@ -433,12 +432,14 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path'; $_GET['callback'] = 'callback_name'; - $this->app->query_string = 'api_path'; + $args = DI::args()->determine($_SERVER, $_GET); + $this->assertEquals( 'callback_name(["some_data"])', - api_call($this->app) + api_call($this->app, $args) ); } @@ -457,7 +458,12 @@ class ApiTest extends DatabaseTest return ['data' => ['some_data']]; } ]; + $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path'; + + $args = DI::args()->determine($_SERVER, $_GET); + $this->config->set('system', 'profiler', true); $this->config->set('rendertime', 'callstack', true); $this->app->callstack = [ @@ -468,10 +474,9 @@ class ApiTest extends DatabaseTest 'network' => ['some_function' => 200] ]; - $this->app->query_string = 'api_path'; $this->assertEquals( '["some_data"]', - api_call($this->app) + api_call($this->app, $args) ); } @@ -491,11 +496,13 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path'; + + $args = DI::args()->determine($_SERVER, $_GET); - $this->app->query_string = 'api_path'; $this->assertEquals( '{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}', - api_call($this->app) + api_call($this->app, $args) ); } @@ -529,11 +536,13 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path.json'; + + $args = DI::args()->determine($_SERVER, $_GET); - $this->app->query_string = 'api_path.json'; $this->assertEquals( '["some_data"]', - api_call($this->app) + api_call($this->app, $args) ); } @@ -553,11 +562,13 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path.xml'; + + $args = DI::args()->determine($_SERVER, $_GET); - $this->app->query_string = 'api_path.xml'; $this->assertEquals( 'some_data', - api_call($this->app) + api_call($this->app, $args) ); } @@ -577,12 +588,14 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path.rss'; + + $args = DI::args()->determine($_SERVER, $_GET); - $this->app->query_string = 'api_path.rss'; $this->assertEquals( '' . "\n" . 'some_data', - api_call($this->app) + api_call($this->app, $args) ); } @@ -602,12 +615,14 @@ class ApiTest extends DatabaseTest } ]; $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path.atom'; + + $args = DI::args()->determine($_SERVER, $_GET); - $this->app->query_string = 'api_path.atom'; $this->assertEquals( '' . "\n" . 'some_data', - api_call($this->app) + api_call($this->app, $args) ); } @@ -622,10 +637,13 @@ class ApiTest extends DatabaseTest global $API; $API['api_path'] = ['method' => 'method']; - $this->app->query_string = 'api_path'; + $_SERVER['QUERY_STRING'] = 'q=api_path'; + + $args = DI::args()->determine($_SERVER, $_GET); + $this->assertEquals( '{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}', - api_call($this->app) + api_call($this->app, $args) ); } @@ -642,13 +660,15 @@ class ApiTest extends DatabaseTest 'method' => 'method', 'auth' => true ]; - $_SERVER['REQUEST_METHOD'] = 'method'; $_SESSION['authenticated'] = false; + $_SERVER['REQUEST_METHOD'] = 'method'; + $_SERVER['QUERY_STRING'] = 'q=api_path'; + + $args = DI::args()->determine($_SERVER, $_GET); - $this->app->query_string = 'api_path'; $this->assertEquals( '{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}', - api_call($this->app) + api_call($this->app, $args) ); } @@ -662,7 +682,7 @@ class ApiTest extends DatabaseTest { $this->assertEquals( '{"status":{"error":"error_message","code":"200 OK","request":""}}', - api_error('json', new HTTPException\OKException('error_message')) + api_error('json', new HTTPException\OKException('error_message'), DI::args()) ); } @@ -683,7 +703,7 @@ class ApiTest extends DatabaseTest ' 200 OK' . "\n" . ' ' . "\n" . '' . "\n", - api_error('xml', new HTTPException\OKException('error_message')) + api_error('xml', new HTTPException\OKException('error_message'), DI::args()) ); } @@ -704,7 +724,7 @@ class ApiTest extends DatabaseTest ' 200 OK' . "\n" . ' ' . "\n" . '' . "\n", - api_error('rss', new HTTPException\OKException('error_message')) + api_error('rss', new HTTPException\OKException('error_message'), DI::args()) ); } @@ -725,7 +745,7 @@ class ApiTest extends DatabaseTest ' 200 OK' . "\n" . ' ' . "\n" . '' . "\n", - api_error('atom', new HTTPException\OKException('error_message')) + api_error('atom', new HTTPException\OKException('error_message'), DI::args()) ); } diff --git a/tests/src/Core/Lock/SemaphoreLockTest.php b/tests/src/Core/Lock/SemaphoreLockTest.php index 8a4baa4fa9..040cdeb521 100644 --- a/tests/src/Core/Lock/SemaphoreLockTest.php +++ b/tests/src/Core/Lock/SemaphoreLockTest.php @@ -5,6 +5,7 @@ namespace Friendica\Test\src\Core\Lock; use Dice\Dice; use Friendica\App; use Friendica\Core\Config\Configuration; +use Friendica\Core\Config\JitConfiguration; use Friendica\Core\Lock\SemaphoreLock; use Friendica\DI; @@ -18,7 +19,7 @@ class SemaphoreLockTest extends LockTest $app->shouldReceive('getHostname')->andReturn('friendica.local'); $dice->shouldReceive('create')->with(App::class)->andReturn($app); - $configMock = \Mockery::mock(Configuration::class); + $configMock = \Mockery::mock(JitConfiguration::class); $configMock ->shouldReceive('get') ->with('system', 'temppath', NULL, false) diff --git a/view/theme/frio/php/default.php b/view/theme/frio/php/default.php index cdb0f16673..5579680a33 100644 --- a/view/theme/frio/php/default.php +++ b/view/theme/frio/php/default.php @@ -66,7 +66,7 @@ $is_singleuser_class = $is_singleuser ? "is-singleuser" : "is-not-singleuser"; ?> - "> + "> Skip to main content