From a60eb9e33de785a855f1684d5bc1af1ac6baea89 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 28 Jul 2019 22:06:33 +0200 Subject: [PATCH] Use DICE for Console - Use Friendica\Core\Console as Controller for DI-library - Refactor every console command to use DICE (when possible) - Adjusting tests --- bin/console.php | 4 +- src/Console/ArchiveContact.php | 35 +++++++++--- src/Console/AutomaticInstallation.php | 48 ++++++++++++----- src/Console/Cache.php | 16 ++++-- src/Console/Config.php | 44 ++++++++++----- src/Console/DatabaseStructure.php | 33 ++++++++---- src/Console/GlobalCommunityBlock.php | 26 +++++++-- src/Console/GlobalCommunitySilence.php | 25 +++++++-- src/Console/Maintenance.php | 30 ++++++++--- src/Console/NewPassword.php | 45 +++++++++++----- src/Console/PostUpdate.php | 53 +++++++++++++------ src/Console/ServerBlock.php | 27 ++++++---- src/Console/Typo.php | 16 +++++- src/Core/Console.php | 23 +++++++- src/Core/Installer.php | 8 +-- static/dependencies.config.php | 1 - .../AutomaticInstallationConsoleTest.php | 37 ++++++++++--- tests/src/Console/ConfigConsoleTest.php | 41 +++++++------- tests/src/Console/ConsoleTest.php | 7 --- tests/src/Console/ServerBlockConsoleTest.php | 47 ++++++++-------- 20 files changed, 400 insertions(+), 166 deletions(-) diff --git a/bin/console.php b/bin/console.php index 15fbf437c..3a64d1d2a 100755 --- a/bin/console.php +++ b/bin/console.php @@ -6,6 +6,4 @@ require dirname(__DIR__) . '/vendor/autoload.php'; $dice = new \Dice\Dice(); $dice = $dice->addRules(include __DIR__ . '/../static/dependencies.config.php'); -\Friendica\BaseObject::setDependencyInjection($dice); - -(new Friendica\Core\Console($argv))->execute(); +(new Friendica\Core\Console($dice, $argv))->execute(); diff --git a/src/Console/ArchiveContact.php b/src/Console/ArchiveContact.php index cf177cf2e..9250bbb36 100644 --- a/src/Console/ArchiveContact.php +++ b/src/Console/ArchiveContact.php @@ -2,8 +2,9 @@ namespace Friendica\Console; +use Friendica\App; use Friendica\Core\L10n; -use Friendica\Database\DBA; +use Friendica\Database\Database; use Friendica\Util\Strings; use RuntimeException; @@ -20,6 +21,19 @@ class ArchiveContact extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + /** + * @var Database + */ + private $dba; + /** + * @var L10n\L10n + */ + private $l10n; + protected function getHelp() { $help = <<appMode = $appMode; + $this->dba = $dba; + $this->l10n = $l10n; + } + protected function doExecute() { - $a = \Friendica\BaseObject::getApp(); - if ($this->getOption('v')) { $this->out('Class: ' . __CLASS__); $this->out('Arguments: ' . var_export($this->args, true)); @@ -56,16 +77,16 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - if ($a->getMode()->isInstall()) { + if ($this->appMode->isInstall()) { throw new RuntimeException('Friendica isn\'t properly installed yet.'); } $nurl = Strings::normaliseLink($this->getArgument(0)); - if (!DBA::exists('contact', ['nurl' => $nurl, 'archive' => false])) { + if (!$this->dba->exists('contact', ['nurl' => $nurl, 'archive' => false])) { throw new RuntimeException(L10n::t('Could not find any unarchived contact entry for this URL (%s)', $nurl)); } - if (DBA::update('contact', ['archive' => true], ['nurl' => $nurl])) { - $this->out(L10n::t('The contact entries have been archived')); + if ($this->dba->update('contact', ['archive' => true], ['nurl' => $nurl])) { + $this->out($this->l10n->t('The contact entries have been archived')); } else { throw new RuntimeException('The contact archival failed.'); } diff --git a/src/Console/AutomaticInstallation.php b/src/Console/AutomaticInstallation.php index d594b2605..05756196a 100644 --- a/src/Console/AutomaticInstallation.php +++ b/src/Console/AutomaticInstallation.php @@ -3,10 +3,11 @@ namespace Friendica\Console; use Asika\SimpleConsole\Console; -use Friendica\BaseObject; +use Friendica\App; use Friendica\Core\Config; use Friendica\Core\Installer; use Friendica\Core\Theme; +use Friendica\Database\Database; use Friendica\Util\BasePath; use Friendica\Util\BaseURL; use Friendica\Util\ConfigFileLoader; @@ -14,6 +15,19 @@ use RuntimeException; class AutomaticInstallation extends Console { + /** + * @var App\Mode + */ + private $appMode; + /** + * @var Config\Cache\ConfigCache + */ + private $configCache; + /** + * @var Database + */ + private $dba; + protected function getHelp() { return <<appMode = $appMode; + $this->configCache =$configCache; + $this->dba = $dba; + } + protected function doExecute() { // Initialise the app $this->out("Initializing setup...\n"); - $a = BaseObject::getApp(); - $installer = new Installer(); - $configCache = $a->getConfigCache(); - $basepath = new BasePath($a->getBasePath()); + $configCache = $this->configCache; + $basePathConf = $configCache->get('system', 'basepath'); + $basepath = new BasePath($basePathConf); $installer->setUpCache($configCache, $basepath->getPath()); $this->out(" Complete!\n\n"); @@ -103,13 +125,13 @@ HELP; if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') { // Copy config file $this->out("Copying config file...\n"); - if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) { - throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n"); + if (!copy($basePathConf . DIRECTORY_SEPARATOR . $config_file, $basePathConf . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) { + throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $basePathConf . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n"); } } //reload the config cache - $loader = new ConfigFileLoader($a->getBasePath(), $a->getMode()); + $loader = new ConfigFileLoader($basePathConf); $loader->setupCache($configCache); } else { @@ -159,7 +181,7 @@ HELP; $this->out('The Friendica URL has to be set during CLI installation.'); return 1; } else { - $baseUrl = new BaseURL($a->getConfig(), []); + $baseUrl = new BaseURL($basePathConf, []); $baseUrl->saveByURL($url); } @@ -173,7 +195,7 @@ HELP; $installer->resetChecks(); - if (!$installer->checkDB($configCache, $a->getProfiler())) { + if (!$installer->checkDB($this->dba)) { $errorMessage = $this->extractErrors($installer->getChecks()); throw new RuntimeException($errorMessage); } @@ -185,7 +207,7 @@ HELP; $installer->resetChecks(); - if (!$installer->installDatabase($a->getBasePath())) { + if (!$installer->installDatabase($basePathConf)) { $errorMessage = $this->extractErrors($installer->getChecks()); throw new RuntimeException($errorMessage); } @@ -194,8 +216,8 @@ HELP; // Install theme $this->out("Installing theme\n"); - if (!empty(Config::get('system', 'theme'))) { - Theme::install(Config::get('system', 'theme')); + if (!empty($configCache->get('system', 'theme'))) { + Theme::install($configCache->get('system', 'theme')); $this->out(" Complete\n\n"); } else { $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n"); diff --git a/src/Console/Cache.php b/src/Console/Cache.php index eefb6cc60..e2e1dba6c 100644 --- a/src/Console/Cache.php +++ b/src/Console/Cache.php @@ -20,6 +20,11 @@ class Cache extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + protected function getHelp() { $help = <<appMode = $appMode; + } + protected function doExecute() { - $a = \Friendica\BaseObject::getApp(); - if ($this->getOption('v')) { $this->out('Executable: ' . $this->executable); $this->out('Class: ' . __CLASS__); @@ -65,7 +75,7 @@ HELP; $this->out('Options: ' . var_export($this->options, true)); } - if ($a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) { + if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) { $this->out('Database isn\'t ready or populated yet, database cache won\'t be available'); } diff --git a/src/Console/Config.php b/src/Console/Config.php index a27ca1349..797f63949 100644 --- a/src/Console/Config.php +++ b/src/Console/Config.php @@ -4,7 +4,7 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; use Friendica\App; -use Friendica\Core; +use Friendica\Core\Config\Configuration; use RuntimeException; /** @@ -35,6 +35,15 @@ class Config extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + /** + * @var Configuration + */ + private $config; + protected function getHelp() { $help = <<appMode = $appMode; + $this->config = $config; + } + protected function doExecute() { - $a = \Friendica\BaseObject::getApp(); - if ($this->getOption('v')) { $this->out('Executable: ' . $this->executable); $this->out('Class: ' . __CLASS__); @@ -84,7 +99,7 @@ HELP; throw new CommandArgsException('Too many arguments'); } - if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) { + if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) { $this->out('Database isn\'t ready or populated yet, showing file config only'); } @@ -93,14 +108,14 @@ HELP; $key = $this->getArgument(1); $value = $this->getArgument(2); - if (is_array(Core\Config::get($cat, $key))) { + if (is_array($this->config->get($cat, $key))) { throw new RuntimeException("$cat.$key is an array and can't be set using this command."); } - $result = Core\Config::set($cat, $key, $value); + $result = $this->config->set($cat, $key, $value); if ($result) { $this->out("{$cat}.{$key} <= " . - Core\Config::get($cat, $key)); + $this->config->get($cat, $key)); } else { $this->out("Unable to set {$cat}.{$key}"); } @@ -109,7 +124,7 @@ HELP; if (count($this->args) == 2) { $cat = $this->getArgument(0); $key = $this->getArgument(1); - $value = Core\Config::get($this->getArgument(0), $this->getArgument(1)); + $value = $this->config->get($this->getArgument(0), $this->getArgument(1)); if (is_array($value)) { foreach ($value as $k => $v) { @@ -122,11 +137,12 @@ HELP; if (count($this->args) == 1) { $cat = $this->getArgument(0); - Core\Config::load($cat); + $this->config->load($cat); + $configCache = $this->config->getCache(); - if ($a->getConfigCache()->get($cat) !== null) { + if ($configCache->get($cat) !== null) { $this->out("[{$cat}]"); - $catVal = $a->getConfigCache()->get($cat); + $catVal = $configCache->get($cat); foreach ($catVal as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { @@ -142,13 +158,13 @@ HELP; } if (count($this->args) == 0) { - Core\Config::load(); + $this->config->load(); - if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) { + if ($this->config->get('system', 'config_adapter') == 'jit' && $this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) { $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only'); } - $config = $a->getConfigCache()->getAll(); + $config = $this->config->getCache()->getAll(); foreach ($config as $cat => $section) { if (is_array($section)) { foreach ($section as $key => $value) { diff --git a/src/Console/DatabaseStructure.php b/src/Console/DatabaseStructure.php index 3feaa64d6..ca2100645 100644 --- a/src/Console/DatabaseStructure.php +++ b/src/Console/DatabaseStructure.php @@ -2,9 +2,9 @@ namespace Friendica\Console; -use Friendica\Core; +use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Update; -use Friendica\Database\DBA; +use Friendica\Database\Database; use Friendica\Database\DBStructure; use RuntimeException; @@ -17,6 +17,15 @@ class DatabaseStructure extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var Database + */ + private $dba; + /** + * @var ConfigCache + */ + private $configCache; + protected function getHelp() { $help = <<dba = $dba; + $this->configCache = $configCache; + } + protected function doExecute() { if ($this->getOption('v')) { @@ -56,26 +73,24 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - if (!DBA::connected()) { + if (!$this->dba->isConnected()) { throw new RuntimeException('Unable to connect to database'); } - Core\Config::load(); - - $a = get_app(); + $basePath = $this->configCache->get('system', 'basepath'); switch ($this->getArgument(0)) { case "dryrun": - $output = DBStructure::update($a->getBasePath(), true, false); + $output = DBStructure::update($basePath, true, false); break; case "update": $force = $this->getOption(['f', 'force'], false); $override = $this->getOption(['o', 'override'], false); - $output = Update::run($a->getBasePath(), $force, $override,true, false); + $output = Update::run($basePath, $force, $override,true, false); break; case "dumpsql": ob_start(); - DBStructure::printStructure($a->getBasePath()); + DBStructure::printStructure($basePath); $output = ob_get_clean(); break; case "toinnodb": diff --git a/src/Console/GlobalCommunityBlock.php b/src/Console/GlobalCommunityBlock.php index 12ea63e5d..1789b66d9 100644 --- a/src/Console/GlobalCommunityBlock.php +++ b/src/Console/GlobalCommunityBlock.php @@ -2,6 +2,7 @@ namespace Friendica\Console; +use Friendica\App; use Friendica\Core\L10n; use Friendica\Model\Contact; @@ -20,6 +21,15 @@ class GlobalCommunityBlock extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + /** + * @var L10n\L10n + */ + private $l10n; + protected function getHelp() { $help = <<appMode = $appMode; + $this->l10n = $l10n; + } + protected function doExecute() { - $a = \get_app(); - if ($this->getOption('v')) { $this->out('Class: ' . __CLASS__); $this->out('Arguments: ' . var_export($this->args, true)); @@ -57,18 +73,18 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - if ($a->getMode()->isInstall()) { + if ($this->appMode->isInstall()) { throw new \RuntimeException('Database isn\'t ready or populated yet'); } $contact_id = Contact::getIdForURL($this->getArgument(0)); if (!$contact_id) { - throw new \RuntimeException(L10n::t('Could not find any contact entry for this URL (%s)', $this->getArgument(0))); + throw new \RuntimeException($this->l10n->t('Could not find any contact entry for this URL (%s)', $this->getArgument(0))); } $block_reason = $this->getArgument(1); if(Contact::block($contact_id, $block_reason)) { - $this->out(L10n::t('The contact has been blocked from the node')); + $this->out($this->l10n->t('The contact has been blocked from the node')); } else { throw new \RuntimeException('The contact block failed.'); } diff --git a/src/Console/GlobalCommunitySilence.php b/src/Console/GlobalCommunitySilence.php index 466c1adf5..58feb4507 100644 --- a/src/Console/GlobalCommunitySilence.php +++ b/src/Console/GlobalCommunitySilence.php @@ -2,8 +2,8 @@ namespace Friendica\Console; -use Friendica\BaseObject; -use Friendica\Database\DBA; +use Friendica\App; +use Friendica\Database\Database; use Friendica\Model\Contact; use RuntimeException; @@ -24,6 +24,15 @@ class GlobalCommunitySilence extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + /** + * @var Database + */ + private $dba; + protected function getHelp() { $help = <<appMode = $appMode; + $this->dba =$dba; + } + protected function doExecute() { if ($this->getOption('v')) { @@ -61,13 +78,13 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - if (BaseObject::getApp()->getMode()->isInstall()) { + if ($this->appMode->isInstall()) { throw new RuntimeException('Database isn\'t ready or populated yet'); } $contact_id = Contact::getIdForURL($this->getArgument(0)); if ($contact_id) { - DBA::update('contact', ['hidden' => true], ['id' => $contact_id]); + $this->dba->update('contact', ['hidden' => true], ['id' => $contact_id]); $this->out('The account has been successfully silenced from the global community page.'); } else { throw new RuntimeException('Could not find any public contact entry for this URL (' . $this->getArgument(0) . ')'); diff --git a/src/Console/Maintenance.php b/src/Console/Maintenance.php index 080eb092b..26b77a974 100644 --- a/src/Console/Maintenance.php +++ b/src/Console/Maintenance.php @@ -2,7 +2,8 @@ namespace Friendica\Console; -use Friendica\Core; +use Friendica\App; +use Friendica\Core\Config\Configuration; /** * @brief Sets maintenance mode for this node @@ -13,6 +14,15 @@ class Maintenance extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + /** + * @var Configuration + */ + private $config; + protected function getHelp() { $help = <<appMode = $appMode; + $this->config = $config; + } + protected function doExecute() { - $a = \Friendica\BaseObject::getApp(); - if ($this->getOption('v')) { $this->out('Class: ' . __CLASS__); $this->out('Arguments: ' . var_export($this->args, true)); @@ -61,20 +77,20 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - if ($a->getMode()->isInstall()) { + if ($this->appMode->isInstall()) { throw new \RuntimeException('Database isn\'t ready or populated yet'); } $enabled = intval($this->getArgument(0)); - Core\Config::set('system', 'maintenance', $enabled); + $this->config->set('system', 'maintenance', $enabled); $reason = $this->getArgument(1); if ($enabled && $this->getArgument(1)) { - Core\Config::set('system', 'maintenance_reason', $this->getArgument(1)); + $this->config->set('system', 'maintenance_reason', $this->getArgument(1)); } else { - Core\Config::set('system', 'maintenance_reason', ''); + $this->config->set('system', 'maintenance_reason', ''); } if ($enabled) { diff --git a/src/Console/NewPassword.php b/src/Console/NewPassword.php index dc6943817..e19fd03ba 100644 --- a/src/Console/NewPassword.php +++ b/src/Console/NewPassword.php @@ -2,8 +2,9 @@ namespace Friendica\Console; -use Friendica\Core\L10n; -use Friendica\Database\DBA; +use Friendica\App; +use Friendica\Core\L10n\L10n; +use Friendica\Database\Database; use Friendica\Model\User; use RuntimeException; @@ -20,6 +21,19 @@ class NewPassword extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var App\Mode + */ + private $appMode; + /** + * @var L10n + */ + private $l10n; + /** + * @var Database + */ + private $dba; + protected function getHelp() { $help = <<appMode = $appMode; + $this->l10n = $l10n; + $this->dba = $dba; + } + protected function doExecute() { - $a = \get_app(); - if ($this->getOption('v')) { $this->out('Class: ' . __CLASS__); $this->out('Arguments: ' . var_export($this->args, true)); @@ -56,31 +77,31 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - if ($a->getMode()->isInstall()) { + if ($this->appMode->isInstall()) { throw new RuntimeException('Database isn\'t ready or populated yet'); } $nick = $this->getArgument(0); - $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]); - if (!DBA::isResult($user)) { - throw new RuntimeException(L10n::t('User not found')); + $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]); + if (!$this->dba->isResult($user)) { + throw new RuntimeException($this->l10n->t('User not found')); } $password = $this->getArgument(1); if (is_null($password)) { - $this->out(L10n::t('Enter new password: '), false); + $this->out($this->l10n->t('Enter new password: '), false); $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true); } try { $result = User::updatePassword($user['uid'], $password); - if (!DBA::isResult($result)) { - throw new \Exception(L10n::t('Password update failed. Please try again.')); + if (!$this->dba->isResult($result)) { + throw new \Exception($this->l10n->t('Password update failed. Please try again.')); } - $this->out(L10n::t('Password changed.')); + $this->out($this->l10n->t('Password changed.')); } catch (\Exception $e) { throw new RuntimeException($e->getMessage(), $e->getCode(), $e); } diff --git a/src/Console/PostUpdate.php b/src/Console/PostUpdate.php index 4d7246d90..fb640a0b0 100644 --- a/src/Console/PostUpdate.php +++ b/src/Console/PostUpdate.php @@ -2,8 +2,9 @@ namespace Friendica\Console; -use Friendica\Core\Config; -use Friendica\Core\L10n; +use Friendica\App; +use Friendica\Core\Config\Configuration; +use Friendica\Core\L10n\L10n; use Friendica\Core\Update; /** @@ -16,11 +17,24 @@ use Friendica\Core\Update; */ class PostUpdate extends \Asika\SimpleConsole\Console { - protected $helpOptions = ['h', 'help', '?']; + protected $helpOptions = ['h', 'help', '?']; - protected function getHelp() - { - $help = <<] @@ -29,8 +43,17 @@ Options -h|--help|-? Show help information --reset Reset the post update version HELP; - return $help; - } + return $help; + } + + public function __construct(App\Mode $appMode, Configuration $config, L10n $l10n, array $argv = null) + { + parent::__construct($argv); + + $this->appMode = $appMode; + $this->config = $config; + $this->l10n = $l10n; + } protected function doExecute() { @@ -46,26 +69,26 @@ HELP; $this->out($this->getHelp()); return 0; } elseif ($reset_version) { - Config::set('system', 'post_update_version', $reset_version); - echo L10n::t('Post update version number has been set to %s.', $reset_version) . "\n"; + $this->config->set('system', 'post_update_version', $reset_version); + echo $this->l10n->t('Post update version number has been set to %s.', $reset_version) . "\n"; return 0; } - if ($a->getMode()->isInstall()) { + if ($this->appMode->isInstall()) { throw new \RuntimeException('Database isn\'t ready or populated yet'); } - echo L10n::t('Check for pending update actions.') . "\n"; + echo $this->l10n->t('Check for pending update actions.') . "\n"; Update::run($a->getBasePath(), true, false, true, false); - echo L10n::t('Done.') . "\n"; + echo $this->l10n->t('Done.') . "\n"; - echo L10n::t('Execute pending post updates.') . "\n"; + echo $this->l10n->t('Execute pending post updates.') . "\n"; while (!\Friendica\Database\PostUpdate::update()) { echo '.'; } - echo "\n" . L10n::t('All pending post updates are done.') . "\n"; + echo "\n" . $this->l10n->t('All pending post updates are done.') . "\n"; return 0; } diff --git a/src/Console/ServerBlock.php b/src/Console/ServerBlock.php index 202f7bb75..322f8f6ee 100644 --- a/src/Console/ServerBlock.php +++ b/src/Console/ServerBlock.php @@ -5,7 +5,6 @@ namespace Friendica\Console; use Asika\SimpleConsole\CommandArgsException; use Asika\SimpleConsole\Console; use Console_Table; -use Friendica\BaseObject; use Friendica\Core\Config\Configuration; /** @@ -20,6 +19,11 @@ class ServerBlock extends Console protected $helpOptions = ['h', 'help', '?']; + /** + * @var Configuration + */ + private $config; + protected function getHelp() { $help = <<config = $config; + } + protected function doExecute() { - $a = BaseObject::getApp(); - if (count($this->args) == 0) { - $this->printBlockedServers($a->getConfig()); + $this->printBlockedServers($this->config); return 0; } switch ($this->getArgument(0)) { case 'add': - return $this->addBlockedServer($a->getConfig()); + return $this->addBlockedServer($this->config); case 'remove': - return $this->removeBlockedServer($a->getConfig()); + return $this->removeBlockedServer($this->config); default: throw new CommandArgsException('Unknown command.'); break; @@ -74,7 +83,7 @@ HELP; { $table = new Console_Table(); $table->setHeaders(['Domain', 'Reason']); - $blocklist = $config->get('system', 'blocklist'); + $blocklist = $config->get('system', 'blocklist', []); foreach ($blocklist as $domain) { $table->addRow($domain); } @@ -99,7 +108,7 @@ HELP; $update = false; - $currBlocklist = $config->get('system', 'blocklist'); + $currBlocklist = $config->get('system', 'blocklist', []); $newBlockList = []; foreach ($currBlocklist as $blocked) { if ($blocked['domain'] === $domain) { @@ -150,7 +159,7 @@ HELP; $found = false; - $currBlocklist = $config->get('system', 'blocklist'); + $currBlocklist = $config->get('system', 'blocklist', []); $newBlockList = []; foreach ($currBlocklist as $blocked) { if ($blocked['domain'] === $domain) { diff --git a/src/Console/Typo.php b/src/Console/Typo.php index 216d05723..855c1705c 100644 --- a/src/Console/Typo.php +++ b/src/Console/Typo.php @@ -2,7 +2,7 @@ namespace Friendica\Console; -use Friendica\BaseObject; +use Friendica\Core\Config\Configuration; /** * Tired of chasing typos and finding them after a commit. @@ -14,6 +14,11 @@ class Typo extends \Asika\SimpleConsole\Console { protected $helpOptions = ['h', 'help', '?']; + /** + * @var Configuration + */ + private $config; + protected function getHelp() { $help = <<config = $config; + } + protected function doExecute() { if ($this->getOption('v')) { @@ -43,7 +55,7 @@ HELP; throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); } - $php_path = BaseObject::getApp()->getConfig()->get('config', 'php_path', 'php'); + $php_path = $this->config->get('config', 'php_path', 'php'); if ($this->getOption('v')) { $this->out('Directory: src'); diff --git a/src/Core/Console.php b/src/Core/Console.php index 2893c27b2..e1654fbef 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -2,6 +2,7 @@ namespace Friendica\Core; +use Dice\Dice; use Friendica; /** @@ -15,6 +16,11 @@ class Console extends \Asika\SimpleConsole\Console protected $helpOptions = []; protected $customHelpOptions = ['h', 'help', '?']; + /** + * @var Dice The DI library + */ + protected $dice; + protected function getHelp() { $help = << Friendica\Console\Storage::class, ]; + /** + * CliInput Friendica constructor. + * + * @param Dice $dice The DI library + * @param array $argv + */ + public function __construct(Dice $dice, array $argv = null) + { + parent::__construct($argv); + + $this->dice = $dice; + } + protected function doExecute() { if ($this->getOption('v')) { @@ -125,8 +144,10 @@ HELP; $className = $this->subConsoles[$command]; + Friendica\BaseObject::setDependencyInjection($this->dice); + /** @var Console $subconsole */ - $subconsole = new $className($subargs); + $subconsole = $this->dice->create($className, [$subargs]); foreach ($this->options as $name => $value) { $subconsole->setOption($name, $value); diff --git a/src/Core/Installer.php b/src/Core/Installer.php index d0beedc09..8dcf776a7 100644 --- a/src/Core/Installer.php +++ b/src/Core/Installer.php @@ -7,7 +7,7 @@ namespace Friendica\Core; use DOMDocument; use Exception; use Friendica\Core\Config\Cache\ConfigCache; -use Friendica\Database\DBA; +use Friendica\Database\Database; use Friendica\Database\DBStructure; use Friendica\Object\Image; use Friendica\Util\Network; @@ -597,11 +597,11 @@ class Installer * @return bool true if the check was successful, otherwise false * @throws Exception */ - public function checkDB(ConfigCache $configCache, Profiler $profiler) + public function checkDB(Database $dba) { - DBA::reconnect(); + $dba->reconnect(); - if (DBA::connected()) { + if ($dba->isConnected()) { if (DBStructure::existsTable('user')) { $this->addCheck(L10n::t('Database already in use.'), false, true, ''); diff --git a/static/dependencies.config.php b/static/dependencies.config.php index 7da1cac86..475f5ccee 100644 --- a/static/dependencies.config.php +++ b/static/dependencies.config.php @@ -64,7 +64,6 @@ return [ ], ], Config\Configuration::class => [ - 'shared' => true, 'instanceOf' => Factory\ConfigFactory::class, 'call' => [ ['createConfig', [], Dice::CHAIN_CALL], diff --git a/tests/src/Console/AutomaticInstallationConsoleTest.php b/tests/src/Console/AutomaticInstallationConsoleTest.php index 57a3a4b37..a8c4894b9 100644 --- a/tests/src/Console/AutomaticInstallationConsoleTest.php +++ b/tests/src/Console/AutomaticInstallationConsoleTest.php @@ -2,24 +2,28 @@ namespace Friendica\Test\src\Console; +use Dice\Dice; +use Friendica\App; +use Friendica\BaseObject; use Friendica\Console\AutomaticInstallation; use Friendica\Core\Config\Cache\ConfigCache; use Friendica\Core\Installer; use Friendica\Core\L10n\L10n; use Friendica\Core\Logger; +use Friendica\Database\Database; use Friendica\Test\Util\DBAMockTrait; use Friendica\Test\Util\DBStructureMockTrait; use Friendica\Test\Util\RendererMockTrait; +use Friendica\Test\Util\VFSTrait; use Friendica\Util\BaseURL; use Friendica\Util\Logger\VoidLogger; +use Mockery\MockInterface; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStreamFile; -/** - * @requires PHP 7.0 - */ class AutomaticInstallationConsoleTest extends ConsoleTest { + use VFSTrait; use DBAMockTrait; use DBStructureMockTrait; use RendererMockTrait; @@ -38,28 +42,49 @@ class AutomaticInstallationConsoleTest extends ConsoleTest */ private $configCache; + /** + * @var App\Mode + */ + private $appMode; + + /** + * @var Database + */ + private $dba; + + /** + * @var Dice|MockInterface + */ + private $dice; + public function setUp() { $this->markTestSkipped('Needs class \'Installer\' as constructing argument for console tests'); parent::setUp(); + $this->setUpVfsDir();; + if ($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php')) { $this->root->getChild('config') ->removeChild('local.config.php'); } + $this->dice = \Mockery::mock(Dice::class)->makePartial(); $l10nMock = \Mockery::mock(L10n::class); $l10nMock->shouldReceive('t')->andReturnUsing(function ($args) { return $args; }); - \Friendica\Core\L10n::init($l10nMock); + + $this->dice->shouldReceive('create') + ->with(L10n::class) + ->andReturn($l10nMock); + + BaseObject::setDependencyInjection($this->dice); $this->configCache = new ConfigCache(); $this->configCache->set('system', 'basepath', $this->root->url()); $this->configCache->set('config', 'php_path', trim(shell_exec('which php'))); $this->configCache->set('system', 'theme', 'smarty3'); - $this->mockApp($this->root, true); - $this->configMock->shouldReceive('set')->andReturnUsing(function ($cat, $key, $value) { if ($key !== 'basepath') { return $this->configCache->set($cat, $key, $value); diff --git a/tests/src/Console/ConfigConsoleTest.php b/tests/src/Console/ConfigConsoleTest.php index 8658097f9..e4206af41 100644 --- a/tests/src/Console/ConfigConsoleTest.php +++ b/tests/src/Console/ConfigConsoleTest.php @@ -2,35 +2,38 @@ namespace Friendica\Test\src\Console; +use Friendica\App; use Friendica\App\Mode; use Friendica\Console\Config; +use Friendica\Core\Config\Configuration; +use Mockery\MockInterface; -/** - * @runTestsInSeparateProcesses - * @preserveGlobalState disabled - * @requires PHP 7.0 - */ class ConfigConsoleTest extends ConsoleTest { + /** + * @var App\Mode|MockInterface $appMode + */ + private $appMode; + protected function setUp() { parent::setUp(); - $this->mockApp($this->root); - \Mockery::getConfiguration()->setConstantsMap([ Mode::class => [ 'DBCONFIGAVAILABLE' => 0 ] ]); - $this->mode - ->shouldReceive('has') - ->andReturn(true); + $this->appMode = \Mockery::mock(App\Mode::class); + $this->appMode->shouldReceive('has') + ->andReturn(true); + $this->configMock = \Mockery::mock(Configuration::class); } - function testSetGetKeyValue() { + function testSetGetKeyValue() + { $this->configMock ->shouldReceive('set') ->with('config', 'test', 'now') @@ -42,7 +45,7 @@ class ConfigConsoleTest extends ConsoleTest ->andReturn('now') ->twice(); - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); $console->setArgument(0, 'config'); $console->setArgument(1, 'test'); $console->setArgument(2, 'now'); @@ -55,7 +58,7 @@ class ConfigConsoleTest extends ConsoleTest ->andReturn('now') ->once(); - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]); $console->setArgument(0, 'config'); $console->setArgument(1, 'test'); $txt = $this->dumpExecute($console); @@ -67,7 +70,7 @@ class ConfigConsoleTest extends ConsoleTest ->andReturn(null) ->once(); - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); $console->setArgument(0, 'config'); $console->setArgument(1, 'test'); $txt = $this->dumpExecute($console); @@ -82,7 +85,7 @@ class ConfigConsoleTest extends ConsoleTest ->andReturn($testArray) ->once(); - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); $console->setArgument(0, 'config'); $console->setArgument(1, 'test'); $console->setArgument(2, 'now'); @@ -92,7 +95,7 @@ class ConfigConsoleTest extends ConsoleTest } function testTooManyArguments() { - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); $console->setArgument(0, 'config'); $console->setArgument(1, 'test'); $console->setArgument(2, 'it'); @@ -109,7 +112,7 @@ class ConfigConsoleTest extends ConsoleTest ->with('test', 'it') ->andReturn('now') ->once(); - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, $this->consoleArgv); $console->setArgument(0, 'test'); $console->setArgument(1, 'it'); $console->setOption('v', 1); @@ -142,7 +145,7 @@ CONF; ->with('test', 'it') ->andReturn(NULL) ->once(); - $console = new Config(); + $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]); $console->setArgument(0, 'test'); $console->setArgument(1, 'it'); $console->setArgument(2, 'now'); @@ -183,7 +186,7 @@ Options -v Show more debug information. HELP; - $console = new Config($this->consoleArgv); + $console = new Config($this->appMode, $this->configMock, [$this->consoleArgv]); $console->setOption('help', true); $txt = $this->dumpExecute($console); diff --git a/tests/src/Console/ConsoleTest.php b/tests/src/Console/ConsoleTest.php index 21979e72b..64fab4baf 100644 --- a/tests/src/Console/ConsoleTest.php +++ b/tests/src/Console/ConsoleTest.php @@ -4,15 +4,10 @@ namespace Friendica\Test\src\Console; use Asika\SimpleConsole\Console; use Friendica\Test\MockedTest; -use Friendica\Test\Util\AppMockTrait; use Friendica\Test\Util\Intercept; -use Friendica\Test\Util\VFSTrait; abstract class ConsoleTest extends MockedTest { - use VFSTrait; - use AppMockTrait; - /** * @var array The default argv for a Console Instance */ @@ -23,8 +18,6 @@ abstract class ConsoleTest extends MockedTest parent::setUp(); Intercept::setUp(); - - $this->setUpVfsDir(); } /** diff --git a/tests/src/Console/ServerBlockConsoleTest.php b/tests/src/Console/ServerBlockConsoleTest.php index 7002ca0fa..d671020e6 100644 --- a/tests/src/Console/ServerBlockConsoleTest.php +++ b/tests/src/Console/ServerBlockConsoleTest.php @@ -3,11 +3,8 @@ namespace Friendica\Test\src\Console; use Friendica\Console\ServerBlock; +use Friendica\Core\Config\Configuration; -/** - * @runTestsInSeparateProcesses - * @preserveGlobalState disabled - */ class ServerBlockConsoleTest extends ConsoleTest { protected $defaultBlockList = [ @@ -25,7 +22,7 @@ class ServerBlockConsoleTest extends ConsoleTest { parent::setUp(); - $this->mockApp($this->root); + $this->configMock = \Mockery::mock(Configuration::class); } /** @@ -35,11 +32,11 @@ class ServerBlockConsoleTest extends ConsoleTest { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $txt = $this->dumpExecute($console); $output = <<configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); @@ -79,7 +76,7 @@ CONS; ->andReturn(true) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'add'); $console->setArgument(1, 'testme.now'); $console->setArgument(2, 'I like it!'); @@ -95,7 +92,7 @@ CONS; { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); @@ -111,7 +108,7 @@ CONS; ->andReturn(true) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'add'); $console->setArgument(1, 'testme.now'); $txt = $this->dumpExecute($console); @@ -126,7 +123,7 @@ CONS; { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); @@ -147,7 +144,7 @@ CONS; ->andReturn(true) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'add'); $console->setArgument(1, 'pod.ordoevangelistarum.com'); $console->setArgument(2, 'Other reason'); @@ -163,7 +160,7 @@ CONS; { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); @@ -180,7 +177,7 @@ CONS; ->andReturn(true) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'remove'); $console->setArgument(1, 'pod.ordoevangelistarum.com'); $txt = $this->dumpExecute($console); @@ -193,7 +190,7 @@ CONS; */ public function testBlockedServersWrongCommand() { - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'wrongcommand'); $txt = $this->dumpExecute($console); @@ -207,11 +204,11 @@ CONS; { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'remove'); $console->setArgument(1, 'not.exiting'); $txt = $this->dumpExecute($console); @@ -224,7 +221,7 @@ CONS; */ public function testAddBlockedServerMissingArgument() { - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'add'); $txt = $this->dumpExecute($console); @@ -238,7 +235,7 @@ CONS; { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); @@ -254,7 +251,7 @@ CONS; ->andReturn(false) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'add'); $console->setArgument(1, 'testme.now'); $txt = $this->dumpExecute($console); @@ -269,7 +266,7 @@ CONS; { $this->configMock ->shouldReceive('get') - ->with('system', 'blocklist') + ->with('system', 'blocklist', []) ->andReturn($this->defaultBlockList) ->once(); @@ -286,7 +283,7 @@ CONS; ->andReturn(false) ->once(); - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'remove'); $console->setArgument(1, 'pod.ordoevangelistarum.com'); $txt = $this->dumpExecute($console); @@ -299,7 +296,7 @@ CONS; */ public function testRemoveBlockedServerMissingArgument() { - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setArgument(0, 'remove'); $txt = $this->dumpExecute($console); @@ -311,7 +308,7 @@ CONS; */ public function testBlockedServersHelp() { - $console = new ServerBlock($this->consoleArgv); + $console = new ServerBlock($this->configMock, $this->consoleArgv); $console->setOption('help', true); $txt = $this->dumpExecute($console);