From 8fec711cdb449dc1a10c1480ea1365de8bd32426 Mon Sep 17 00:00:00 2001 From: dcoda Date: Thu, 15 Oct 2015 15:30:57 +0100 Subject: [PATCH 1/6] Added array support Added in support for setting arrays --- src/Core/Setting.php | 118 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 2 deletions(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index fe3a5c4..2c81f68 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -17,6 +17,7 @@ use Cake\Datasource\ConnectionManager; use Cake\ORM\TableRegistry; use Cake\Utility\Hash; +use Cake\Core\Configure; class Setting { @@ -26,6 +27,13 @@ class Setting * @var array */ protected static $_data = []; + + /** + * Array of values currently stored in Configure. + * + * @var array + */ + protected static $_values = []; /** * Options @@ -84,10 +92,37 @@ public static function read($key = null, $type = null) if ($data->count() > 0) { $data = $data->first()->toArray(); - } else { - return null; + } + else { + + $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']; @@ -234,6 +269,11 @@ public static function register($key, $value, $data = []) } self::autoLoad(); + + if(is_array($value)) + { + $value = seralize($value); + } $_data = [ 'value' => $value, @@ -348,4 +388,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': + // This looks odd but it is quicker than isset()ing + $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; + } + + } From f29b240210e301d8a1c97889b6ee14c694781d1b Mon Sep 17 00:00:00 2001 From: dcoda Date: Thu, 15 Oct 2015 15:33:25 +0100 Subject: [PATCH 2/6] Removed comment --- src/Core/Setting.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index 2c81f68..ed0c25d 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -416,7 +416,7 @@ protected static function _serialized( $value, &$result = null ) { case 'b': case 'i': case 'd': - // This looks odd but it is quicker than isset()ing + $end .= ';'; case 'a': case 'O': From 5ea3eda968dd54e712ff8b6da42e8ab5a7c8cbdb Mon Sep 17 00:00:00 2001 From: dcoda Date: Thu, 15 Oct 2015 15:41:19 +0100 Subject: [PATCH 3/6] Removed Configure Not required. Originally added for testing purposes to compare Configure::write against Setting::write. --- src/Core/Setting.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index ed0c25d..572a7dd 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -17,7 +17,6 @@ use Cake\Datasource\ConnectionManager; use Cake\ORM\TableRegistry; use Cake\Utility\Hash; -use Cake\Core\Configure; class Setting { From 83bfe8075114fe14c007ab835dabade764e41048 Mon Sep 17 00:00:00 2001 From: dcoda Date: Thu, 15 Oct 2015 15:54:05 +0100 Subject: [PATCH 4/6] Added serialize feature Serializes arrays if used within Setting::write --- src/Core/Setting.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index 572a7dd..8867c0c 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -178,6 +178,10 @@ public static function write($key, $value = null, $options = []) if ($options['overrule']) { $data = $model->findByName($key)->first(); if ($data) { + if(is_array($value) && !empty($value)) { + $value = serialize($value); + } + $data->set('value', $value); $model->save($data); } else { From 456ee577304f2dc61e67294aef636bf0e85627d4 Mon Sep 17 00:00:00 2001 From: dcoda Date: Mon, 19 Oct 2015 14:00:35 +0100 Subject: [PATCH 5/6] Tidy up plus test cases General tidy up of code removing error suppression. Changes made following phpunit tests as well as test cases added for testing Setting::read/write arrays. --- src/Core/Setting.php | 36 ++++++++++++++--------------- tests/TestCase/Core/SettingTest.php | 36 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index 8867c0c..dc15687 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -105,7 +105,7 @@ public static function read($key = null, $type = null) { if(self::_serialized($data_set->value)) { - $data_set->value = @unserialize($data_set->value); + $data_set->value = unserialize($data_set->value); } static::$_values = Hash::insert(static::$_values, $data_set->name, $data_set->value); } @@ -120,7 +120,7 @@ public static function read($key = null, $type = null) if(self::_serialized($data['value'])) { - $data['value'] = @unserialize($data['value']); + $data['value'] = unserialize($data['value']); } self::_store($key, $data['value']); @@ -173,15 +173,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(is_array($value) && !empty($value)) { - $value = serialize($value); - } - + if ($data) { $data->set('value', $value); $model->save($data); } else { @@ -400,12 +400,12 @@ protected static function _tableExists() protected static function _serialized( $value, &$result = null ) { if ( ! is_string( $value ) ) { - return FALSE; + return false; } if ( 'b:0;' === $value ) { - $result = FALSE; - return TRUE; + $result = false; + return true; } $length = strlen($value); $end = ''; @@ -414,7 +414,7 @@ protected static function _serialized( $value, &$result = null ) { switch ($value[0]) { case 's': if ( '"' !== $value[$length - 2] ) - return FALSE; + return false; case 'b': case 'i': @@ -426,7 +426,7 @@ protected static function _serialized( $value, &$result = null ) { $end .= '}'; if ( ':' !== $value[1] ) - return FALSE; + return false; switch ( $value[2] ) { case 0: @@ -442,26 +442,26 @@ protected static function _serialized( $value, &$result = null ) { break; default: - return FALSE; + return false; } case 'N': $end .= ';'; if ( $value[$length - 1] !== $end[0] ) - return FALSE; + return false; break; default: - return FALSE; + return false; } } - if ( ( $result = @unserialize($value) ) === FALSE ) { + if ( ( $result = unserialize($value) ) === false ) { $result = null; - return FALSE; + return false; } - return TRUE; + return true; } diff --git a/tests/TestCase/Core/SettingTest.php b/tests/TestCase/Core/SettingTest.php index 855c322..e6a01f6 100644 --- a/tests/TestCase/Core/SettingTest.php +++ b/tests/TestCase/Core/SettingTest.php @@ -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')); + } /** @@ -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); + } /** From 0d5b7821f59e5306bab2aaebb334f7c682e2f5b5 Mon Sep 17 00:00:00 2001 From: dcoda Date: Mon, 19 Oct 2015 15:33:42 +0100 Subject: [PATCH 6/6] Strict Travis Conforms --- src/Core/Setting.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index dc15687..1ca7b7b 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -91,9 +91,8 @@ public static function read($key = null, $type = null) if ($data->count() > 0) { $data = $data->first()->toArray(); - } - else { - + } else { + $data = $model->find() ->select(['name', 'value']) ->where(['name LIKE' => $key.'.%']); @@ -103,8 +102,7 @@ public static function read($key = null, $type = null) foreach($data as $data_set) { - if(self::_serialized($data_set->value)) - { + 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); @@ -112,14 +110,12 @@ public static function read($key = null, $type = null) $data['value'] = static::$_values; } - else - { + else { return null; } } - if(self::_serialized($data['value'])) - { + if(self::_serialized($data['value'])) { $data['value'] = unserialize($data['value']); } self::_store($key, $data['value']);