Skip to content
Snippets Groups Projects
Commit 78b4f912 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

rollout base class: php8 only; added variable types; short array syntax

parent adb605ca
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
This commit is part of merge request !66. Comments created here will be created in the context of that merge request.
......@@ -54,6 +54,7 @@ class build_base implements iBuildplugin
// ---------------------------------------------------------------
/**
* Constructor
* Initialize build plugin
* @param array $aParams hash with those possible keys
* lang string language, i.e. 'de'
......@@ -154,7 +155,9 @@ class build_base implements iBuildplugin
// ---------------------------------------------------------------
/**
* Check requirements if the plugin could work
* Get an array with shell commands to check requirements if the plugin
* can work
*
* @return array
*/
public function checkRequirements(): array
......
......@@ -7,6 +7,9 @@ require_once __DIR__ . '/../../vendor/axelhahn/ahcache/cache.class.php';
* see deployment/plugins/rollout/*
*
* @author axel
*
* Axel <axel.hahn@unibe.ch>
* 2024-08-29 Axel php8 only; added variable types; short array syntax
*/
class rollout_base implements iRolloutplugin
{
......@@ -19,58 +22,64 @@ class rollout_base implements iRolloutplugin
* settings in the config structore for global and project based config
* @var string
*/
protected $_sPluginId = 'UNSET';
protected string $_sPluginId = 'UNSET';
/**
* data with plugin infos (read from info.json)
* Data with plugin infos (read from info.json)
* @var array
*/
protected $_aPlugininfos = false;
protected array $_aPlugininfos = [];
/**
* array with translation texts
* Array with translation texts
* @var array
*/
protected $_aLang = [];
protected array $_aLang = [];
/**
* set language; 2 letter code, i.e. "de"; default language is "en" ; a
* file "lang_en.json" is required in the plugin dir
* @var string
*/
protected $_sFallbackLang = 'en-en';
protected string $_sFallbackLang = 'en-en';
/**
* set language; 2 letter code, i.e. "de"; default language is "en" ; a
* file "lang_en.json" is required in the plugin dir
* @var string
*/
protected $_sLang = 'en-en';
protected string $_sLang = 'en-en';
/**
* string with phase of project; one of preview|stage|live
* @var string
*/
protected $_sPhase = false;
protected string $_sPhase = '';
/**
* global configuration of the rollout plugin
* @var array
*/
protected $_aCfgGlobal = false;
protected array $_aCfgGlobal = [];
/**
* configuration of the project
* @var array
*/
protected $_aCfgProject = false;
protected array $_aCfgProject = [];
protected $_sNamePrefix4Project = false; // set in constructor
protected $_sNamePrefix4Phase = false; // set in constructor
/*
UNUSED
protected string $_sNamePrefix4Project = ''; // set in constructor
protected string $_sNamePrefix4Phase = ''; // set in constructor
*/
// ---------------------------------------------------------------
// CONSTRUCTOR
// ---------------------------------------------------------------
/**
* Constructor
* initialize rollout plugin
* @param array $aParams hash with those possible keys
* lang string language, i.e. 'de'
......@@ -114,13 +123,13 @@ class rollout_base implements iRolloutplugin
// ---------------------------------------------------------------
/**
* get a string for a prefix for name attribute in form vars.
* Get a string for a prefix for name attribute in form vars.
* It is important to store the value in the wanted structure.
*
* @param string $sPhase
* @return string
*/
protected function _getNamePrefix($sPhase = false)
protected function _getNamePrefix(string $sPhase = ''): string
{
return ($sPhase
? 'phases[' . $sPhase . '][plugins][rollout][' . $this->getId() . ']'
......@@ -129,7 +138,7 @@ class rollout_base implements iRolloutplugin
}
/**
* get Data from a callback function and store it in a cache
* Get Data from a callback function and store it in a cache
* The response type depends on the callback function
*
* @param string $sFunctionname name of the callback function
......@@ -138,11 +147,11 @@ class rollout_base implements iRolloutplugin
* @param integer $iTtlOnError ttl value = how many seconds to use cache if there was no response
* @return mixed
*/
protected function _getCallback($sFunctionname, $sKey, $iTtl = 15, $iTtlOnError = 10)
protected function _getCallback(string $sFunctionname, string $sKey, int $iTtl = 15, int $iTtlOnError = 10): mixed
{
$oCache = new AhCache('rollout-' . $this->getId(), 'callback-' . $sFunctionname . '-' . $sKey);
if ($oCache->isExpired()) {
$aMydata = call_user_func(array($this, $sFunctionname));
$aMydata = call_user_func([$this, $sFunctionname]);
// echo "$sFunctionname fresh ".($aMydata ? "OK": "false")." - storing for $iTtl sec<br>";
$oCache->write($aMydata, ($aMydata ? $iTtl : $iTtlOnError));
} else {
......@@ -152,13 +161,15 @@ class rollout_base implements iRolloutplugin
// echo '<pre>'.print_r($aMydata, 1).'</pre>'; die(__METHOD__);
return $aMydata;
}
/**
* render a form by given form elementes
* Get Html code for a form by given form elementes
*
* @param array $aFormdata array of form elements
* @param string $sKey part of the identifier used in id of the input field
* @return string
*/
protected function _renderForm($aFormdata, $sKey)
protected function _renderForm(array $aFormdata, string $sKey): string
{
static $i;
if (!isset($i)) {
......@@ -177,12 +188,13 @@ class rollout_base implements iRolloutplugin
}
/**
* render form fields for global plugin variables
* Get Html code for form fields for global plugin variables
*
* @param string $sScope scope of vars ... one of global|project|phase
* @param string $sPhase optional: render global vars in a phase; if no phase was set it renders form fields for project based settings
* @return string
*/
protected function _renderForm4Vars($sScope, $sPhase = false)
protected function _renderForm4Vars(string $sScope, string $sPhase = ''): string
{
$sReturn = '';
......@@ -209,8 +221,8 @@ class rollout_base implements iRolloutplugin
// create form fields
// $aFormdata[]=array('type' => 'markup','value' => '<br>'.$this->_t('section-override-'.$sScope.'-vars').':');
$aFormdata[] = array('type' => 'markup', 'value' => '<div style="style: clear: left;"></div><h4>' . $this->getId() . ' :: ' . $sScope . '</h4>');
// $aFormdata[]=['type' => 'markup','value' => '<br>'.$this->_t('section-override-'.$sScope.'-vars').':'];
$aFormdata[] = ['type' => 'markup', 'value' => '<div style="style: clear: left;"></div><h4>' . $this->getId() . ' :: ' . $sScope . '</h4>'];
$sMiss = '';
foreach ($aInfos['vars'][$sScope] as $sVarname => $aVarinfos) {
......@@ -243,7 +255,7 @@ class rollout_base implements iRolloutplugin
$aCallbackData[$aEffectiveConfig[$sVarname]]['checked'] = 'checked';
$aCallbackData[$aEffectiveConfig[$sVarname]]['label'] .= ' &laquo;&laquo;';
} elseif ($aVarinfos['type'] === 'select') {
$aCallbackData = array_merge(array('NO_SELECTED_ITEM_YET' => array('value' => '', 'label' => '...')), $aCallbackData);
$aCallbackData = array_merge(['NO_SELECTED_ITEM_YET' => ['value' => '', 'label' => '...']], $aCallbackData);
}
// wenn value = defaultvalue, dann value auf '' setzen (damit bei Default vom Scope
......@@ -262,7 +274,7 @@ class rollout_base implements iRolloutplugin
? '******************************'
: $sMyPlaceholder
);
$aFormdata[] = array(
$aFormdata[] = [
'type' => $aVarinfos['type'],
'name' => $sPrefixName . '[' . $sVarname . ']',
'label' => $this->_t($sVarname . '-label'),
......@@ -273,12 +285,12 @@ class rollout_base implements iRolloutplugin
// 'size' => 25,
'placeholder' => $sMyPlaceholder,
'autocomplete' => 'off',
);
];
break;
case "select":
case "radio":
$aOptions = $aCallbackData;
$aFormdata[] = array(
$aFormdata[] = [
'type' => $aVarinfos['type'],
'name' => $sPrefixName . '[' . $sVarname . ']',
'label' => $this->_t($sVarname . '-label'),
......@@ -288,10 +300,10 @@ class rollout_base implements iRolloutplugin
'options' => $aOptions,
// 'placeholder' => $sMyPlaceholder
);
];
break;
case "text":
$aFormdata[] = array(
$aFormdata[] = [
'type' => $aVarinfos['type'],
'name' => $sPrefixName . '[' . $sVarname . ']',
'label' => $this->_t($sVarname . '-label'),
......@@ -309,7 +321,7 @@ class rollout_base implements iRolloutplugin
// 'size' => 25,
'placeholder' => $sMyPlaceholder,
'autocomplete' => 'off',
);
];
break;
default:
......@@ -317,7 +329,7 @@ class rollout_base implements iRolloutplugin
break;
}
}
// $aFormdata[]=array('type' => 'markup','value' => '<div style="style: clear: left;"></div><br><br>');
// $aFormdata[]=['type' => 'markup','value' => '<div style="style: clear: left;"></div><br><br>'];
return $this->_renderForm($aFormdata, $sKey)
. ($sMiss
? '<pre>WARNINGS:<br>' . $sMiss . '</pre>' . ($sScope === 'global' ? $this->renderCfgExample() : '')
......@@ -327,14 +339,15 @@ class rollout_base implements iRolloutplugin
}
/**
* get a translated text from lang_XX.json in plugin dir;
* Get a translated text from lang_XX.json in plugin dir;
* If the key is missed it returns "[KEY :: LANG]"
*
* @see setLang()
*
* @param string $sKey key to find in lang file
* @return string
*/
protected function _t($sKey)
protected function _t(string $sKey): string
{
return (isset($this->_aLang[$sKey]) && $this->_aLang[$sKey])
? $this->_aLang[$sKey]
......@@ -352,10 +365,11 @@ class rollout_base implements iRolloutplugin
* translated texts can be done with $this->_t("your_key")
*
* @see _t()
*
* @param string $sLang language code, i.e. "de-de"
* @return boolean
*/
public function setLang($sLang = false)
public function setLang(string $sLang = ''): bool
{
$this->_sLang = $sLang ? $sLang : $this->_sLang;
......@@ -371,8 +385,11 @@ class rollout_base implements iRolloutplugin
/**
* set a phase for automatic use GETTER methods
*
* @param string $sPhase name of the phase; one of preview|stage|live
* @return boolean
*/
public function setPhase($sPhase)
public function setPhase(string $sPhase): bool
{
$this->_sPhase = $sPhase;
return true;
......@@ -384,7 +401,9 @@ class rollout_base implements iRolloutplugin
// ----------------------------------------------------------------------
/**
* check requirements if the plugin could work
* Get an array with shell commands to check requirements if the plugin
* can work
*
* @return array
*/
public function checkRequirements(): array
......@@ -394,34 +413,39 @@ class rollout_base implements iRolloutplugin
}
/**
* check access to a deploy target
* Get an array with shell commands to check access to a deploy target
*
* @return array
*/
public function checkConnectionToTarget()
public function checkConnectionToTarget(): array
{
// do nothing ... always true
return true;
return [];
}
// ----------------------------------------------------------------------
// INTERFACE :: SETTER
// ----------------------------------------------------------------------
/**
* set Config ... by given global config of the current plugin
* @param array $aConfigArray
* Set Config ... by given global config of the current plugin
*
* @param array $aConfigArray complete array of all config data
* @return boolean
*/
public function setGlobalConfig($aConfigArray)
public function setGlobalConfig(array $aConfigArray): bool
{
return $this->_aCfgGlobal = $aConfigArray;
$this->_aCfgGlobal = $aConfigArray;
return true;
}
/**
* set Config ... by given project config
* Set Config ... by given project config
*
* @param array $aProjectConfigArray complete array of project config
* @return array
*/
public function setProjectConfig($aProjectConfigArray)
public function setProjectConfig(array $aProjectConfigArray): array
{
$this->_aCfgProject = $aProjectConfigArray;
// echo '<pre>'.print_r($aProjectConfigArray, 1).'</pre>';
......@@ -431,13 +455,13 @@ class rollout_base implements iRolloutplugin
/*
if (!isset($this->_aCfgProject['plugins']['rollout'])){
if (!isset($this->_aCfgProject['plugins'])){
$this->_aCfgProject['plugins']=array();
$this->_aCfgProject['plugins']=[];
}
$this->_aCfgProject['plugins']['rollout']=array();
$this->_aCfgProject['plugins']['rollout']=[];
}
*
*/
$this->_aCfgProject['plugins']['rollout'][$this->_sPluginId] = array('INFO' => 'created');
$this->_aCfgProject['plugins']['rollout'][$this->_sPluginId] = ['INFO' => 'created'];
}
// unset empty project values to get global values
......@@ -456,7 +480,7 @@ class rollout_base implements iRolloutplugin
}
}
}
// TODO:
return $this->_aCfgProject;
}
......@@ -465,12 +489,13 @@ class rollout_base implements iRolloutplugin
// ----------------------------------------------------------------------
/**
* get configuration for the project .. or more specifi for a given phase
* Get configuration array for the project .. or more specific for a given phase
*
* @param string $sPhase
* @param boolean $bMask Flag for public output; if true then mask your secrets
* @return array
*/
public function getConfig($sPhase = false, $bMask = false)
public function getConfig(string $sPhase = '', bool $bMask = false): array
{
$aReturn = array_merge($this->_aCfgGlobal, $this->_aCfgProject['plugins']['rollout'][$this->getId()]);
......@@ -490,12 +515,13 @@ class rollout_base implements iRolloutplugin
}
/**
* get an array with shell commands to execute
* Get an array with shell commands to execute for deployment of built file
*
* @param string $sPhase
* @param boolean $bMask Flag for public output; if true then mask your secrets
* @return array
*/
public function getDeployCommands($sPhase, $bMask = false)
public function getDeployCommands(string $sPhase, bool $bMask = false): array
{
return [
'echo "ERROR: The method getDeployCommamds($sPhase) was not implemented in the rollout plugin [' . $this->getId() . ']"',
......@@ -504,36 +530,39 @@ class rollout_base implements iRolloutplugin
}
/**
* get string with current ID
* Get string with current plugin ID
*
* @return string
*/
public function getId()
public function getId(): string
{
return $this->_sPluginId;
}
/**
* get string with plugin name (taken from plugin language file)
* Get name of plugin as string ... language specific
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->_t('plugin_name');
}
/**
* get string with plugin description (taken from plugin language file)
* Get description of plugin as string ... language specific
* @return string
*/
public function getDescription()
public function getDescription(): string
{
return $this->_t('description');
}
/**
* get array read from info.json
* Get array of data in info.js
* @return array
*/
public function getPluginInfos()
public function getPluginInfos(): array
{
if ($this->_aPlugininfos) {
......@@ -544,7 +573,7 @@ class rollout_base implements iRolloutplugin
$sFile = dirname($oReflection->getFileName()) . '/info.json';
$this->_aPlugininfos = (file_exists($sFile))
? json_decode(file_get_contents($sFile), 1)
: array('error' => 'unable to read info file [' . $sFile . '].')
: ['error' => 'unable to read info file [' . $sFile . '].']
;
return $this->_aPlugininfos;
}
......@@ -552,21 +581,26 @@ class rollout_base implements iRolloutplugin
// ----------------------------------------------------------------------
// INTERFACE :: RENDERER
// ----------------------------------------------------------------------
/**
* Get HTML code for example configuration
* @return string
*/
public function renderCfgExample()
{
$sReturn = '';
$sPre = ' ';
$aInfos = $this->getPluginInfos();
$sReturn .= '<pre>$aConfig = array(
$sReturn .= '<pre>$aConfig = [
...
\'plugins\'=>array(
\'plugins\'=>[
...
// enabled rollout plugins
\'rollout\'=>array(
\'rollout\'=>[
...
<strong>
\'' . $this->getId() . '\'=>array(
\'' . $this->getId() . '\'=>[
// ' . $this->getName() . '
// ' . $this->getDescription() . '
' . PHP_EOL;
......@@ -583,16 +617,23 @@ class rollout_base implements iRolloutplugin
}
$sReturn .= '
),
],
</strong>
...
),
],
...
),
);</pre>';
],
];</pre>';
return $sReturn;
}
protected function _renderMoreToggler($sContent)
/**
* Get html code for button [...] that shows and hides more information
*
* @param string $sContent
* @return string
*/
protected function _renderMoreToggler(string $sContent)
{
$sDivId = 'rollout-more-toggler-' . $this->getId() . '-' . md5($sContent);
return ''
......@@ -603,7 +644,14 @@ class rollout_base implements iRolloutplugin
;
}
public function renderFormdata4Project()
/**
* Override general form renderer: show
* - formvars for project
*
* @return string
*/
public function renderFormdata4Project(): string
{
return ''
. $this->_renderForm4Vars('project', false)
......@@ -614,7 +662,14 @@ class rollout_base implements iRolloutplugin
// .'<pre>DEBUG: $this->_aCfgProject ... plugin = '.print_r($this->_aCfgProject, 1).'</pre>'
;
}
public function renderFormdata4Phase($sPhase)
/**
* override of form renderer: show configuration for a given phase
* @param string $sPhase phaese; one of preview|stage|live
* @return string
*/
public function renderFormdata4Phase(string $sPhase): string
{
static $iCounter;
if (!isset($iCounter)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment