From 8fec711cdb449dc1a10c1480ea1365de8bd32426 Mon Sep 17 00:00:00 2001 From: dcoda Date: Thu, 15 Oct 2015 15:30:57 +0100 Subject: [PATCH 1/8] 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/8] 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/8] 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/8] 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/8] 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 643a1ccbbdbd30621f09778af79642e878145c15 Mon Sep 17 00:00:00 2001 From: dcoda Date: Mon, 19 Oct 2015 15:42:13 +0100 Subject: [PATCH 6/8] Travis Commits --- src/Core/Setting.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index dc15687..dcc72cd 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -91,16 +91,13 @@ 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.'.%']); if ($data->count() > 0) { $data = $data->toArray(); - foreach($data as $data_set) { if(self::_serialized($data_set->value)) @@ -112,14 +109,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']); @@ -273,8 +268,7 @@ public static function register($key, $value, $data = []) self::autoLoad(); - if(is_array($value)) - { + if(is_array($value)) { $value = seralize($value); } From 42e662b42a63868a178a2db3bc95031a8f464383 Mon Sep 17 00:00:00 2001 From: dcoda Date: Mon, 19 Oct 2015 17:00:25 +0100 Subject: [PATCH 7/8] phpcs compliancy Applied compliancy for phpcs. --- src/Core/Setting.php | 113 +++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 47 deletions(-) diff --git a/src/Core/Setting.php b/src/Core/Setting.php index dcc72cd..49b8d47 100644 --- a/src/Core/Setting.php +++ b/src/Core/Setting.php @@ -1,5 +1,6 @@ count() > 0) { $data = $data->first()->toArray(); } else { - $data = $model->find() - ->select(['name', 'value']) - ->where(['name LIKE' => $key.'.%']); + $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); + foreach ($data as $dataSet) { + if (self::_serialized($dataSet->value)) { + $dataSet->value = unserialize($dataSet->value); } - static::$_values = Hash::insert(static::$_values, $data_set->name, $data_set->value); + static::$_values = Hash::insert(static::$_values, $dataSet->name, $dataSet->value); } $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']); @@ -129,6 +132,7 @@ public static function read($key = null, $type = null) } /** + * * write * * Method to write data to database. @@ -147,9 +151,9 @@ public static function read($key = null, $type = null) * 'editable' => 0, * ] * - * @param string $key Key of the value. Must contain an prefix. - * @param mixed $value The value of the key. - * @param array $options Options array. + * @param string $key Key of the value. Must contain an prefix. + * @param mixed $value The value of the key. + * @param array $options Options array. * @return void|bool */ public static function write($key, $value = null, $options = []) @@ -169,14 +173,14 @@ public static function write($key, $value = null, $options = []) $model = self::model(); - if(is_array($value) && !empty($value)) { + 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 { @@ -198,6 +202,7 @@ public static function write($key, $value = null, $options = []) } /** + * * check * * Checks if an specific key exists. @@ -229,6 +234,7 @@ public static function check($key) } /** + * * model * * Returns an instance of the Configurations-model (Table). @@ -251,13 +257,14 @@ public static function model($model = null) } /** + * * register * * Registers a setting and its default values. * - * @param string $key The key. - * @param mixed $value The default value. - * @param array $data Custom data. + * @param string $key The key. + * @param mixed $value The default value. + * @param array $data Custom data. * @return void */ public static function register($key, $value, $data = []) @@ -268,7 +275,7 @@ public static function register($key, $value, $data = []) self::autoLoad(); - if(is_array($value)) { + if (is_array($value)) { $value = seralize($value); } @@ -291,10 +298,11 @@ public static function register($key, $value, $data = []) } /** + * * options * - * @param string $key Key for options. - * @param array $value Options to use. + * @param string $key Key for options. + * @param array $value Options to use. * @return mixed */ public static function options($key, $value = null) @@ -315,6 +323,7 @@ public static function options($key, $value = null) } /** + * * autoLoad * * AutoLoad method. @@ -342,6 +351,7 @@ public static function autoLoad() } /** + * * clear * * Clears all settings out of the class. Settings @@ -357,12 +367,13 @@ public static function clear($reload = false) } /** + * * _store * * Stores recent data in the $_data-variable. * - * @param string $key The key. - * @param mixed $value The value. + * @param string $key The key. + * @param mixed $value The value. * @return void */ protected static function _store($key, $value) @@ -371,6 +382,7 @@ protected static function _store($key, $value) } /** + * * _tableExists * * @return bool @@ -389,40 +401,47 @@ protected static function _tableExists() /** * _serialized * + * @param string $value The value. + * @param mixed $result The result (null default). * @return bool */ - protected static function _serialized( $value, &$result = null ) { - - if ( ! is_string( $value ) ) { + protected static function _serialized($value, $result = null) + { + if (! is_string($value)) { return false; } - if ( 'b:0;' === $value ) { + if ('b:0;' === $value) { $result = false; return true; } - $length = strlen($value); - $end = ''; + $length = strlen($value); + $end = ''; - if ( isset( $value[0] ) ) { + if (isset($value[0])) { switch ($value[0]) { case 's': - if ( '"' !== $value[$length - 2] ) + if ('"' !== $value[$length - 2]) { return false; - + } + // no break case 'b': + // no break case 'i': + // no break case 'd': - $end .= ';'; + // no break case 'a': + // no break case 'O': $end .= '}'; - - if ( ':' !== $value[1] ) + + if (':' !== $value[1]) { return false; - - switch ( $value[2] ) { + } + + switch ($value[2]) { case 0: case 1: case 2: @@ -433,30 +452,30 @@ protected static function _serialized( $value, &$result = null ) { case 7: case 8: case 9: - break; + break; default: return false; } + // break appled in embedded switch case 'N': $end .= ';'; - if ( $value[$length - 1] !== $end[0] ) + if ($value[$length - 1] !== $end[0]) { return false; - break; + } + break; default: return false; } } - if ( ( $result = unserialize($value) ) === false ) { + if (( $result = unserialize($value) ) === false) { $result = null; return false; } return true; } - - } From 601502f2eac46725518668f2b99eb2837d8b68cb Mon Sep 17 00:00:00 2001 From: dcoda Date: Mon, 19 Oct 2015 17:16:16 +0100 Subject: [PATCH 8/8] phpcs code formatting changes Conformed to phpcs. --- tests/TestCase/Core/SettingTest.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/TestCase/Core/SettingTest.php b/tests/TestCase/Core/SettingTest.php index e6a01f6..cdb69c3 100644 --- a/tests/TestCase/Core/SettingTest.php +++ b/tests/TestCase/Core/SettingTest.php @@ -157,8 +157,7 @@ public function testRead() $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')); - + $this->assertEquals([1, 2 => 3, 'one', 'two' => 'three'], Setting::read('App.UniqueArray')); } /** @@ -209,7 +208,7 @@ public function testWrite() $this->assertEquals(20, $value->weight); $this->assertEquals(1, $value->autoload); - Setting::write('App.WriteArray',[1,2=>3,'one','two'=>'three'],[ + Setting::write('App.WriteArray', [1, 2 => 3, 'one', 'two' => 'three'], [ 'description' => 'Short description', 'type' => 'array', 'editable' => true, @@ -219,20 +218,19 @@ public function testWrite() ], 'weight' => 20, 'autoload' => true, - ]); + ]); $this->assertEquals(3, $this->Settings->find('all')->count()); - $value = $this->Settings->get(3); + $value = $this->Settings->get(3); $this->assertEquals('App.WriteArray', $value->name); - $this->assertEquals('App.WriteArray', $value->key); + $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); - + $this->assertEquals(1, $value->autoload); } /**