Skip to content

Commit

Permalink
Introduce developer page
Browse files Browse the repository at this point in the history
Implemented option to execute post-update processes
  • Loading branch information
getdatakick committed Apr 14, 2023
1 parent 8a75ec1 commit ebf5459
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
25 changes: 25 additions & 0 deletions classes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Settings
const SETTINGS_CACHE_SYSTEM = 'CORE_UPDATER_CACHE_SYSTEM';
const SETTINGS_VERIFY_SSL = 'CORE_UPDATER_VERIFY_SSL';
const SETTINGS_TARGET_PHP_VERSION = 'CORE_UPDATER_TARGET_PHP_VERSION';
const SETTINGS_DEVELOPER_MODE = 'CORE_UPDATER_DEVELOPER_MODE';

// values
const API_SERVER = 'https://api.thirtybees.com';
Expand Down Expand Up @@ -376,4 +377,28 @@ public static function getTargetPHP()
return $value;
}

/**
* Sets developer mode flag
*
* @param bool $developerMode
* @return boolean
* @throws PrestaShopException
*/
public static function setDeveloperMode($developerMode)
{
Configuration::updateGlobalValue(static::SETTINGS_DEVELOPER_MODE, $developerMode ? 1 : 0);
return !!$developerMode;
}

/**
* Returns true, if developer mode was enabled
*
* @return bool
* @throws PrestaShopException
*/
public static function isDeveloperMode()
{
return (bool)Configuration::getGlobalValue(static::SETTINGS_DEVELOPER_MODE);
}

}
118 changes: 118 additions & 0 deletions controllers/admin/AdminCoreUpdaterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AdminCoreUpdaterController extends ModuleAdminController
const TAB_UPDATE = 'update';
const TAB_SETTINGS = 'settings';
const TAB_DB = 'database';
const TAB_DEVELOPER = 'developer';

const ACTION_SAVE_SETTINGS = 'SAVE_SETTINGS';
const ACTION_CLEAR_CACHE = 'CLEAR_CACHE';
Expand All @@ -47,6 +48,10 @@ class AdminCoreUpdaterController extends ModuleAdminController
const ACTION_UPDATE_PROCESS = "UPDATE";
const ACTION_GET_DATABASE_DIFFERENCES = "GET_DATABASE_DIFFERENCES";
const ACTION_APPLY_DATABASE_FIX = 'APPLY_DATABASE_FIX';
const ACTION_RUN_POST_UPDATE_PROCESSES = 'RUN_POST_UPDATE_PROCESSES';

const SELECTED_PROCESS_MIGRATE_DB = 'SELECTED_PROCESS_MIGRATE_DB';
const SELECTED_PROCESS_INITIALIZATION_CALLBACK = 'SELECTED_PROCESS_INITIALIZATION_CALLBACK';

/**
* @var Factory
Expand Down Expand Up @@ -115,6 +120,49 @@ private function initUpdateTab()
}
}

/**
* @return void
* @throws PrestaShopException
*/
private function initDeveloperTab()
{
if (Settings::isDeveloperMode()) {
$this->fields_options = [
'runPostUpdateProcesses' => [
'title' => $this->l('Execute Post Update processes'),
'icon' => 'icon-terminal',
'description' => (
$this->l('You can manually execute processes that are run after each update.') . '<br>' .
$this->l('This can be useful for testing changes in core database schema or code initialization.')
),
'submit' => [
'title' => $this->l('Execute processes'),
'imgclass' => 'save',
'name' => static::ACTION_RUN_POST_UPDATE_PROCESSES,
],
'fields' => [
static::SELECTED_PROCESS_MIGRATE_DB => [
'type' => 'bool',
'title' => $this->l('Migrate database'),
'desc' => $this->l('Fixes some critical databas schema differencies. For example adds missing tables'),
'defaultValue' => true,
'no_multishop_checkbox' => true,
],
static::SELECTED_PROCESS_INITIALIZATION_CALLBACK => [
'type' => 'bool',
'title' => $this->l('Initialize codebase'),
'desc' => $this->l('Executes initialization method on all classes that implementes InitializationCallback interface'),
'defaultValue' => true,
'no_multishop_checkbox' => true,
],
],
],
];
} else {
$this->errors[] = $this->l('Developer mode is not enabled');
}
}

/**
* @param string $updateMode
* @param array $version
Expand Down Expand Up @@ -422,6 +470,12 @@ private function initSettingsTab()
'desc' => $this->l('Secret token for communication with thirtybees api. Optional'),
'no_multishop_checkbox' => true,
],
Settings::SETTINGS_DEVELOPER_MODE => [
'type' => 'bool',
'title' => $this->l('Developer mode'),
'desc' => $this->l('This will unlock features that useful for core developers'),
'no_multishop_checkbox' => true,
],
]
],
];
Expand Down Expand Up @@ -508,6 +562,9 @@ public function performInitContent()
case static::TAB_DB:
$this->initDatabaseTab();
break;
case static::TAB_DEVELOPER:
$this->initDeveloperTab();
break;
}
}
} catch (Exception $e) {
Expand Down Expand Up @@ -535,17 +592,23 @@ public function initToolbar()
{
switch ($this->getActiveTab()) {
case static::TAB_UPDATE:
$this->addDeveloperButton();
$this->addDatabaseButton();
$this->addSettingsButton();
break;
case static::TAB_DB:
$this->addDeveloperButton();
$this->addUpdateButton();
$this->addSettingsButton();
break;
case static::TAB_SETTINGS:
$this->addDeveloperButton();
$this->addDatabaseButton();
$this->addUpdateButton();
break;
case static::TAB_DEVELOPER:
$this->addDatabaseButton();
$this->addSettingsButton();
}
parent::initToolbar();
}
Expand Down Expand Up @@ -586,6 +649,20 @@ private function addUpdateButton()
];
}

/**
* @throws PrestaShopException
*/
private function addDeveloperButton()
{
if (Settings::isDeveloperMode()) {
$this->page_header_toolbar_btn['developer'] = [
'icon' => 'process-icon-terminal',
'href' => static::tabLink(static::TAB_DEVELOPER),
'desc' => $this->l('Developer'),
];
}
}

/**
* Set media.
*
Expand Down Expand Up @@ -627,6 +704,7 @@ public function performPostProcess()
Settings::setSyncThemes(!!Tools::getValue(Settings::SETTINGS_SYNC_THEMES));
Settings::setServerPerformance(Tools::getValue(Settings::SETTINGS_SERVER_PERFORMANCE));
Settings::setApiToken(Tools::getValue(Settings::SETTINGS_API_TOKEN));
Settings::setDeveloperMode(Tools::getValue(Settings::SETTINGS_DEVELOPER_MODE));
Settings::setCacheSystem(Tools::getValue(Settings::SETTINGS_CACHE_SYSTEM));
Settings::setVerifySsl(Tools::getValue(Settings::SETTINGS_VERIFY_SSL));
Settings::setTargetPHP(Tools::getValue(Settings::SETTINGS_TARGET_PHP_VERSION));
Expand All @@ -642,6 +720,12 @@ public function performPostProcess()
$this->setRedirectAfter(static::tabLink(static::TAB_SETTINGS));
$this->redirect();
}

if (Tools::isSubmit(static::ACTION_RUN_POST_UPDATE_PROCESSES)) {
$this->runPostUpdateProcesses();
$this->setRedirectAfter(static::tabLink(static::TAB_DEVELOPER));
$this->redirect();
}
}

/**
Expand Down Expand Up @@ -1094,4 +1178,38 @@ protected function getSupportedPHPVersions($api)
return [];
}
}

/**
* @return void
*
* @throws PrestaShopException
*/
protected function runPostUpdateProcesses()
{
$migrateDb = (bool)Tools::getValue(static::SELECTED_PROCESS_MIGRATE_DB);
$initializeCodeBase = (bool)Tools::getValue(static::SELECTED_PROCESS_INITIALIZATION_CALLBACK);

if ($migrateDb || $initializeCodeBase) {
$updater = $this->factory->getUpdater();
if ($migrateDb) {
try {
$updater->migrateDb();
$this->confirmations[] = '<p>' . $this->l('Database has been migrated') . '</p>';
} catch (Exception $e) {
$this->errors[] = $this->l('Failed to migrate database') . "<pre>$e</pre>";
}
}

if ($initializeCodeBase) {
try {
$updater->initializeCodebase();
$this->confirmations[] = '<p>' . $this->l('Codebase has been initialized') . '</p>';
} catch (Exception $e) {
$this->errors[] = $this->l('Failed to initialize codebase') . "<pre>$e</pre>";
}
}
} else {
$this->warnings[] = $this->l('No operation performed');
}
}
}

0 comments on commit ebf5459

Please sign in to comment.