From 505d8c18d08cd44e502eef4ffdbb22f3912a3368 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 14 Mar 2019 02:36:49 +0100 Subject: [PATCH] Move AppMode --- src/App.php | 34 +++++++++------------ src/Core/Config/Cache/ConfigCacheLoader.php | 21 +++++++++++-- src/Factory/DependencyFactory.php | 7 +++-- src/Factory/LoggerFactory.php | 1 + tests/DatabaseTest.php | 6 ++-- tests/include/ApiTest.php | 7 +++-- tests/src/Database/DBATest.php | 7 +++-- tests/src/Database/DBStructureTest.php | 7 +++-- 8 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/App.php b/src/App.php index f48abcf9cb..ee7d29b32d 100644 --- a/src/App.php +++ b/src/App.php @@ -163,6 +163,16 @@ class App return $this->profiler; } + /** + * Returns the Mode of the Application + * + * @return App\Mode The Application Mode + */ + public function getMode() + { + return $this->mode; + } + /** * Register a stylesheet file path to be included in the tag of every page. * Inclusion is done in App->initHead(). @@ -206,19 +216,21 @@ class App * @brief App constructor. * * @param Configuration $config The Configuration + * @param App\Mode $mode The mode of this Friendica app * @param LoggerInterface $logger The current app logger * @param Profiler $profiler The profiler of this application * @param bool $isBackend Whether it is used for backend or frontend (Default true=backend) * * @throws Exception if the Basepath is not usable */ - public function __construct(Configuration $config, LoggerInterface $logger, Profiler $profiler, $isBackend = true) + public function __construct(Configuration $config, App\Mode $mode, LoggerInterface $logger, Profiler $profiler, $isBackend = true) { BaseObject::setApp($this); $this->logger = $logger; $this->config = $config; $this->profiler = $profiler; + $this->mode = $mode; $this->basePath = $this->config->get('system', 'basepath'); if (!Core\System::isDirectoryUsable($this->basePath, false)) { @@ -231,8 +243,6 @@ class App $this->profiler->reset(); - $this->mode = new App\Mode($this->basePath); - $this->reload(); set_time_limit(0); @@ -332,22 +342,6 @@ class App Core\Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); } - /** - * Returns the Mode of the Application - * - * @return App\Mode The Application Mode - * - * @throws InternalServerErrorException when the mode isn't created - */ - public function getMode() - { - if (empty($this->mode)) { - throw new InternalServerErrorException('Mode of the Application is not defined'); - } - - return $this->mode; - } - /** * Reloads the whole app instance */ @@ -359,7 +353,7 @@ class App if ($this->getMode()->has(App\Mode::DBAVAILABLE)) { Core\Hook::loadHooks(); - $loader = new ConfigCacheLoader($this->basePath); + $loader = new ConfigCacheLoader($this->basePath, $this->mode); Core\Hook::callAll('load_config', $loader); $this->config->getCache()->load($loader->loadCoreConfig('addon'), true); } diff --git a/src/Core/Config/Cache/ConfigCacheLoader.php b/src/Core/Config/Cache/ConfigCacheLoader.php index 44a01f467d..0da16a5f47 100644 --- a/src/Core/Config/Cache/ConfigCacheLoader.php +++ b/src/Core/Config/Cache/ConfigCacheLoader.php @@ -2,6 +2,7 @@ namespace Friendica\Core\Config\Cache; +use Friendica\App; use Friendica\Core\Addon; /** @@ -23,8 +24,14 @@ class ConfigCacheLoader private $baseDir; private $configDir; - public function __construct($baseDir) + /** + * @var App\Mode + */ + private $appMode; + + public function __construct($baseDir, App\Mode $mode) { + $this->appMode = $mode; $this->baseDir = $baseDir; $this->configDir = $baseDir . DIRECTORY_SEPARATOR . self::SUBDIRECTORY; } @@ -34,8 +41,12 @@ class ConfigCacheLoader * * First loads the default value for all the configuration keys, then the legacy configuration files, then the * expected local.config.php + * + * @param IConfigCache The config cache to load to + * + * @throws \Exception */ - public function loadConfigFiles(ConfigCache $config) + public function loadConfigFiles(IConfigCache $config) { // Setting at least the basepath we know $config->set('system', 'basepath', $this->baseDir); @@ -47,6 +58,12 @@ class ConfigCacheLoader $config->load($this->loadLegacyConfig('htconfig'), true); $config->load($this->loadCoreConfig('local'), true); + + // In case of install mode, add the found basepath (because there isn't a basepath set yet + if ($this->appMode->isInstall()) { + // Setting at least the basepath we know + $config->set('system', 'basepath', $this->baseDir); + } } /** diff --git a/src/Factory/DependencyFactory.php b/src/Factory/DependencyFactory.php index 9ada2bc5d8..8cc63d05c3 100644 --- a/src/Factory/DependencyFactory.php +++ b/src/Factory/DependencyFactory.php @@ -22,8 +22,9 @@ class DependencyFactory */ public static function setUp($channel, $directory, $isBackend = true) { - $basedir = BasePath::create($directory, $_SERVER); - $configLoader = new Cache\ConfigCacheLoader($basedir); + $basePath = BasePath::create($directory, $_SERVER); + $mode = new App\Mode($basePath); + $configLoader = new Cache\ConfigCacheLoader($basePath, $mode); $configCache = Factory\ConfigFactory::createCache($configLoader); $profiler = Factory\ProfilerFactory::create($configCache); Factory\DBFactory::init($configCache, $profiler, $_SERVER); @@ -32,6 +33,6 @@ class DependencyFactory Factory\ConfigFactory::createPConfig($configCache); $logger = Factory\LoggerFactory::create($channel, $config, $profiler); - return new App($config, $logger, $profiler, $isBackend); + return new App($config, $mode, $logger, $profiler, $isBackend); } } diff --git a/src/Factory/LoggerFactory.php b/src/Factory/LoggerFactory.php index 3136b30101..9f741aac15 100644 --- a/src/Factory/LoggerFactory.php +++ b/src/Factory/LoggerFactory.php @@ -222,6 +222,7 @@ class LoggerFactory if (!is_int($loglevel)) { $loglevel = LogLevel::NOTICE; } + $fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel); $formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n"); diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index dde61e856b..8f24141b16 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -5,6 +5,7 @@ namespace Friendica\Test; +use Friendica\App; use Friendica\Core\Config\Cache; use Friendica\Database\DBA; use Friendica\Factory; @@ -40,8 +41,9 @@ abstract class DatabaseTest extends MockedTest $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.'); } - $basedir = BasePath::create(dirname(__DIR__)); - $configLoader = new Cache\ConfigCacheLoader($basedir); + $basepath = BasePath::create(dirname(__DIR__)); + $mode = new App\Mode($basepath); + $configLoader = new Cache\ConfigCacheLoader($basepath, $mode); $config = Factory\ConfigFactory::createCache($configLoader); $profiler = \Mockery::mock(Profiler::class); diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index c43deea405..bccf30f924 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -36,15 +36,16 @@ class ApiTest extends DatabaseTest */ public function setUp() { - $basedir = BasePath::create(dirname(__DIR__) . '/../'); - $configLoader = new Cache\ConfigCacheLoader($basedir); + $basePath = BasePath::create(dirname(__DIR__) . '/../'); + $mode = new App\Mode($basePath); + $configLoader = new Cache\ConfigCacheLoader($basePath, $mode); $configCache = Factory\ConfigFactory::createCache($configLoader); $profiler = Factory\ProfilerFactory::create($configCache); Factory\DBFactory::init($configCache, $profiler, $_SERVER); $config = Factory\ConfigFactory::createConfig($configCache); Factory\ConfigFactory::createPConfig($configCache); $logger = Factory\LoggerFactory::create('test', $config, $profiler); - $this->app = new App($config, $logger, $profiler, false); + $this->app = new App($config, $mode, $logger, $profiler, false); parent::setUp(); diff --git a/tests/src/Database/DBATest.php b/tests/src/Database/DBATest.php index c6f2fae6dd..90e7486817 100644 --- a/tests/src/Database/DBATest.php +++ b/tests/src/Database/DBATest.php @@ -13,15 +13,16 @@ class DBATest extends DatabaseTest { public function setUp() { - $basedir = BasePath::create(dirname(__DIR__) . '/../../'); - $configLoader = new Cache\ConfigCacheLoader($basedir); + $basePath = BasePath::create(dirname(__DIR__) . '/../../'); + $mode = new App\Mode($basePath); + $configLoader = new Cache\ConfigCacheLoader($basePath, $mode); $configCache = Factory\ConfigFactory::createCache($configLoader); $profiler = Factory\ProfilerFactory::create($configCache); Factory\DBFactory::init($configCache, $profiler, $_SERVER); $config = Factory\ConfigFactory::createConfig($configCache); Factory\ConfigFactory::createPConfig($configCache); $logger = Factory\LoggerFactory::create('test', $config, $profiler); - $this->app = new App($config, $logger, $profiler, false); + $this->app = new App($config, $mode, $logger, $profiler, false); parent::setUp(); diff --git a/tests/src/Database/DBStructureTest.php b/tests/src/Database/DBStructureTest.php index fde41dab01..00a6955f76 100644 --- a/tests/src/Database/DBStructureTest.php +++ b/tests/src/Database/DBStructureTest.php @@ -13,15 +13,16 @@ class DBStructureTest extends DatabaseTest { public function setUp() { - $basedir = BasePath::create(dirname(__DIR__) . '/../../'); - $configLoader = new Cache\ConfigCacheLoader($basedir); + $basePath = BasePath::create(dirname(__DIR__) . '/../../'); + $mode = new App\Mode($basePath); + $configLoader = new Cache\ConfigCacheLoader($basePath, $mode); $configCache = Factory\ConfigFactory::createCache($configLoader); $profiler = Factory\ProfilerFactory::create($configCache); Factory\DBFactory::init($configCache, $profiler, $_SERVER); $config = Factory\ConfigFactory::createConfig($configCache); Factory\ConfigFactory::createPConfig($configCache); $logger = Factory\LoggerFactory::create('test', $config, $profiler); - $this->app = new App($config, $logger, $profiler, false); + $this->app = new App($config, $mode, $logger, $profiler, false); parent::setUp(); }