Skip to content
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"cakemanager/cakephp-utils": "dev-master"
},
"require-dev": {
"phpunit/phpunit": " 4.6.*@dev"
"phpunit/phpunit": "3.7.*"
Copy link
Contributor

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?

},
"extra": {
"installer-name": "Settings"
Expand Down
156 changes: 144 additions & 12 deletions src/Core/Setting.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**
*
* CakeManager (http://cakemanager.org)
* Copyright (c) http://cakemanager.org
*
Expand All @@ -20,40 +21,54 @@

class Setting
{

/**
*
Copy link
Contributor

Choose a reason for hiding this comment

The 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
*/
Expand Down Expand Up @@ -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'];
Expand All @@ -100,6 +133,7 @@ public static function read($key = null, $type = null)
}

/**
*
* write
*
* Method to write data to database.
Expand All @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = [])
Expand All @@ -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']) {
Expand All @@ -165,6 +203,7 @@ public static function write($key, $value = null, $options = [])
}

/**
*
* check
*
* Checks if an specific key exists.
Expand Down Expand Up @@ -196,6 +235,7 @@ public static function check($key)
}

/**
*
* model
*
* Returns an instance of the Configurations-model (Table).
Expand All @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

* @return void
*/
public static function register($key, $value, $data = [])
Expand All @@ -234,6 +275,10 @@ public static function register($key, $value, $data = [])
}

self::autoLoad();

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

$_data = [
'value' => $value,
Expand All @@ -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)
Expand All @@ -278,6 +324,7 @@ public static function options($key, $value = null)
}

/**
*
* autoLoad
*
* AutoLoad method.
Expand Down Expand Up @@ -305,6 +352,7 @@ public static function autoLoad()
}

/**
*
* clear
*
* Clears all settings out of the class. Settings
Expand All @@ -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)
Expand All @@ -334,6 +383,7 @@ protected static function _store($key, $value)
}

/**
*
* _tableExists
*
* @return bool
Expand All @@ -348,4 +398,86 @@ protected static function _tableExists()
}
return false;
}

/**
* _serialized
*
* @codeCoverageIgnore
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}
Loading