Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 115 additions & 2 deletions src/Core/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class Setting
* @var array
*/
protected static $_data = [];

/**
* Array of values currently stored in Configure.
*
* @var array
*/
protected static $_values = [];

/**
* Options
Expand Down Expand Up @@ -85,9 +92,32 @@ public static function read($key = null, $type = null)
if ($data->count() > 0) {
$data = $data->first()->toArray();
} else {
return null;

$data = $model->find()
->select(['name', 'value'])
->where(['name LIKE' => $key.'.%']);

if ($data->count() > 0) {
$data = $data->toArray();

foreach($data as $data_set)
{
if(self::_serialized($data_set->value)) {
$data_set->value = unserialize($data_set->value);
}
static::$_values = Hash::insert(static::$_values, $data_set->name, $data_set->value);
}

$data['value'] = static::$_values;
}
else {
return null;
}
}

if(self::_serialized($data['value'])) {
$data['value'] = unserialize($data['value']);
}
self::_store($key, $data['value']);

$value = $data['value'];
Expand Down Expand Up @@ -139,11 +169,15 @@ public static function write($key, $value = null, $options = [])
$options = Hash::merge($_options, $options);

$model = self::model();

if(is_array($value) && !empty($value)) {
$value = serialize($value);
}

if (self::check($key)) {
if ($options['overrule']) {
$data = $model->findByName($key)->first();
if ($data) {
if ($data) {
$data->set('value', $value);
$model->save($data);
} else {
Expand Down Expand Up @@ -234,6 +268,11 @@ public static function register($key, $value, $data = [])
}

self::autoLoad();

if(is_array($value))
{
$value = seralize($value);
}

$_data = [
'value' => $value,
Expand Down Expand Up @@ -348,4 +387,78 @@ protected static function _tableExists()
}
return false;
}

/**
* _serialized
*
* @return bool
*/
protected static function _serialized( $value, &$result = null ) {

if ( ! is_string( $value ) ) {
return false;
}

if ( 'b:0;' === $value ) {
$result = false;
return true;
}
$length = strlen($value);
$end = '';

if ( isset( $value[0] ) ) {
switch ($value[0]) {
case 's':
if ( '"' !== $value[$length - 2] )
return false;

case 'b':
case 'i':
case 'd':

$end .= ';';
case 'a':
case 'O':
$end .= '}';

if ( ':' !== $value[1] )
return false;

switch ( $value[2] ) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
break;

default:
return false;
}
case 'N':
$end .= ';';

if ( $value[$length - 1] !== $end[0] )
return false;
break;

default:
return false;
}
}

if ( ( $result = unserialize($value) ) === false ) {
$result = null;
return false;
}

return true;
}


}
36 changes: 36 additions & 0 deletions tests/TestCase/Core/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public function testRead()
$this->assertEquals(1, Setting::read('App.UniqueReadvalue'));
$this->assertEquals(1, Setting::read('App.UniqueReadvalue', 'integer'));
$this->assertEquals('1', Setting::read('App.UniqueReadvalue', 'string'));

$data = [
'name' => 'App.UniqueArray',
'value' => 'a:4:{i:0;i:1;i:2;i:3;i:3;s:3:"one";s:3:"two";s:5:"three";}'
];

$this->Settings->save($this->Settings->newEntity($data));
$read = Setting::read('App.UniqueArray');
$this->assertGreaterThan(0, count($read));
$this->assertEquals([1,2=>3,'one','two'=>'three'], Setting::read('App.UniqueArray'));

}

/**
Expand Down Expand Up @@ -197,6 +208,31 @@ public function testWrite()
$this->assertEquals(1, $value->editable);
$this->assertEquals(20, $value->weight);
$this->assertEquals(1, $value->autoload);

Setting::write('App.WriteArray',[1,2=>3,'one','two'=>'three'],[
'description' => 'Short description',
'type' => 'array',
'editable' => true,
'options' => [
1 => 'One',
2 => 'Two'
],
'weight' => 20,
'autoload' => true,
]);

$this->assertEquals(3, $this->Settings->find('all')->count());

$value = $this->Settings->get(3);
$this->assertEquals('App.WriteArray', $value->name);
$this->assertEquals('App.WriteArray', $value->key);
$this->assertEquals('a:4:{i:0;i:1;i:2;i:3;i:3;s:3:"one";s:3:"two";s:5:"three";}', $value->value);
$this->assertEquals('Short description', $value->description);
$this->assertEquals('array', $value->type);
$this->assertEquals(1, $value->editable);
$this->assertEquals(20, $value->weight);
$this->assertEquals(1, $value->autoload);

}

/**
Expand Down