params);
$this->params = HooksMng::getInstance()->applyFilters('installer_get_init_params', $this->params);
$this->paramsHtmlInfo[] = '***** INIT PARAMS WITH STD VALUlES';
return true;
}
/**
* get value of param key.
* thorw execption if key don't exists
*
* @param string $key param key
*
* @return mixed
*
* @throws \Exception
*/
public function getValue($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
return $this->params[$key]->getValue();
}
/**
* Get param status
*
* @param string $key param key
*
* @return string // STATUS_INIT | STATUS_OVERWRITE | STATUS_UPD_FROM_INPUT
*
* @throws \Exception
*/
public function getInitStatus($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'does not exists');
}
return $this->params[$key]->getStatus();
}
/**
* get the label of param key.
* thorw execption if key don't exists
*
* @param string $key param key
*
* @return string
*
* @throws \Exception
*/
public function getLabel($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
return rtrim($this->params[$key]->getLabel(), ": \n\t");
}
/**
* Set param value
*
* @param string $key param key
* @param mixed $value value
*
* @return boolean // return false if params isn't valid
*
* @throws \Exception // if key don't exists
*/
public function setValue($key, $value)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
return $this->params[$key]->setValue($value);
}
/**
* this cungion set value get from input method.
*
* @param string $key param key
* @param string $method input method (GET, POST ... )
* @param boolean $thowException if true throw exception if value isn't valid.
* @param boolean $nextStepErrorMessage if true and param isn't valid add next step message
*
* @return boolean
*/
public function setValueFromInput($key, $method = ParamForm::INPUT_POST, $thowException = true, $nextStepErrorMessage = false)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (($result = $this->params[$key]->setValueFromInput($method)) === false) {
$this->paramsHtmlInfo[] = 'INVALID VALUE INPUT ' . $key . '';
if ($nextStepErrorMessage) {
$this->addParamValidationFaliedNotice($key);
}
if ($thowException) {
$errorMessage = 'Parameter "' . $this->getLabel($key) . '" have invalid value';
throw new \Exception('PARAM ERROR: ' . $errorMessage);
}
} else {
$this->paramsHtmlInfo[] = 'SET FROM INPUT ' . $key . ' VALUE: ' . Log::v2str($this->params[$key]->getValue());
}
return $result;
}
/**
* Add next step validation failed notice
*
* @param string $key param key
*
* @return void
*/
public function addParamValidationFaliedNotice($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
$longMessage = '' . $this->getLabel($key) . ' ' . $this->params[$key]->getInvalidMessage() . "
\n";
\DUPX_NOTICE_MANAGER::getInstance()->addNextStepNotice(array(
'shortMsg' => 'Parameter validation failed',
'level' => \DUPX_NOTICE_ITEM::CRITICAL,
'longMsg' => $longMessage,
'longMsgMode' => \DUPX_NOTICE_ITEM::MSG_MODE_HTML
), \DUPX_NOTICE_MANAGER::ADD_UNIQUE_APPEND, 'params_validation_fail');
}
/**
* Return the form param wrapper id
*
* @param string $key param key
*
* @return boolean|string return false if the item key isn't a instance of ParamForm
*/
public function getFormWrapperId($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'getFormWrapperId')) {
return $this->params[$key]->getFormWrapperId();
} else {
return false;
}
}
/**
* Return form item id
*
* @param string $key param key
*
* @return boolean|string return false if the item key isn't a instance of ParamForm
*/
public function getFormItemId($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'getFormItemId')) {
return $this->params[$key]->getFormItemId();
} else {
return false;
}
}
/**
* Get param form status
*
* @param string $key param key
*
* @return boolean|string return false if the item key isn't a instance of ParamForm
*/
public function getFormStatus($key)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'getFormStatus')) {
return $this->params[$key]->getFormStatus();
} else {
return false;
}
}
/**
* Set form param status
*
* @param string $key param key
* @param string|callable $status STATUS_ENABLED , STATUS_READONLY or callable function
*
* @return boolean|string return false if the item key isn't a instance of ParamForm
*/
public function setFormStatus($key, $status)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'setFormAttr')) {
return $this->params[$key]->setFormAttr('status', $status);
} else {
return false;
}
}
/**
* Set form wrapper class
*
* @param string $key param key
* @param string $class wrapper class
*
* @return boolean|string return false if the item key isn't a instance of ParamForm
*/
public function addFormWrapperClass($key, $class)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'addWrapperClass')) {
return $this->params[$key]->addWrapperClass($class);
} else {
return false;
}
}
/**
* Remove form wrapper class
*
* @param string $key param key
* @param string $class wrapper class
*
* @return boolean|string return false if the item key isn't a instance of ParamForm
*/
public function removeFormWrapperClass($key, $class)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'removeWrapperClass')) {
return $this->params[$key]->removeWrapperClass($class);
} else {
return false;
}
}
/**
* This tunction add o remove note on the param form
*
* @param string $key param key
* @param string $htmlString true if is html
*
* @return boolean
*/
public function setFormNote($key, $htmlString)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (method_exists($this->params[$key], 'setFormAttr')) {
$this->params[$key]->setFormAttr('subNote', $htmlString);
return true;
} else {
return false;
}
}
/**
* return true if the input exists in html form
* false if isn't ParamForm object or status is STATUS_INFO_ONLY or STATUS_SKIP
*
* @param string $key param key
*
* @return boolean
*/
public function isHtmlInput($key)
{
$status = $this->getFormStatus($key);
switch ($status) {
case ParamForm::STATUS_ENABLED:
case ParamForm::STATUS_READONLY:
case ParamForm::STATUS_DISABLED:
return true;
case ParamForm::STATUS_INFO_ONLY:
case ParamForm::STATUS_SKIP:
default:
return false;
}
}
/**
* get the input form html
*
* @param string $key the param identifier
* @param mixed $overwriteValue if not null set overwriteValue begore get html
* (IMPORTANT: the stored param value don't change. To change it use setValue.)
* @param bool $echo true echo else return string
*
* @return bool|string return false if the item kay isn't a instance of ParamForm
*/
public function getHtmlFormParam($key, $overwriteValue = null, $echo = true)
{
if (!isset($this->params[$key])) {
throw new \Exception('Param key ' . Log::v2str($key) . 'don\' exists');
}
if (!($this->params[$key] instanceof ParamForm)) {
return false;
}
if (is_null($overwriteValue)) {
return $this->params[$key]->getHtml($echo);
} else {
$tmpParam = clone $this->params[$key];
if ($tmpParam->setValue($overwriteValue) === false) {
throw new \Exception('Can\'t set overwriteValue ' . Log::v2str($overwriteValue) . ' in param:' . $tmpParam->getName());
}
return $tmpParam->getHtml($echo);
}
}
/**
* Load params from persistance files
*
* @param boolean $reset if true reset params
*
* @return boolean
*/
public function load($reset = false)
{
if ($reset) {
$this->resetParams();
$this->initParamsOverwrite();
return true;
} else {
if (!file_exists(self::getPersistanceFilePath())) {
return false;
}
$this->paramsHtmlInfo[] = '***** LOAD PARAMS FROM PERSISTENCE FILE';
if (($json = file_get_contents(self::getPersistanceFilePath())) === false) {
throw new \Exception('Can\'t read param persistence file ' . Log::v2str(self::getPersistanceFilePath()));
}
$arrayData = json_decode($json, true);
if ($this->setParamsValues($arrayData) === false) {
throw new \Exception('Can\'t set params from persistence file ' . Log::v2str(self::getPersistanceFilePath()));
}
return true;
}
}
/**
* Remove persistance file and all params and reinit all
*
* @return boolean
*/
protected function resetParams()
{
$this->paramsHtmlInfo[] = '***** RESET PARAMS';
SnapIO::rm(self::getPersistanceFilePath());
$this->params = array();
self::$initialized = false;
return $this->initParams();
}
/**
* Ovrewrite params from sources
*
* @return boolean
*/
public function initParamsOverwrite()
{
Log::info('OVERWRITE PARAMS');
$this->paramsHtmlInfo[] = '***** LOAD OVERWRITE INFO';
/**
* @todo temp disabled require major study
* if (isset($_ENV[self::ENV_PARAMS_KEY])) {
* $this->paramsHtmlInfo[] = 'LOAD FROM ENV VARS';
$arrayData = json_decode($_ENV[self::ENV_PARAMS_KEY]);
$this->setParamsValues($arrayData, true);
} */
// LOAD PARAMS FROM PACKAGE OVERWRITE
$arrayData = (array) \DUPX_ArchiveConfig::getInstance()->overwriteInstallerParams;
if (!empty($arrayData)) {
$this->paramsHtmlInfo[] = '***** LOAD FROM PACKAGE OVERWRITE';
Log::info(' *** FROM PACKAGE');
if ($this->setParamsValues($arrayData, true, Log::LV_DEFAULT) === false) {
throw new \Exception('Can\'t set params from package overwrite ');
}
}
// LOAD PARAMS FROM LOCAL OVERWRITE
$localOverwritePath = DUPX_ROOT . '/' . self::LOCAL_OVERWRITE_PARAMS . self::LOCAL_OVERWRITE_PARAMS_EXTENSION;
if (is_readable($localOverwritePath)) {
// json file is set in $localOverwritePath php file
$json = null;
include($localOverwritePath);
if (empty($json)) {
Log::info('LOCAL OVERWRITE PARAMS FILE ISN\'T WELL FORMED');
} else {
$arrayData = json_decode($json, true);
if (!empty($arrayData)) {
$this->paramsHtmlInfo[] = '***** LOAD FROM LOCAL OVERWRITE';
Log::info(' *** FROM LOCAL FILE');
if ($this->setParamsValues($arrayData, true, Log::LV_DEFAULT) === false) {
throw new \Exception('Can\'t set params from local overwrite ');
}
}
}
}
// LOAD PARAMS FROM LOCAL OVERWRITE PACKAGE_HASH
$localOverwritePath = DUPX_ROOT . '/' . self::LOCAL_OVERWRITE_PARAMS . '_' . Bootstrap::getPackageHash() . '.json';
if (is_readable($localOverwritePath)) {
if (($json = file_get_contents($localOverwritePath)) === false) {
Log::info('CAN\'T READ LOCAL OVERWRITE PARAM HASH FILE');
} else {
$arrayData = json_decode($json, true);
if (!empty($arrayData)) {
$this->paramsHtmlInfo[] = '***** LOAD FROM LOCAL OVERWRITE HASH';
Log::info(' *** FROM LOCAL FILE');
if ($this->setParamsValues($arrayData, true, Log::LV_DEFAULT) === false) {
throw new \Exception('Can\'t set params from local overwrite ');
}
}
}
}
HooksMng::getInstance()->doAction('after_params_overwrite', $this->params);
Log::info("********************************************************************************");
return true;
}
/**
* Update params values from arrayData
*
* @param array $arrayData params data
* @param boolean $overwrite if true overwrite status
* @param integer $logginLevelSet log level
*
* @return bool returns false if a parameter has not been set
*/
protected function setParamsValues($arrayData, $overwrite = false, $logginLevelSet = Log::LV_DEBUG)
{
if (!is_array($arrayData)) {
throw new \Exception('Invalid data params ');
}
$result = true;
foreach ($arrayData as $key => $arrayValues) {
if (isset($this->params[$key])) {
$arrayValues = (array) $arrayValues;
$arrayValValToStr = array_map(array('Duplicator\\Installer\\Utils\\Log\\Log', 'v2str'), $arrayValues);
$this->paramsHtmlInfo[] = 'SET PARAM ' . $key . ' ARRAY DATA: ' .
SnapString::implodeKeyVals(', ', $arrayValValToStr, '[%s = %s]');
if ($this->params[$key]->fromArrayData($arrayValues) === false) {
Log::info('PARAM ISSUE SET KEY[' . $key . '] VALUE: ' . SnapString::implodeKeyVals(', ', $arrayValValToStr, '[%s = %s]'));
Log::info(Log::traceToString(debug_backtrace()));
// $result = false;
} else {
$log = 'PARAM SET KEY[' . $key . ']';
$log .= (Log::isLevel(Log::LV_DEBUG) ? (' VALUE: ' . SnapString::implodeKeyVals(', ', $arrayValValToStr, '[%s = %s]')) : '');
Log::info($log, $logginLevelSet);
if ($overwrite) {
$this->params[$key]->setOveriteStatus();
}
}
}
}
return $result;
}
/**
* update persistance file
*
* @return bool\int // This function returns the number of bytes that were written to the file, or FALSE on failure.
*/
public function save()
{
Log::info("SAVE PARAMS\n" . Log::traceToString(debug_backtrace()), Log::LV_DEBUG);
$arrayData = array();
foreach ($this->params as $param) {
if ($param->isPersistent()) {
$arrayData[$param->getName()] = $param->toArrayData();
}
}
$json = SnapJson::jsonEncodePPrint($arrayData);
if (($result = file_put_contents(self::getPersistanceFilePath(), $json, LOCK_EX)) === false) {
Log::info('PRAMS: can\'t save persistence file');
}
return $result;
}
/**
*
* @staticvar string $path
* @return string
*/
protected static function getPersistanceFilePath()
{
static $path = null;
if (is_null($path)) {
$path = DUPX_INIT . '/' . 'dup-params__' . \DUPX_Package::getPackageHash() . '.json';
}
return $path;
}
/**
* html params info for debug params
*
* @return void
*/
public function getParamsHtmlInfo()
{
if (!$this->getValue(self::PARAM_DEBUG_PARAMS)) {
return;
}
?>