Remove deprecated App::module - replace with DI::module()->getName()

This commit is contained in:
Philipp Holzer 2019-12-16 01:35:26 +01:00
parent 33674808dc
commit 4835f1185f
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
8 changed files with 72 additions and 49 deletions

View file

@ -283,30 +283,35 @@ function api_check_method($method)
* @brief Main API entry point * @brief Main API entry point
* *
* @param App $a App * @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 * @return string|array API call result
* @throws Exception * @throws Exception
*/ */
function api_call(App $a) function api_call(App $a, App\Arguments $args = null)
{ {
global $API, $called_api; global $API, $called_api;
if ($args == null) {
$args = DI::args();
}
$type = "json"; $type = "json";
if (strpos(DI::args()->getQueryString(), ".xml") > 0) { if (strpos($args->getQueryString(), ".xml") > 0) {
$type = "xml"; $type = "xml";
} }
if (strpos(DI::args()->getQueryString(), ".json") > 0) { if (strpos($args->getQueryString(), ".json") > 0) {
$type = "json"; $type = "json";
} }
if (strpos(DI::args()->getQueryString(), ".rss") > 0) { if (strpos($args->getQueryString(), ".rss") > 0) {
$type = "rss"; $type = "rss";
} }
if (strpos(DI::args()->getQueryString(), ".atom") > 0) { if (strpos($args->getQueryString(), ".atom") > 0) {
$type = "atom"; $type = "atom";
} }
try { try {
foreach ($API as $p => $info) { foreach ($API as $p => $info) {
if (strpos(DI::args()->getQueryString(), $p) === 0) { if (strpos($args->getQueryString(), $p) === 0) {
if (!api_check_method($info['method'])) { if (!api_check_method($info['method'])) {
throw new MethodNotAllowedException(); throw new MethodNotAllowedException();
} }
@ -369,7 +374,7 @@ function api_call(App $a)
throw new NotImplementedException(); throw new NotImplementedException();
} catch (HTTPException $e) { } catch (HTTPException $e) {
header("HTTP/1.1 {$e->getCode()} {$e->httpdesc}"); 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 string $type Return type (xml, json, rss, as)
* @param object $e HTTPException Error object * @param object $e HTTPException Error object
* @param App\Arguments $args The App arguments
* @return string|array error message formatted as $type * @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); $error = ($e->getMessage() !== "" ? $e->getMessage() : $e->httpdesc);
/// @TODO: https://dev.twitter.com/overview/api/response-codes /// @TODO: https://dev.twitter.com/overview/api/response-codes
$error = ["error" => $error, $error = ["error" => $error,
"code" => $e->getCode() . " " . $e->httpdesc, "code" => $e->getCode() . " " . $e->httpdesc,
"request" => DI::args()->getQueryString()]; "request" => $args->getQueryString()];
$return = api_format_data('status', $type, ['status' => $error]); $return = api_format_data('status', $type, ['status' => $error]);

View file

@ -57,8 +57,6 @@ class App
public $argv; public $argv;
/** @deprecated 2019.09 - use App\Arguments->getArgc() */ /** @deprecated 2019.09 - use App\Arguments->getArgc() */
public $argc; public $argc;
/** @deprecated 2019.09 - Use App\Module->getName() instead */
public $module;
public $timezone; public $timezone;
public $interactive = true; public $interactive = true;
public $identities; public $identities;
@ -156,7 +154,7 @@ class App
* @param App\Arguments $args The Friendica Arguments of the call * @param App\Arguments $args The Friendica Arguments of the call
* @param Core\Process $process The process methods * @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->database = $database;
$this->config = $config; $this->config = $config;
@ -170,7 +168,6 @@ class App
$this->argv = $args->getArgv(); $this->argv = $args->getArgv();
$this->argc = $args->getArgc(); $this->argc = $args->getArgc();
$this->module = $module->getName();
$this->page = $page; $this->page = $page;
$this->load(); $this->load();

View file

@ -15,6 +15,7 @@ use Friendica\Core\Session;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -378,7 +379,7 @@ class Authentication
if ($login_initial) { if ($login_initial) {
Hook::callAll('logged_in', $a->user); 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')); $this->baseUrl->redirect($this->session->get('return_path'));
} }
} }

View file

@ -153,7 +153,7 @@ class Nav
if (Session::isAuthenticated()) { if (Session::isAuthenticated()) {
$nav['logout'] = ['logout', L10n::t('Logout'), '', L10n::t('End this session')]; $nav['logout'] = ['logout', L10n::t('Logout'), '', L10n::t('End this session')];
} else { } 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()) { if (local_user()) {
@ -179,7 +179,7 @@ class Nav
$homelink = Session::get('visitor_home', ''); $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')]; $nav['home'] = [$homelink, L10n::t('Home'), '', L10n::t('Home Page')];
} }

View file

@ -109,7 +109,7 @@ class ACL
$arr = ['contact' => $contacts, 'entry' => $o]; $arr = ['contact' => $contacts, 'entry' => $o];
// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow' // 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)) { if (DBA::isResult($contacts)) {
foreach ($contacts as $contact) { foreach ($contacts as $contact) {
@ -127,7 +127,7 @@ class ACL
$o .= '</select>' . PHP_EOL; $o .= '</select>' . PHP_EOL;
Hook::callAll($a->module . '_post_' . $selname, $o); Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o);
return $o; return $o;
} }
@ -175,7 +175,7 @@ class ACL
$arr = ['contact' => $contacts, 'entry' => $o]; $arr = ['contact' => $contacts, 'entry' => $o];
// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow' // 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 = []; $receiverlist = [];
@ -201,7 +201,7 @@ class ACL
$o .= implode(', ', $receiverlist); $o .= implode(', ', $receiverlist);
} }
Hook::callAll($a->module . '_post_' . $selname, $o); Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o);
return $o; return $o;
} }

View file

@ -115,8 +115,7 @@ class ApiTest extends DatabaseTest
// User ID that we know is not in the database // User ID that we know is not in the database
$this->wrongUserId = 666; $this->wrongUserId = 666;
$session = DI::session(); DI::session()->start();
$session->start();
// Most API require login so we force the session // Most API require login so we force the session
$_SESSION = [ $_SESSION = [
@ -433,12 +432,14 @@ class ApiTest extends DatabaseTest
} }
]; ];
$_SERVER['REQUEST_METHOD'] = 'method'; $_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'q=api_path';
$_GET['callback'] = 'callback_name'; $_GET['callback'] = 'callback_name';
$this->app->query_string = 'api_path'; $args = DI::args()->determine($_SERVER, $_GET);
$this->assertEquals( $this->assertEquals(
'callback_name(["some_data"])', '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']]; return ['data' => ['some_data']];
} }
]; ];
$_SERVER['REQUEST_METHOD'] = 'method'; $_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('system', 'profiler', true);
$this->config->set('rendertime', 'callstack', true); $this->config->set('rendertime', 'callstack', true);
$this->app->callstack = [ $this->app->callstack = [
@ -468,10 +474,9 @@ class ApiTest extends DatabaseTest
'network' => ['some_function' => 200] 'network' => ['some_function' => 200]
]; ];
$this->app->query_string = 'api_path';
$this->assertEquals( $this->assertEquals(
'["some_data"]', '["some_data"]',
api_call($this->app) api_call($this->app, $args)
); );
} }
@ -491,11 +496,13 @@ class ApiTest extends DatabaseTest
} }
]; ];
$_SERVER['REQUEST_METHOD'] = 'method'; $_SERVER['REQUEST_METHOD'] = 'method';
$_SERVER['QUERY_STRING'] = 'q=api_path';
$args = DI::args()->determine($_SERVER, $_GET);
$this->app->query_string = 'api_path';
$this->assertEquals( $this->assertEquals(
'{"status":{"error":"Internal Server Error","code":"500 Internal Server Error","request":"api_path"}}', '{"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['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( $this->assertEquals(
'["some_data"]', '["some_data"]',
api_call($this->app) api_call($this->app, $args)
); );
} }
@ -553,11 +562,13 @@ class ApiTest extends DatabaseTest
} }
]; ];
$_SERVER['REQUEST_METHOD'] = 'method'; $_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( $this->assertEquals(
'some_data', 'some_data',
api_call($this->app) api_call($this->app, $args)
); );
} }
@ -577,12 +588,14 @@ class ApiTest extends DatabaseTest
} }
]; ];
$_SERVER['REQUEST_METHOD'] = 'method'; $_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( $this->assertEquals(
'<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'some_data', 'some_data',
api_call($this->app) api_call($this->app, $args)
); );
} }
@ -602,12 +615,14 @@ class ApiTest extends DatabaseTest
} }
]; ];
$_SERVER['REQUEST_METHOD'] = 'method'; $_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( $this->assertEquals(
'<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
'some_data', 'some_data',
api_call($this->app) api_call($this->app, $args)
); );
} }
@ -622,10 +637,13 @@ class ApiTest extends DatabaseTest
global $API; global $API;
$API['api_path'] = ['method' => 'method']; $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( $this->assertEquals(
'{"status":{"error":"Method Not Allowed","code":"405 Method Not Allowed","request":"api_path"}}', '{"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', 'method' => 'method',
'auth' => true 'auth' => true
]; ];
$_SERVER['REQUEST_METHOD'] = 'method';
$_SESSION['authenticated'] = false; $_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( $this->assertEquals(
'{"status":{"error":"This API requires login","code":"401 Unauthorized","request":"api_path"}}', '{"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( $this->assertEquals(
'{"status":{"error":"error_message","code":"200 OK","request":""}}', '{"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
' <code>200 OK</code>' . "\n" . ' <code>200 OK</code>' . "\n" .
' <request/>' . "\n" . ' <request/>' . "\n" .
'</status>' . "\n", '</status>' . "\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
' <code>200 OK</code>' . "\n" . ' <code>200 OK</code>' . "\n" .
' <request/>' . "\n" . ' <request/>' . "\n" .
'</status>' . "\n", '</status>' . "\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
' <code>200 OK</code>' . "\n" . ' <code>200 OK</code>' . "\n" .
' <request/>' . "\n" . ' <request/>' . "\n" .
'</status>' . "\n", '</status>' . "\n",
api_error('atom', new HTTPException\OKException('error_message')) api_error('atom', new HTTPException\OKException('error_message'), DI::args())
); );
} }

View file

@ -5,6 +5,7 @@ namespace Friendica\Test\src\Core\Lock;
use Dice\Dice; use Dice\Dice;
use Friendica\App; use Friendica\App;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Config\JitConfiguration;
use Friendica\Core\Lock\SemaphoreLock; use Friendica\Core\Lock\SemaphoreLock;
use Friendica\DI; use Friendica\DI;
@ -18,7 +19,7 @@ class SemaphoreLockTest extends LockTest
$app->shouldReceive('getHostname')->andReturn('friendica.local'); $app->shouldReceive('getHostname')->andReturn('friendica.local');
$dice->shouldReceive('create')->with(App::class)->andReturn($app); $dice->shouldReceive('create')->with(App::class)->andReturn($app);
$configMock = \Mockery::mock(Configuration::class); $configMock = \Mockery::mock(JitConfiguration::class);
$configMock $configMock
->shouldReceive('get') ->shouldReceive('get')
->with('system', 'temppath', NULL, false) ->with('system', 'temppath', NULL, false)

View file

@ -66,7 +66,7 @@ $is_singleuser_class = $is_singleuser ? "is-singleuser" : "is-not-singleuser";
?> ?>
</head> </head>
<body id="top" class="mod-<?php echo $a->module . " " . $is_singleuser_class . " " . $view_mode_class;?>"> <body id="top" class="mod-<?php echo DI::module()->getName() . " " . $is_singleuser_class . " " . $view_mode_class;?>">
<a href="#content" class="sr-only sr-only-focusable">Skip to main content</a> <a href="#content" class="sr-only sr-only-focusable">Skip to main content</a>
<?php <?php
if (!empty($page['nav']) && !$minimal) { if (!empty($page['nav']) && !$minimal) {