Skip to content

Commit

Permalink
IN: Added usage of the Step & StepList classes in the installer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémie Tabet committed Jun 30, 2016
1 parent c21765f commit 71717b8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 117 deletions.
23 changes: 21 additions & 2 deletions install-dev/classes/StepList.php
@@ -1,6 +1,6 @@
<?php

class StepList
class StepList implements IteratorAggregate
{
/**
*
Expand Down Expand Up @@ -63,11 +63,21 @@ public function setOffset($offset)
*/
public function setOffsetFromStepName($stepName)
{
$this->offset = array_search($stepName, $this->stepNames);
$this->offset = (int) array_search($stepName, $this->stepNames);

return $this;
}

/**
*
* @param string $stepName
* @return int
*/
public function getOffsetFromStepName($stepName)
{
return (int) array_search($stepName, $this->stepNames);
}

/**
*
* @return Step[]
Expand Down Expand Up @@ -129,4 +139,13 @@ public function isLastStep()
{
return $this->offset == count($this->steps) -1;
}

/**
*
* @return Traversable
*/
public function getIterator()
{
return new ArrayIterator($this->steps);
}
}
173 changes: 70 additions & 103 deletions install-dev/classes/controllerHttp.php
Expand Up @@ -27,9 +27,9 @@
class InstallControllerHttp
{
/**
* @var array List of installer steps
* @var StepList List of installer steps
*/
protected static $steps = array();
protected static $steps;

/**
* @var string
Expand Down Expand Up @@ -88,6 +88,43 @@ class InstallControllerHttp
*/
protected $__vars = array();

private function initSteps()
{
$stepConfig = [
[
'name' => 'welcome',
'displayName' => $this->translator->trans('Choose your language', array(), 'Install'),
'controllerClass' => 'InstallControllerHttpWelcome'
],
[
'name' => 'license',
'displayName' => $this->translator->trans('License agreements', array(), 'Install'),
'controllerClass' => 'InstallControllerHttpLicense'
],
[
'name' => 'system',
'displayName' => $this->translator->trans('System compatibility', array(), 'Install'),
'controllerClass' => 'InstallControllerHttpSystem'
],
[
'name' => 'configure',
'displayName' => $this->translator->trans('Store information', array(), 'Install'),
'controllerClass' => 'InstallControllerHttpConfigure'
],
[
'name' => 'database',
'displayName' => $this->translator->trans('System configuration', array(), 'Install'),
'controllerClass' => 'InstallControllerHttpDatabase'
],
[
'name' => 'process',
'displayName' => $this->translator->trans('Store installation', array(), 'Install'),
'controllerClass' => 'InstallControllerHttpProcess'
],
];
self::$steps = new StepList($stepConfig);
}

public function __construct()
{
$this->session = InstallSession::getInstance();
Expand All @@ -112,14 +149,9 @@ public function __construct()
}
$this->language->setLanguage($lang);

self::$steps = array(
'welcome' => $this->translator->trans('Choose your language', array(), 'Install'),
'license'=> $this->translator->trans('License agreements', array(), 'Install'),
'system'=> $this->translator->trans('System compatibility', array(), 'Install'),
'configure'=> $this->translator->trans('Store information', array(), 'Install'),
'database'=> $this->translator->trans('System configuration', array(), 'Install'),
'process'=> $this->translator->trans('Store installation', array(), 'Install')
);
if (empty(self::$steps)) {
$this->initSteps();
}

$this->init();
}
Expand All @@ -145,21 +177,8 @@ final public static function execute()
Tools::generateIndex();
}

// Include all controllers
foreach (array_keys(self::$steps) as $step) {
if (!file_exists(_PS_INSTALL_CONTROLLERS_PATH_.'http/'.$step.'.php')) {
throw new PrestashopInstallerException("Controller file 'http/{$step}.php' not found");
}

require_once _PS_INSTALL_CONTROLLERS_PATH_.'http/'.$step.'.php';
$classname = 'InstallControllerHttp'.$step;
$controller = new $classname();
$controller->setCurrentStep($step);
self::$instances[$step] = $controller;
}

if (!$session->last_step || !in_array($session->last_step, array_keys(self::$steps))) {
$session->last_step = array_keys(self::$steps)[0];
if (empty($session->last_step)) {
$session->last_step = self::$steps->current()->getName();
}

// Set timezone
Expand All @@ -184,56 +203,51 @@ final public static function execute()

// Get current step (check first if step is changed, then take it from session)
if (Tools::getValue('step')) {
$current_step = Tools::getValue('step');
$session->step = $current_step;
} else {
$current_step = (isset($session->step)) ? $session->step : array_keys(self::$steps)[0];
self::$steps->setOffsetFromStepName(Tools::getValue('step'));
$session->step = self::$steps->current()->getName();
} elseif (!empty($session->step)) {
self::$steps->setOffsetFromStepName($session->step);
}

if (!in_array($current_step, array_keys(self::$steps))) {
$current_step = array_keys(self::$steps)[0];
}


// Validate all steps until current step. If a step is not valid, use it as current step.
foreach (array_keys(self::$steps) as $check_step) {
foreach (self::$steps as $key => $check_step) {
// Do not validate current step
if ($check_step == $current_step) {

if (self::$steps->current() == $check_step) {
break;
}

if (!self::$instances[$check_step]->validate()) {
$current_step = $check_step;
$session->step = $current_step;
$session->last_step = $current_step;
if (!$check_step->getControllerInstance()->validate()) {
self::$steps->setOffset($key);
$session->step = $session->last_step = self::$steps->current()->getName();
break;
}
}

// Submit form to go to next step
if (Tools::getValue('submitNext')) {

self::$instances[$current_step]->processNextStep();
self::$steps->current()->getControllerInstance()->processNextStep();

// If current step is validated, let's go to next step
if (self::$instances[$current_step]->validate()) {
$current_step = self::$instances[$current_step]->findNextStep();
if (self::$steps->current()->getControllerInstance()->validate()) {
self::$steps->next();
}
$session->step = $current_step;
$session->step = self::$steps->current()->getName();

// Change last step
if (self::getStepOffset($current_step) > self::getStepOffset($session->last_step)) {
$session->last_step = $current_step;
if (self::$steps->getOffset() > self::getStepOffset($session->last_step)) {
$session->last_step = self::$steps->current()->getName();
}
}
// Go to previous step
elseif (Tools::getValue('submitPrevious') && $current_step != array_keys(self::$steps)[0]) {
$current_step = self::$instances[$current_step]->findPreviousStep($current_step);
$session->step = $current_step;
elseif (Tools::getValue('submitPrevious') && 0 !== self::$steps->getOffset()) {
self::$steps->previous();
$session->step = self::$steps->current()->getName();
}

self::$instances[$current_step]->process();
self::$instances[$current_step]->display();
self::$steps->current()->getControllerInstance()->process();
self::$steps->current()->getControllerInstance()->display();
}

public function init()
Expand All @@ -252,14 +266,7 @@ public function process()
*/
public function getSteps()
{
return array(
'welcome' => $this->translator->trans('Choose your language', array(), 'Install'),
'license'=> $this->translator->trans('License agreements', array(), 'Install'),
'system'=> $this->translator->trans('System compatibility', array(), 'Install'),
'configure'=> $this->translator->trans('Store information', array(), 'Install'),
'database'=> $this->translator->trans('System configuration', array(), 'Install'),
'process'=> $this->translator->trans('Store installation', array(), 'Install')
);
return self::$steps;
}

public function getLastStep()
Expand All @@ -275,18 +282,7 @@ public function getLastStep()
*/
public static function getStepOffset($step)
{
static $flip = null;
$steps = array_keys(self::$steps);

if (is_numeric($step)) {
$step = array_search($steps, $steps);
}

if (is_null($flip)) {
$flip = array_flip($steps);
}

return $flip[$step];
return self::$steps->getOffsetFromStepName($step);
}

/**
Expand All @@ -300,43 +296,14 @@ public function redirect($step)
exit;
}

/**
* Find previous step
*
* @param string $step
*/
public function findPreviousStep()
{
$steps = array_keys(self::$steps);
return (isset($steps[$this->getStepOffset($this->step) - 1])) ? $steps[$this->getStepOffset($this->step) - 1] : false;
}

/**
* Find next step
*
* @param string $step
*/
public function findNextStep()
{
$steps = array_keys(self::$steps);

$nextStep = (isset($steps[$this->getStepOffset($this->step) + 1])) ? $steps[$this->getStepOffset($this->step) + 1] : false;

if ($nextStep == 'system' && self::$instances[$nextStep]->validate()) {
$nextStep = self::$instances[$nextStep]->findNextStep();
}

return $nextStep;
}

/**
* Check if current step is first step in list of steps
*
* @return bool
*/
public function isFirstStep()
{
return self::getStepOffset($this->step) == 0;
return self::$steps->isFirstStep();
}

/**
Expand All @@ -346,7 +313,7 @@ public function isFirstStep()
*/
public function isLastStep()
{
return self::getStepOffset($this->step) == (count(self::$steps) - 1);
return self::$steps->isLastStep();
}

/**
Expand All @@ -357,7 +324,7 @@ public function isLastStep()
*/
public function isStepFinished($step)
{
return self::getStepOffset($step) < self::getStepOffset($this->getLastStep());
return self::getStepOffset($step) < self::$steps->getOffset();
}

/**
Expand Down
24 changes: 12 additions & 12 deletions install-dev/theme/views/header.php
Expand Up @@ -19,8 +19,8 @@
<script type="text/javascript" src="../js/jquery/plugins/jquery.chosen.js"></script>
<script type="text/javascript" src="theme/js/install.js"></script>
<script type="text/javascript" src="//www.prestashop.com/js/user-assistance.js"></script>
<?php if (file_exists(_PS_INSTALL_PATH_.'theme/js/'.$this->step.'.js')): ?>
<script type="text/javascript" src="theme/js/<?php echo $this->step ?>.js"></script>
<?php if (file_exists(_PS_INSTALL_PATH_.'theme/js/'.self::$steps->current()->getName().'.js')): ?>
<script type="text/javascript" src="theme/js/<?php echo self::$steps->current()->getName() ?>.js"></script>
<?php endif; ?>
<script type="text/javascript">
var ps_base_uri = '<?php echo addslashes(__PS_BASE_URI__) ?>';
Expand Down Expand Up @@ -57,15 +57,15 @@
<!-- List of steps -->
<div id="leftpannel">
<ol id="tabs">
<?php foreach ($this->getSteps() as $step => $translation): ?>
<?php if ($this->step == $step): ?>
<li class="selected"><?php echo $translation; ?></li>
<?php elseif ($this->isStepFinished($step)): ?>
<li class="finished"><a href="index.php?step=<?php echo $step ?>"><?php echo $translation; ?></a></li>
<?php elseif ($step == $this->getLastStep()): ?>
<li class="configuring"><a href="index.php?step=<?php echo $step ?>"><?php echo $translation; ?></a></li>
<?php foreach ($this->getSteps() as $step): ?>
<?php if ($this->step == $step->getName()): ?>
<li class="selected"><?php echo $step; ?></li>
<?php elseif ($this->isStepFinished($step->getName())): ?>
<li class="finished"><a href="index.php?step=<?php echo $step->getName() ?>"><?php echo $step; ?></a></li>
<?php elseif ($step->getName() == $this->getLastStep()): ?>
<li class="configuring"><a href="index.php?step=<?php echo $step->getName() ?>"><?php echo $step; ?></a></li>
<?php else: ?>
<li><?php echo $translation; ?></li>
<li><?php echo $step; ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
Expand All @@ -84,8 +84,8 @@
<div class="contentTitle">
<h1><?php echo $this->translator->trans('Installation Assistant', array(), 'Install'); ?></h1>
<ul id="stepList_1" class="stepList clearfix">
<?php foreach ($this->getSteps() as $step => $translation): ?>
<li <?php if ($this->isStepFinished($step)): ?>class="ok"<?php endif; ?>><?php echo $this->getSteps()[$step]; ?></li>
<?php foreach ($this->getSteps() as $step): ?>
<li <?php if ($this->isStepFinished($step->getName())): ?>class="ok"<?php endif; ?>><?php echo $step ?></li>
<?php endforeach; ?>
</ul>
</div>
Expand Down

0 comments on commit 71717b8

Please sign in to comment.