Permalink
Browse files

ESDEV-4156 Use exceptions as returns of controller

By using an exception inside controller instead of return statement
this enables us to extract methods from actual controller (to make
controllers smaller than they are now) and to still have a way to
exit early from the controller to display some information or an
error message.
  • Loading branch information...
1 parent 5a5d2a5 commit 11b4d5150fd040350daffef5cef1b07911014d47 @rezonanc-oxid rezonanc-oxid committed Dec 20, 2016
@@ -26,6 +26,7 @@
use OxidEsales\Eshop\Core\Edition\EditionSelector;
use OxidEsales\Eshop\Core\SystemRequirements;
use OxidEsales\EshopCommunity\Setup\Controller\ModuleStateMapGenerator;
+use OxidEsales\EshopCommunity\Setup\Exception\SetupControllerExitException;
/**
* Class holds scripts (controllers) needed to perform shop setup steps
@@ -52,8 +53,6 @@ public function __construct()
* `Acceptance/Frontend/ShopSetUpTest.php::testSystemRequirementsPageShowsTranslatedModuleGroupNames`
* `Acceptance/Frontend/ShopSetUpTest.php::testSystemRequirementsContainsProperModuleStateHtmlClassNames`
* `Acceptance/Frontend/ShopSetUpTest.php::testInstallShopCantContinueDueToHtaccessProblem`
- *
- * @return string
*/
public function systemReq()
{
@@ -64,6 +63,7 @@ public function systemReq()
$isSafeForSetupToContinue = SystemRequirements::canSetupContinue($systemRequirementsInfo);
$this->setViewOptions(
+ 'systemreq.php',
'STEP_0_TITLE',
[
"blContinue" => $isSafeForSetupToContinue,
@@ -72,14 +72,10 @@ public function systemReq()
"sLanguage" => $this->getSessionInstance()->getSessionParam('setup_lang'),
]
);
-
- return "systemreq.php";
}
/**
* Welcome page
- *
- * @return string
*/
public function welcome()
{
@@ -90,6 +86,7 @@ public function welcome()
$this->getUtilitiesInstance()->setCookie("oxidadminlanguage", $adminLanguage, time() + 31536000, "/");
$this->setViewOptions(
+ 'welcome.php',
'STEP_1_TITLE',
[
"aCountries" => getCountryList(),
@@ -101,34 +98,27 @@ public function welcome()
"sCountryLang" => $session->getSessionParam('country_lang')
]
);
-
- return "welcome.php";
}
/**
* License confirmation page
- *
- * @return string
*/
public function license()
{
$languageId = $this->getLanguageInstance()->getLanguage();
$utils = $this->getUtilitiesInstance();
$this->setViewOptions(
+ 'license.php',
'STEP_2_TITLE',
[
"aLicenseText" => $utils->getLicenseContent($languageId)
]
);
-
- return "license.php";
}
/**
* DB info entry page
- *
- * @return string
*/
public function dbInfo()
{
@@ -143,7 +133,7 @@ public function dbInfo()
$setup->setNextStep($setup->getStep("STEP_WELCOME"));
$view->setMessage($this->getLanguageInstance()->getText("ERROR_SETUP_CANCELLED"));
- return "licenseerror.php";
+ throw new SetupControllerExitException("licenseerror.php");
}
$databaseConfigValues = $session->getSessionParam('aDB');
@@ -157,21 +147,18 @@ public function dbInfo()
}
$this->setViewOptions(
+ 'dbinfo.php',
'STEP_3_TITLE',
[
"aDB" => $databaseConfigValues,
"blMbStringOn" => $systemRequirements->getModuleInfo('mb_string'),
"blUnicodeSupport" => $systemRequirements->getModuleInfo('unicode_support')
]
);
-
- return "dbinfo.php";
}
/**
* Setup paths info entry page
- *
- * @return string
*/
public function dirsInfo()
{
@@ -183,21 +170,18 @@ public function dirsInfo()
}
$this->setViewOptions(
+ 'dirsinfo.php',
'STEP_4_TITLE',
[
"aAdminData" => $session->getSessionParam('aAdminData'),
"aPath" => $this->getUtilitiesInstance()->getDefaultPathParams(),
"aSetupConfig" => ["blDelSetupDir" => $setup->deleteSetupDirectory()],
]
);
-
- return "dirsinfo.php";
}
/**
* Testing database connection
- *
- * @return string
*/
public function dbConnect()
{
@@ -216,7 +200,7 @@ public function dbConnect()
$setup->setNextStep($setup->getStep('STEP_DB_INFO'));
$view->setMessage($language->getText('ERROR_FILL_ALL_FIELDS'));
- return "default.php";
+ throw new SetupControllerExitException();
}
try {
@@ -228,12 +212,12 @@ public function dbConnect()
$setup->setNextStep($setup->getStep('STEP_DB_INFO'));
$view->setMessage($language->getText('ERROR_DB_CONNECT') . " - " . $exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
} elseif ($exception->getCode() === Database::ERROR_MYSQL_VERSION_DOES_NOT_FIT_REQUIREMENTS) {
$setup->setNextStep($setup->getStep('STEP_DB_INFO'));
$view->setMessage($exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
} elseif ($exception->getCode() === Database::ERROR_MYSQL_VERSION_DOES_NOT_FIT_RECOMMENDATIONS) {
$setup->setNextStep(null);
$this->formMessageIfMySqyVersionIsNotRecommended($view, $language);
@@ -243,7 +227,7 @@ public function dbConnect()
}
$this->formMessageInstallAnyway($view, $language, $session->getSid(), $setup->getStep('STEP_DIRS_INFO'));
- return "default.php";
+ throw new SetupControllerExitException();
} else {
try {
// if database is not there, try to create it
@@ -252,7 +236,7 @@ public function dbConnect()
$setup->setNextStep($setup->getStep('STEP_DB_INFO'));
$view->setMessage($exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
}
$view->setViewParam("blCreated", 1);
}
@@ -265,18 +249,16 @@ public function dbConnect()
$this->formMessageIfDBCanBeOverwritten($databaseConfigValues['dbName'], $view, $language);
$this->formMessageInstallAnyway($view, $language, $session->getSid(), $setup->getStep('STEP_DIRS_INFO'));
- return "default.php";
+ throw new SetupControllerExitException();
}
$setup->setNextStep($setup->getStep('STEP_DIRS_INFO'));
- return "dbconnect.php";
+ $this->view->setTemplateFileName("dbconnect.php");
}
/**
* Creating database
- *
- * @return string
*/
public function dbCreate()
{
@@ -302,12 +284,12 @@ public function dbCreate()
}
$this->formMessageInstallAnyway($view, $language, $session->getSid(), $setup->getStep('STEP_DB_CREATE'));
- return "default.php";
+ throw new SetupControllerExitException();
} else {
$setup->setNextStep($setup->getStep('STEP_DB_CREATE'));
$view->setMessage($exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
}
}
@@ -319,15 +301,15 @@ public function dbCreate()
$view->setMessage($exception->getMessage());
$setup->setNextStep($setup->getStep('STEP_DB_INFO'));
- return "default.php";
+ throw new SetupControllerExitException();
}
// check if DB is already UP and running
if (!$this->databaseCanBeOverwritten($database)) {
$this->formMessageIfDBCanBeOverwritten($databaseConfigValues['dbName'], $view, $language);
$this->formMessageInstallAnyway($view, $language, $session->getSid(), $setup->getStep('STEP_DB_CREATE'));
- return "default.php";
+ throw new SetupControllerExitException();
}
try {
@@ -341,14 +323,14 @@ public function dbCreate()
// there where problems with queries
$view->setMessage($language->getText('ERROR_BAD_DEMODATA') . "<br><br>" . $exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
}
$this->getUtilitiesInstance()->regenerateViews();
} catch (Exception $exception) {
$view->setMessage($exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
}
//update dyn pages / shop country config options (from first step)
@@ -361,19 +343,15 @@ public function dbCreate()
} catch (Exception $exception) {
$view->setMessage($exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
}
$view->setMessage($language->getText('STEP_4_2_UPDATING_DATABASE'));
$this->onDirsWriteSetStep($setup);
-
- return "default.php";
}
/**
* Writing config info
- *
- * @return string
*/
public function dirsWrite()
{
@@ -413,31 +391,31 @@ public function dirsWrite()
$setup->setNextStep($setup->getStep('STEP_DIRS_INFO'));
$view->setMessage($language->getText('ERROR_FILL_ALL_FIELDS'));
- return "default.php";
+ throw new SetupControllerExitException();
}
// check if passwords match
if (strlen($adminData['sPassword']) < 6) {
$setup->setNextStep($setup->getStep('STEP_DIRS_INFO'));
$view->setMessage($language->getText('ERROR_PASSWORD_TOO_SHORT'));
- return "default.php";
+ throw new SetupControllerExitException();
}
// check if passwords match
if ($adminData['sPassword'] != $adminData['sPasswordConfirm']) {
$setup->setNextStep($setup->getStep('STEP_DIRS_INFO'));
$view->setMessage($language->getText('ERROR_PASSWORDS_DO_NOT_MATCH'));
- return "default.php";
+ throw new SetupControllerExitException();
}
// check if email matches pattern
if (!$utils->isValidEmail($adminData['sLoginName'])) {
$setup->setNextStep($setup->getStep('STEP_DIRS_INFO'));
$view->setMessage($language->getText('ERROR_USER_NAME_DOES_NOT_MATCH_PATTERN'));
- return "default.php";
+ throw new SetupControllerExitException();
}
// write it now
@@ -456,7 +434,7 @@ public function dirsWrite()
$setup->setNextStep($setup->getStep('STEP_DIRS_INFO'));
$view->setMessage($exception->getMessage());
- return "default.php";
+ throw new SetupControllerExitException();
}
$view->setMessage($language->getText('STEP_4_1_DATA_WAS_WRITTEN'));
@@ -466,30 +444,25 @@ public function dirsWrite()
$databaseConfigValues = $session->getSessionParam('aDB');
$view->setViewParam("aDB", $databaseConfigValues);
$setup->setNextStep($setup->getStep('STEP_DB_CREATE'));
-
- return "default.php";
}
/**
* Final setup step
- *
- * @return string
*/
public function finish()
{
$session = $this->getSessionInstance();
$pathCollection = $session->getSessionParam("aPath");
$this->setViewOptions(
+ 'finish.php',
'STEP_6_TITLE',
[
"aPath" => $pathCollection,
"aSetupConfig" => $session->getSessionParam("aSetupConfig"),
"blWritableConfig" => is_writable($pathCollection['sShopDir'] . "/config.inc.php")
]
);
-
- return "finish.php";
}
/**
@@ -616,12 +589,14 @@ private function installShopData($database, $demodata = 0)
/**
* Allows to set all necessary view information with single method call.
*
- * @param string $titleId Title Id which will be used in the template.
- * @param array $viewOptions An array containing all view elements to be used inside a template.
+ * @param string $templateFileName File name of template which will be used to pass in the context data.
+ * @param string $titleId Title Id which will be used in the template.
+ * @param array $viewOptions An array containing all view elements to be used inside a template.
*/
- protected function setViewOptions($titleId, $viewOptions)
+ protected function setViewOptions($templateFileName, $titleId, $viewOptions)
{
$view = $this->getView();
+ $view->setTemplateFileName($templateFileName);
$view->setTitle($titleId);
foreach ($viewOptions as $optionKey => $optionValue) {
@@ -16,12 +16,14 @@
* 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 \OxidEsales\EshopCommunity\Setup\Exception\SetupControllerExitException;
+
/**
* Chooses and executes controller action which must be executec to render expected view
*/
@@ -41,7 +43,13 @@ public function run()
$view = $oController->getView();
$view->sendHeaders();
- $view->display($oController->$sAction());
+
+ try {
+ $oController->$sAction();
+ } catch (SetupControllerExitException $exception) {
+ } finally {
+ $view->display();
+ }
}
/**
Oops, something went wrong.

0 comments on commit 11b4d51

Please sign in to comment.