return array( 'name' => 'My Awesome Web Site', 'lang' => 'ru', 'sourceLang' => 'en', );
class ConfigForm extends CFormModel { /** @var array , */ private $_config = array(); /** * * @param array $config * @param string $scenario */ public function __construct($config = array(), $scenario = '') { parent::__construct($scenario); $this->setConfig($config); } public function setConfig($config) { $this->_config = $config; } public function getConfig() { return $this->_config; } }
array( 'name' => 'My Awesome Site', // , - 'params' => array( 'adminEmail' => 'admin@example.com', 'phoneNumber' => '555-555-555', 'motto' => 'the best of the most awesome', ), );
/** * * * @return array */ public function getAttributes() { $this->attributesRecursive($this->_config, $output); return $output; } /** * * * @return array */ public function attributeNames() { $this->attributesRecursive($this->_config, $output); return array_keys($output); } /** * * * @param array $config * @param array $output * @param string $name */ public function attributesRecursive($config, &$output = array(), $name = '') { foreach ($config as $key => $attribute) { if ($name == '') $paramName = $key; else $paramName = $name . "[{$key}]"; if (is_array($attribute)) $this->attributesRecursive($attribute, $output, $paramName); else $output[$paramName] = $attribute; } }
public function rules() { $rules = array(); $attributes = array_keys($this->_config); $rules[] = array(implode(', ', $attributes), 'safe'); return $rules; }
public function __get($name) { // - . - if (isset($this->_config[$name])) return $this->_config[$name]; else return parent::__get($name); } public function __set($name, $value) { // - if (isset($this->_config[$name])) $this->_config[$name] = $value; else parent::__set($name, $value); }
public function run() { $path = YiiBase::getPathOfAlias('application.config') . '/params.php'; $model = new ConfigForm(require($path)); if (isset($_POST['ConfigForm'])) { $model->setAttributes($_POST['ConfigForm']); if($model->save($path)) { Yii::app()->user->setFlash('success config', ' '); $this->controller->refresh(); } } $this->controller->render('config', compact('model')); }
public function save($path) { $config = $this->generateConfigFile(); // , if(!is_writable($path)) throw new CException("Cannot write to config file!"); file_put_contents($path, $config, FILE_TEXT); return true; } public function generateConfigFile() { $this->generateConfigFileRecursive($this->_config, $output); $output = preg_replace('#,$\n#s', '', $output); // return "<?php\nreturn " . $output . ";\n"; } public function generateConfigFileRecursive($attributes, &$output = "", $depth = 1) { $output .= "array(\n"; foreach ($attributes as $attribute => $value) { if (!is_array($value)) $output .= str_repeat("\t", $depth) . "'" . $this->escape($attribute) . "' => '" . $this->escape($value) . "',\n"; else { $output .= str_repeat("\t", $depth) . "'" . $this->escape($attribute) . "' => "; $this->generateConfigFileRecursive($value, $output, $depth + 1); } } $output .= str_repeat("\t", $depth - 1) . "),\n"; // , } private function escape($value) { /** * , (php-injection). * , php - , * , */ return str_replace("'", "\'", $value); }
<?php $form = $this->beginWidget('CActiveForm', array( 'id' => 'config-form', 'enableAjaxValidation' => false, // Ajax- Client- , .. 'enableClientValidation' => false, )); foreach ($model->attributeNames() as $attribute) { echo CHtml::openTag('div', array('class' => 'row')); { echo $form->labelEx($model, $attribute); echo $form->textField($model, $attribute); } echo CHtml::closeTag('div'); } echo CHtml::submitButton(''); $this->endWidget();
public function attributeLabels() { return array( 'name' => ' ', 'params[adminEmail]' => 'Email ', 'params[phoneNumber]' => ' ', 'params[motto]' => ' ', ); }
class ConfigForm extends CFormModel { private $_config = array(); /** * * @param array $config * @param string $scenario */ public function __construct($config = array(), $scenario = '') { parent::__construct($scenario); $this->setConfig($config); } public function setConfig($config) { $this->_config = $config; } public function getConfig() { return $this->_config; } public function __get($name) { if (isset($this->_config[$name])) return $this->_config[$name]; else return parent::__get($name); } public function __set($name, $value) { if (isset($this->_config[$name])) $this->_config[$name] = $value; else parent::__set($name, $value); } public function save($path) { $config = $this->generateConfigFile(); if(!is_writable($path)) throw new CException("Cannot write to config file!"); file_put_contents($path, $config, FILE_TEXT); return true; } public function generateConfigFile() { $this->generateConfigFileRecursive($this->_config, $output); $output = preg_replace('#,$\n#s', '', $output); return "<?php\nreturn " . $output . ";\n"; } public function generateConfigFileRecursive($attributes, &$output = "", $depth = 1) { $output .= "array(\n"; foreach ($attributes as $attribute => $value) { if (!is_array($value)) $output .= str_repeat("\t", $depth) . "'" . $this->escape($attribute) . "' => '" . $this->escape($value) . "',\n"; else { $output .= str_repeat("\t", $depth) . "'" . $this->escape($attribute) . "' => "; $this->generateConfigFileRecursive($value, $output, $depth + 1); } } $output .= str_repeat("\t", $depth - 1) . "),\n"; } private function escape($value) { return str_replace("'", "\'", $value); } /** * * * @return array */ public function getAttributes() { $this->attributesRecursive($this->_config, $output); return $output; } /** * * * @return array */ public function attributeNames() { $this->attributesRecursive($this->_config, $output); return array_keys($output); } /** * * * @param array $config * @param array $output * @param string $name */ public function attributesRecursive($config, &$output = array(), $name = '') { foreach ($config as $key => $attribute) { if ($name == '') $paramName = $key; else $paramName = $name . "[{$key}]"; if (is_array($attribute)) $this->attributesRecursive($attribute, $output, $paramName); else $output[$paramName] = $attribute; } } public function attributeLabels() { return array( 'name' => ' ', 'params[adminEmail]' => 'Email ', 'params[phoneNumber]' => ' ', 'params[motto]' => ' ', ); } public function rules() { $rules = array(); $attributes = array_keys($this->_config); $rules[] = array(implode(', ', $attributes), 'safe'); return $rules; } }
Source: https://habr.com/ru/post/173153/