From 0d9c7282df24c4853f1c1fb11dfa1d3ac9e5db28 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 6 Oct 2018 11:12:42 +0200 Subject: [PATCH] Used constants when possible when parsing app config --- .../Plugin/ApplicationConfigCustomizer.php | 4 +- .../Plugin/DatabaseConfigCustomizer.php | 12 ++-- .../Plugin/LanguageConfigCustomizer.php | 4 +- .../Plugin/UrlShortenerConfigCustomizer.php | 8 +-- .../src/Model/CustomizableAppConfig.php | 61 ++++++++++--------- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php b/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php index 563ec901..7843833a 100644 --- a/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php @@ -13,8 +13,8 @@ class ApplicationConfigCustomizer implements ConfigCustomizerInterface { use StringUtilsTrait; - private const SECRET = 'SECRET'; - private const DISABLE_TRACK_PARAM = 'DISABLE_TRACK_PARAM'; + public const SECRET = 'SECRET'; + public const DISABLE_TRACK_PARAM = 'DISABLE_TRACK_PARAM'; private const EXPECTED_KEYS = [ self::SECRET, self::DISABLE_TRACK_PARAM, diff --git a/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php b/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php index c003d40f..3039f2af 100644 --- a/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php @@ -16,12 +16,12 @@ class DatabaseConfigCustomizer implements ConfigCustomizerInterface { use AskUtilsTrait; - private const DRIVER = 'DRIVER'; - private const NAME = 'NAME'; - private const USER = 'USER'; - private const PASSWORD = 'PASSWORD'; - private const HOST = 'HOST'; - private const PORT = 'PORT'; + public const DRIVER = 'DRIVER'; + public const NAME = 'NAME'; + public const USER = 'USER'; + public const PASSWORD = 'PASSWORD'; + public const HOST = 'HOST'; + public const PORT = 'PORT'; private const DRIVER_DEPENDANT_OPTIONS = [ self::DRIVER, self::NAME, diff --git a/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php b/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php index f55858fb..96b8b2cb 100644 --- a/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/LanguageConfigCustomizer.php @@ -10,8 +10,8 @@ use function array_keys; class LanguageConfigCustomizer implements ConfigCustomizerInterface { - private const DEFAULT_LANG = 'DEFAULT'; - private const CLI_LANG = 'CLI'; + public const DEFAULT_LANG = 'DEFAULT'; + public const CLI_LANG = 'CLI'; private const EXPECTED_KEYS = [ self::DEFAULT_LANG, self::CLI_LANG, diff --git a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php index 2877c826..6eb5fabf 100644 --- a/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/UrlShortenerConfigCustomizer.php @@ -15,10 +15,10 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface { use AskUtilsTrait; - private const SCHEMA = 'SCHEMA'; - private const HOSTNAME = 'HOSTNAME'; - private const CHARS = 'CHARS'; - private const VALIDATE_URL = 'VALIDATE_URL'; + public const SCHEMA = 'SCHEMA'; + public const HOSTNAME = 'HOSTNAME'; + public const CHARS = 'CHARS'; + public const VALIDATE_URL = 'VALIDATE_URL'; private const EXPECTED_KEYS = [ self::SCHEMA, self::HOSTNAME, diff --git a/module/Installer/src/Model/CustomizableAppConfig.php b/module/Installer/src/Model/CustomizableAppConfig.php index 94da5ec4..253cba21 100644 --- a/module/Installer/src/Model/CustomizableAppConfig.php +++ b/module/Installer/src/Model/CustomizableAppConfig.php @@ -3,6 +3,10 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Installer\Model; +use Shlinkio\Shlink\Installer\Config\Plugin\ApplicationConfigCustomizer; +use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer; +use Shlinkio\Shlink\Installer\Config\Plugin\LanguageConfigCustomizer; +use Shlinkio\Shlink\Installer\Config\Plugin\UrlShortenerConfigCustomizer; use Zend\Stdlib\ArraySerializableInterface; use function Shlinkio\Shlink\Common\array_get_path; use function Shlinkio\Shlink\Common\array_path_exists; @@ -115,29 +119,29 @@ final class CustomizableAppConfig implements ArraySerializableInterface public function exchangeArray(array $array): void { $this->setApp($this->mapExistingPathsToKeys([ - 'SECRET' => ['app_options', 'secret_key'], - 'DISABLE_TRACK_PARAM' => ['app_options', 'disable_track_param'], + ApplicationConfigCustomizer::SECRET => ['app_options', 'secret_key'], + ApplicationConfigCustomizer::DISABLE_TRACK_PARAM => ['app_options', 'disable_track_param'], ], $array)); $this->setDatabase($this->mapExistingPathsToKeys([ - 'DRIVER' => ['entity_manager', 'connection', 'driver'], - 'USER' => ['entity_manager', 'connection', 'user'], - 'PASSWORD' => ['entity_manager', 'connection', 'password'], - 'NAME' => ['entity_manager', 'connection', 'dbname'], - 'HOST' => ['entity_manager', 'connection', 'host'], - 'PORT' => ['entity_manager', 'connection', 'port'], + DatabaseConfigCustomizer::DRIVER => ['entity_manager', 'connection', 'driver'], + DatabaseConfigCustomizer::USER => ['entity_manager', 'connection', 'user'], + DatabaseConfigCustomizer::PASSWORD => ['entity_manager', 'connection', 'password'], + DatabaseConfigCustomizer::NAME => ['entity_manager', 'connection', 'dbname'], + DatabaseConfigCustomizer::HOST => ['entity_manager', 'connection', 'host'], + DatabaseConfigCustomizer::PORT => ['entity_manager', 'connection', 'port'], ], $array)); $this->setLanguage($this->mapExistingPathsToKeys([ - 'DEFAULT' => ['translator', 'locale'], - 'CLI' => ['cli', 'locale'], + LanguageConfigCustomizer::DEFAULT_LANG => ['translator', 'locale'], + LanguageConfigCustomizer::CLI_LANG => ['cli', 'locale'], ], $array)); $this->setUrlShortener($this->mapExistingPathsToKeys([ - 'SCHEMA' => ['url_shortener', 'domain', 'schema'], - 'HOSTNAME' => ['url_shortener', 'domain', 'hostname'], - 'CHARS' => ['url_shortener', 'shortcode_chars'], - 'VALIDATE_URL' => ['url_shortener', 'validate_url'], + UrlShortenerConfigCustomizer::SCHEMA => ['url_shortener', 'domain', 'schema'], + UrlShortenerConfigCustomizer::HOSTNAME => ['url_shortener', 'domain', 'hostname'], + UrlShortenerConfigCustomizer::CHARS => ['url_shortener', 'shortcode_chars'], + UrlShortenerConfigCustomizer::VALIDATE_URL => ['url_shortener', 'validate_url'], ], $array)); } @@ -155,11 +159,11 @@ final class CustomizableAppConfig implements ArraySerializableInterface public function getArrayCopy(): array { - $dbDriver = $this->database['DRIVER'] ?? ''; + $dbDriver = $this->database[DatabaseConfigCustomizer::DRIVER] ?? ''; $config = [ 'app_options' => [ - 'secret_key' => $this->app['SECRET'] ?? '', - 'disable_track_param' => $this->app['DISABLE_TRACK_PARAM'] ?? null, + 'secret_key' => $this->app[ApplicationConfigCustomizer::SECRET] ?? '', + 'disable_track_param' => $this->app[ApplicationConfigCustomizer::DISABLE_TRACK_PARAM] ?? null, ], 'entity_manager' => [ 'connection' => [ @@ -167,18 +171,18 @@ final class CustomizableAppConfig implements ArraySerializableInterface ], ], 'translator' => [ - 'locale' => $this->language['DEFAULT'] ?? 'en', + 'locale' => $this->language[LanguageConfigCustomizer::DEFAULT_LANG] ?? 'en', ], 'cli' => [ - 'locale' => $this->language['CLI'] ?? 'en', + 'locale' => $this->language[LanguageConfigCustomizer::CLI_LANG] ?? 'en', ], 'url_shortener' => [ 'domain' => [ - 'schema' => $this->urlShortener['SCHEMA'] ?? 'http', - 'hostname' => $this->urlShortener['HOSTNAME'] ?? '', + 'schema' => $this->urlShortener[UrlShortenerConfigCustomizer::SCHEMA] ?? 'http', + 'hostname' => $this->urlShortener[UrlShortenerConfigCustomizer::HOSTNAME] ?? '', ], - 'shortcode_chars' => $this->urlShortener['CHARS'] ?? '', - 'validate_url' => $this->urlShortener['VALIDATE_URL'] ?? true, + 'shortcode_chars' => $this->urlShortener[UrlShortenerConfigCustomizer::CHARS] ?? '', + 'validate_url' => $this->urlShortener[UrlShortenerConfigCustomizer::VALIDATE_URL] ?? true, ], ]; @@ -186,11 +190,12 @@ final class CustomizableAppConfig implements ArraySerializableInterface if ($dbDriver === 'pdo_sqlite') { $config['entity_manager']['connection']['path'] = self::SQLITE_DB_PATH; } else { - $config['entity_manager']['connection']['user'] = $this->database['USER'] ?? ''; - $config['entity_manager']['connection']['password'] = $this->database['PASSWORD'] ?? ''; - $config['entity_manager']['connection']['dbname'] = $this->database['NAME'] ?? ''; - $config['entity_manager']['connection']['host'] = $this->database['HOST'] ?? ''; - $config['entity_manager']['connection']['port'] = $this->database['PORT'] ?? ''; + $config['entity_manager']['connection']['user'] = $this->database[DatabaseConfigCustomizer::USER] ?? ''; + $config['entity_manager']['connection']['password'] = + $this->database[DatabaseConfigCustomizer::PASSWORD] ?? ''; + $config['entity_manager']['connection']['dbname'] = $this->database[DatabaseConfigCustomizer::NAME] ?? ''; + $config['entity_manager']['connection']['host'] = $this->database[DatabaseConfigCustomizer::HOST] ?? ''; + $config['entity_manager']['connection']['port'] = $this->database[DatabaseConfigCustomizer::PORT] ?? ''; if ($dbDriver === 'pdo_mysql') { $config['entity_manager']['connection']['driverOptions'] = [