-
Notifications
You must be signed in to change notification settings - Fork 17
Updated to allow arrays - Update #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8fec711
f29b240
5ea3eda
83bfe80
456ee57
643a1cc
42e662b
601502f
5768e6e
b070a7d
4a3b561
e6f78b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| <?php | ||
| /** | ||
| * | ||
| * CakeManager (http://cakemanager.org) | ||
| * Copyright (c) http://cakemanager.org | ||
| * | ||
|
|
@@ -20,40 +21,54 @@ | |
|
|
||
| class Setting | ||
| { | ||
|
|
||
| /** | ||
| * | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a lot of these changes: new lines / extra spaces / and go on. You should prevent them. They make commits unclear (I can't see your important changes quick enough) and it will mess up the code because of code standard. |
||
| * List of loaded data | ||
| * | ||
| * @var array | ||
| */ | ||
| protected static $_data = []; | ||
|
|
||
| /** | ||
| * | ||
| * Array of values currently stored in Configure. | ||
| * | ||
| * @var array | ||
| */ | ||
| protected static $_values = []; | ||
|
|
||
| /** | ||
| * | ||
| * Options | ||
| * | ||
| * @var array | ||
| */ | ||
| protected static $_options = []; | ||
|
|
||
| /** | ||
| * | ||
| * Holder for the model | ||
| * | ||
| * @var \Cake\ORM\Table | ||
| */ | ||
| protected static $_model = null; | ||
|
|
||
| /** | ||
| * | ||
| * Keeps the boolean if the autoload method has been loaded | ||
| * | ||
| * @var bool | ||
| */ | ||
| protected static $_autoloaded = false; | ||
|
|
||
| /** | ||
| * | ||
| * read | ||
| * | ||
| * Method to read the data. | ||
| * | ||
| * @param string $key Key with the name of the setting. | ||
| * @param string $key Key with the name of the setting. | ||
| * @param string $type The type to return in. | ||
| * @return mixed | ||
| */ | ||
|
|
@@ -85,9 +100,27 @@ 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) { | ||
| static::$_values = []; | ||
| $data = $data->toArray(); | ||
| foreach ($data as $dataSet) { | ||
| if (self::_serialized($dataSet->value)) { | ||
| $dataSet->value = unserialize($dataSet->value); | ||
| } | ||
| static::$_values = Hash::insert(static::$_values, $dataSet->name, $dataSet->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']; | ||
|
|
@@ -100,6 +133,7 @@ public static function read($key = null, $type = null) | |
| } | ||
|
|
||
| /** | ||
| * | ||
| * write | ||
| * | ||
| * Method to write data to database. | ||
|
|
@@ -118,9 +152,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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| * @return void|bool | ||
| */ | ||
| public static function write($key, $value = null, $options = []) | ||
|
|
@@ -139,6 +173,10 @@ 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']) { | ||
|
|
@@ -165,6 +203,7 @@ public static function write($key, $value = null, $options = []) | |
| } | ||
|
|
||
| /** | ||
| * | ||
| * check | ||
| * | ||
| * Checks if an specific key exists. | ||
|
|
@@ -196,6 +235,7 @@ public static function check($key) | |
| } | ||
|
|
||
| /** | ||
| * | ||
| * model | ||
| * | ||
| * Returns an instance of the Configurations-model (Table). | ||
|
|
@@ -218,13 +258,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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here |
||
| * @return void | ||
| */ | ||
| public static function register($key, $value, $data = []) | ||
|
|
@@ -234,6 +275,10 @@ public static function register($key, $value, $data = []) | |
| } | ||
|
|
||
| self::autoLoad(); | ||
|
|
||
| if (is_array($value)) { | ||
| $value = serialize($value); | ||
| } | ||
|
|
||
| $_data = [ | ||
| 'value' => $value, | ||
|
|
@@ -254,10 +299,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) | ||
|
|
@@ -278,6 +324,7 @@ public static function options($key, $value = null) | |
| } | ||
|
|
||
| /** | ||
| * | ||
| * autoLoad | ||
| * | ||
| * AutoLoad method. | ||
|
|
@@ -305,6 +352,7 @@ public static function autoLoad() | |
| } | ||
|
|
||
| /** | ||
| * | ||
| * clear | ||
| * | ||
| * Clears all settings out of the class. Settings | ||
|
|
@@ -320,12 +368,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) | ||
|
|
@@ -334,6 +383,7 @@ protected static function _store($key, $value) | |
| } | ||
|
|
||
| /** | ||
| * | ||
| * _tableExists | ||
| * | ||
| * @return bool | ||
|
|
@@ -348,4 +398,86 @@ protected static function _tableExists() | |
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * _serialized | ||
| * | ||
| * @codeCoverageIgnore | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why ignore the coverage? What should the method do? |
||
| * @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)) { | ||
| 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; | ||
| } | ||
| // 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]) { | ||
| 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; | ||
| } | ||
| // break appled in embedded switch | ||
| 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; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the change to a lower version?