Skip to content

Instantly share code, notes, and snippets.

@ineersa
Created September 2, 2013 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ineersa/6410546 to your computer and use it in GitHub Desktop.
Save ineersa/6410546 to your computer and use it in GitHub Desktop.
<?php
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 CMap::mergeArray(require(dirname(__FILE__).'/main.php'), " . $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($names=array())
{
$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 = array(), &$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' => 'Site name',
'params[adminEmail]' => 'Admin email',
'params[phoneNumber]' => 'Phone number',
'params[motto]' => 'Site description',
);
}
public function rules()
{
$rules = array();
$attributes = array_keys($this->_config);
$rules[] = array(implode(', ', $attributes), 'safe');
return $rules;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment