Permalink
Browse files

ESDEV-4156 Extract system requirements

Has similar goals as "Move non controller methods":

* Extract system requirements information processing class
  as a separate unit;
* Faster visual scan of all available controllers

A new class `MouduleStateMapGenerator` was created to help and
manage results from System Requirements Info array.
  • Loading branch information...
1 parent e14efd2 commit 5a5d2a5c391498105efc00b934f82c4339ee0e25 @rezonanc-oxid rezonanc-oxid committed Dec 15, 2016
@@ -16,31 +16,32 @@
* along with OXID eShop Community Edition. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://www.oxid-esales.com
- * @copyright (C) OXID eSales AG 2003-2016
+ * @copyright (C) OXID eSales AG 2003-2017
* @version OXID eShop CE
*/
namespace OxidEsales\EshopCommunity\Core;
-use Exception;
use OxidEsales\EshopCommunity\Core\Exception\SystemComponentException;
-use oxDb;
use OxidEsales\Eshop\Core\Database\Adapter\ResultSetInterface;
+use OxidEsales\Eshop\Core\Edition\EditionSelector;
use oxRegistry;
+use oxDb;
+use Exception;
/**
* System requirements class.
*/
class SystemRequirements
{
- /** Your system does not fit the requirement */
- const REQUIREMENT_FITS_NOT = 0;
-
- /** The requirement is not or only partly fit */
- const REQUIREMENT_PARTLY_FITS = 1;
+ const MODULE_STATUS_UNABLE_TO_DETECT = -1;
+ const MODULE_STATUS_BLOCKS_SETUP = 0;
+ const MODULE_STATUS_FITS_MINIMUM_REQUIREMENTS = 1;
+ const MODULE_STATUS_OK = 2;
- /** Your system fits the requirement */
- const REQUIREMENT_FITS = 2;
+ const MODULE_GROUP_ID_SERVER_CONFIG = 'server_config';
+ const MODULE_ID_MOD_REWRITE = 'mod_rewrite';
+ const MODULE_ID_MYSQL_VERSION = 'mysql_version';
/**
* System required modules
@@ -581,17 +582,17 @@ public function checkPhpVersion()
$installedPhpVersion = $this->getPhpVersion();
if (version_compare($installedPhpVersion, $minimalRequiredVersion, '<')) {
- $requirementFits = static::REQUIREMENT_FITS_NOT;
+ $requirementFits = static::MODULE_STATUS_BLOCKS_SETUP;
}
if (is_null($requirementFits) &&
version_compare($installedPhpVersion, $minimalRecommendedVersion, '>=')
&& version_compare($installedPhpVersion, $maximalRecommendedVersion, '<=')) {
- $requirementFits = static::REQUIREMENT_FITS;
+ $requirementFits = static::MODULE_STATUS_OK;
}
if (is_null($requirementFits)) {
- $requirementFits = static::REQUIREMENT_PARTLY_FITS;
+ $requirementFits = static::MODULE_STATUS_FITS_MINIMUM_REQUIREMENTS;
}
return $requirementFits;
@@ -718,7 +719,7 @@ public function checkMysqlVersion($installedVersion = null)
}
if (version_compare($installedVersion, $minimalRequiredVersion, '<')) {
- $requirementFits = static::REQUIREMENT_FITS_NOT;
+ $requirementFits = static::MODULE_STATUS_BLOCKS_SETUP;
}
/**
@@ -730,18 +731,18 @@ public function checkMysqlVersion($installedVersion = null)
version_compare($installedVersion, '5.6.0', '>=') &&
version_compare($installedVersion, '5.7.0', '<')
) {
- $requirementFits = static::REQUIREMENT_PARTLY_FITS;
+ $requirementFits = static::MODULE_STATUS_FITS_MINIMUM_REQUIREMENTS;
}
if (is_null($requirementFits) &&
version_compare($installedVersion, $minimalRequiredVersion, '>=') &&
version_compare($installedVersion, $maximalRequiredVersion, '<=')
) {
- $requirementFits = static::REQUIREMENT_FITS;
+ $requirementFits = static::MODULE_STATUS_OK;
}
if (is_null($requirementFits)) {
- $requirementFits = static::REQUIREMENT_PARTLY_FITS;
+ $requirementFits = static::MODULE_STATUS_FITS_MINIMUM_REQUIREMENTS;
}
return $requirementFits;
@@ -945,6 +946,27 @@ public function getSystemInfo()
}
/**
+ * Apply given filter function to all iterations of SystemRequirementInfo array.
+ *
+ * @param array $systemRequirementsInfo
+ * @param \Closure $filterFunction Filter function used for the update of actual values; Function will
+ * receive the same arguments as provided from
+ * `iterateThroughSystemRequirementsInfo` method.
+ *
+ * @return array An array which is in the same format as the main input argument but with updated data.
+ */
+ public static function filter($systemRequirementsInfo, $filterFunction)
+ {
+ $iterator = static::iterateThroughSystemRequirementsInfo($systemRequirementsInfo);
+
+ foreach ($iterator as list($groupId, $moduleId, $moduleState)) {
+ $systemRequirementsInfo[$groupId][$moduleId] = $filterFunction($groupId, $moduleId, $moduleState);
+ }
+
+ return $systemRequirementsInfo;
+ }
+
+ /**
* Returns passed module state
*
* @param string $sModule module name to check
@@ -963,6 +985,44 @@ public function getModuleInfo($sModule = null)
}
/**
+ * Returns true if given module state is acceptable for setup process to continue.
+ *
+ * @param array $systemRequirementsInfo
+ * @return bool
+ */
+ public static function canSetupContinue($systemRequirementsInfo)
+ {
+ $iterator = static::iterateThroughSystemRequirementsInfo($systemRequirementsInfo);
+
+ foreach ($iterator as list($groupId, $moduleId, $moduleState)) {
+ if ($moduleState === static::MODULE_STATUS_BLOCKS_SETUP) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Iterates through given SystemRequirementsInfo returning three items:
+ *
+ * - GroupId
+ * - ModuleId
+ * - ModuleState
+ *
+ * @param array $systemRequirementsInfo
+ * @return \Generator Iterator which yields [group_id, module_id, module_state].
+ */
+ public static function iterateThroughSystemRequirementsInfo($systemRequirementsInfo)
+ {
+ foreach ($systemRequirementsInfo as $groupId => $modules) {
+ foreach ($modules as $moduleId => $moduleState) {
+ yield [$groupId, $moduleId, $moduleState];
+ }
+ }
+ }
+
+ /**
* Returns or prints url for info about missing web service configuration
*
* @param string $sIdent Module identifier
@@ -16,14 +16,16 @@
* along with OXID eShop Community Edition. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://www.oxid-esales.com
- * @copyright (C) OXID eSales AG 2003-2016
+ * @copyright (C) OXID eSales AG 2003-2017
* @version OXID eShop CE
*/
namespace OxidEsales\EshopCommunity\Setup;
use Exception;
use OxidEsales\Eshop\Core\Edition\EditionSelector;
+use OxidEsales\Eshop\Core\SystemRequirements;
+use OxidEsales\EshopCommunity\Setup\Controller\ModuleStateMapGenerator;
/**
* Class holds scripts (controllers) needed to perform shop setup steps
@@ -44,54 +46,28 @@ public function __construct()
/**
* First page with system requirements check
*
+ * Functionality is tested via:
+ * `Acceptance/Frontend/ShopSetUpTest.php::testSystemRequirementsPageCanContinueWithSetup`
+ * `Acceptance/Frontend/ShopSetUpTest.php::testSystemRequirementsPageShowsTranslatedModuleNames`
+ * `Acceptance/Frontend/ShopSetUpTest.php::testSystemRequirementsPageShowsTranslatedModuleGroupNames`
+ * `Acceptance/Frontend/ShopSetUpTest.php::testSystemRequirementsContainsProperModuleStateHtmlClassNames`
+ * `Acceptance/Frontend/ShopSetUpTest.php::testInstallShopCantContinueDueToHtaccessProblem`
+ *
* @return string
*/
public function systemReq()
{
- $setup = $this->getSetupInstance();
- $language = $this->getLanguageInstance();
- $utils = $this->getUtilitiesInstance();
-
- $continue = true;
- $groupModuleInfo = array();
-
- $htaccessUpdateError = false;
- try {
- $path = $utils->getDefaultPathParams();
- $path['sBaseUrlPath'] = $utils->extractRewriteBase($path['sShopURL']);
- //$oUtils->updateHtaccessFile( $aPath, "admin" );
- $utils->updateHtaccessFile($path);
- } catch (Exception $exception) {
- //$oView->setMessage( $oExcp->getMessage() );
- $htaccessUpdateError = true;
- }
+ $systemRequirementsInfo = $this->getSystemRequirementsInfo();
+ $moduleStateMapGenerator = $this->getModuleStateMapGenerator($systemRequirementsInfo);
- $info = $this->getSystemInformation();
- foreach ($info as $groupName => $modules) {
- // translating
- $translatedGroupName = $language->getModuleName($groupName);
- foreach ($modules as $moduleName => $moduleState) {
- // translating
- $continue = $continue && ( bool )abs($moduleState);
-
- // was unable to update htaccess file for mod_rewrite check
- if ($htaccessUpdateError && $moduleName == 'server_permissions') {
- $class = $setup->getModuleClass(0);
- $continue = false;
- } else {
- $class = $setup->getModuleClass($moduleState);
- }
- $groupModuleInfo[$translatedGroupName][] = array('module' => $moduleName,
- 'class' => $class,
- 'modulename' => $language->getModuleName($moduleName));
- }
- }
+ $moduleStateMap = $moduleStateMapGenerator->getModuleStateMap();
+ $isSafeForSetupToContinue = SystemRequirements::canSetupContinue($systemRequirementsInfo);
$this->setViewOptions(
'STEP_0_TITLE',
[
- "blContinue" => $continue,
- "aGroupModuleInfo" => $groupModuleInfo,
+ "blContinue" => $isSafeForSetupToContinue,
+ "aGroupModuleInfo" => $moduleStateMap,
"aLanguages" => getLanguages(),
"sLanguage" => $this->getSessionInstance()->getSessionParam('setup_lang'),
]
@@ -654,18 +630,95 @@ protected function setViewOptions($titleId, $viewOptions)
}
/**
- * Cause the system requirement object is used on multiple places and we need an adjustment (a "don't know now" for
- * the MySQL version), we wrap it here.
+ * Getter for ModuleStateMapGenerator.
+ *
+ * Returns an instance of ModuleStateMapGenerator which has all necessary functions predefined:
+ *
+ * - StateHtmlClassConverterFunction to convert module state to HTML class attribute for setup page;
+ * - ModuleNameTranslateFunction to translate requirement module id to it's full name;
+ * - ModuleGroupNameTranslateFunction to translate requirement module group id to it's full name.
+ *
+ * @param array $systemRequirementsInfo
+ *
+ * @return ModuleStateMapGenerator
+ */
+ private function getModuleStateMapGenerator($systemRequirementsInfo)
+ {
+ $setup = $this->getSetupInstance();
+ $language = $this->getLanguageInstance();
+
+ $moduleStateMapGenerator = new Controller\ModuleStateMapGenerator($systemRequirementsInfo);
+
+ $moduleStateMapGenerator->setModuleStateHtmlClassConvertFunction(function ($moduleState) use ($setup) {
+ return $setup->getModuleClass($moduleState);
+ });
+ $moduleStateMapGenerator->setModuleNameTranslateFunction(function ($moduleId) use ($language) {
+ return $language->getModuleName($moduleId);
+ });
+ $moduleStateMapGenerator->setModuleGroupNameTranslateFunction(function ($moduleGroupId) use ($language) {
+ return $language->getModuleName($moduleGroupId);
+ });
+
+ return $moduleStateMapGenerator;
+ }
+
+ /**
+ * Get updated array in the same format as provided by `SystemRequirements::getSystemInfo`.
*
- * @return array The system information, enriched with the "don't know now" MySQL version.
+ * @return array Updated SystemRequirementsInfo array.
*/
- protected function getSystemInformation()
+ private function getSystemRequirementsInfo()
{
$systemRequirements = getSystemReqCheck();
- $information = $systemRequirements->getSystemInfo();
- $information['server_config']['mysql_version'] = -1;
+ return $this->updateSystemRequirementsInfo(
+ $systemRequirements->getSystemInfo()
+ );
+ }
- return $information;
+ /**
+ * Modify given array of format `SystemRequirements::getSystemInfo` with exceptional cases.
+ *
+ * ATM it is a bit tricky to include these changes due to the way SystemRequirements are constructed.
+ *
+ * @param array $systemRequirementsInfo An array taken from `SystemRequirements::getSystemInfo`.
+ *
+ * @return array An array in the same format as provided in `SystemRequirements::getSystemInfo`.
+ */
+ private function updateSystemRequirementsInfo($systemRequirementsInfo)
+ {
+ return SystemRequirements::filter(
+ $systemRequirementsInfo,
+ function ($groupId, $moduleId, $moduleState) {
+ // HtAccess check exception case
+ if (($groupId === SystemRequirements::MODULE_GROUP_ID_SERVER_CONFIG)
+ && ($moduleId === SystemRequirements::MODULE_ID_MOD_REWRITE)
+ && (!$this->canUpdateHtaccess())
+ ) {
+ return SystemRequirements::MODULE_STATUS_BLOCKS_SETUP;
+ }
+
+ // MySql version detect exception case
+ // More information can be obtained from commits with tag 'ESDEV-3999'
+ if (($groupId === SystemRequirements::MODULE_GROUP_ID_SERVER_CONFIG)
+ && ($moduleId === SystemRequirements::MODULE_ID_MYSQL_VERSION)
+ ) {
+ return SystemRequirements::MODULE_STATUS_UNABLE_TO_DETECT;
+ }
+
+ return $moduleState;
+ }
+ );
+ }
+
+ /**
+ * Check if htaccess file can be updated.
+ *
+ * @return bool Returns true in case htaccess file can be updated.
+ */
+ private function canUpdateHtaccess()
+ {
+ $utilities = $this->getUtilitiesInstance();
+ return $utilities->canHtaccessFileBeUpdated();
}
}
Oops, something went wrong.

0 comments on commit 5a5d2a5

Please sign in to comment.