Skip to content

Instantly share code, notes, and snippets.

@pgasiorowski
Last active August 29, 2015 14:01
Show Gist options
  • Save pgasiorowski/55d1abcf6a719a594e40 to your computer and use it in GitHub Desktop.
Save pgasiorowski/55d1abcf6a719a594e40 to your computer and use it in GitHub Desktop.
PhalconeEye - Core\Model\Settings proposal
<?php
/*
+------------------------------------------------------------------------+
| PhalconEye CMS |
+------------------------------------------------------------------------+
| Copyright (c) 2013-2014 PhalconEye Team (http://phalconeye.com/) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconeye.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Author: Ivan Vorontsov <ivan.vorontsov@phalconeye.com> |
+------------------------------------------------------------------------+
*/
namespace Core\Model;
use Engine\Db\AbstractModel;
use Phalcon\Mvc\Model\ResultsetInterface;
/**
* Settings.
*
* @category PhalconEye
* @package Core\Model
* @author Ivan Vorontsov <ivan.vorontsov@phalconeye.com>
* @copyright 2013-2014 PhalconEye Team
* @license New BSD License
* @link http://phalconeye.com/
*
* @Source("settings")
*/
class Settings extends AbstractModel
{
const
/**
* Cache prefix.
*/
CACHE_PREFIX = 'setting_';
/**
* @Primary
* @Identity
* @Column(type="string", nullable=false, column="name", size="60")
*/
public $name;
/**
* @Column(type="string", nullable=false, column="value", size="250")
*/
public $value;
/**
* Save settings and clear cache
*
* {@inheritdoc}
*/
public function save($data=null, $whiteList=null)
{
if (!empty($this->value) && !isset($data['value'])) {
parent::save($data, $whiteList) and $this->clearCache();
} else {
$this->delete();
}
return true;
}
/**
* Delete setting and clear cache
*
* {@inheritdoc}
*/
public function delete()
{
parent::delete() and $this->clearCache();
return true;
}
/**
* Update cache with new value
*/
protected function updateCache()
{
$this->getDI()->get('cacheData')->save(self::CACHE_PREFIX . $this->name, $this->value);
}
/**
* Delete setting from cache
*/
protected function clearCache() {
$this->getDI()->get('cacheData')->delete(self::CACHE_PREFIX . $this->name);
}
/**
* Get module's setting or a list
*
* @param string $module Module name.
* @param null|string $setting Setting name.
* @param null|mixed $default Default value.
*
* @return mixed
*/
public static function getValue($module, $setting = null, $default = null)
{
$settingObject = self::factory($module, $setting);
if (!$settingObject) {
return $default;
}
if ($settingObject instanceof ResultsetInterface) {
$rows = [];
/** @var $entity Settings **/
foreach ($settingObject as $entity) {
$entityName = substr($entity->name, strlen($module .'_'));
$rows[$entityName] = $entity->value;
$entity->updateCache();
}
return $rows;
}
return $settingObject->value;
}
/**
* Set setting by name.
*
* @param string $module Module name.
* @param null|string $setting Setting name.
* @param mixed $value Setting value.
*
* @throw \InvalidArgumentException
*/
public static function setValue($module, $setting, $value)
{
if (empty($setting)) {
throw new \InvalidArgumentException('Missing required $setting');
}
$settingObject = self::factory($module, $setting);
if (!$settingObject) {
$settingObject = new self;
}
$settingObject->name = $module .'_'. $setting;
$settingObject->value = $value;
$settingObject->save();
}
/**
* Create module's setting instance or a resultset
*
* @param string $module Module name.
* @param null|string $setting Setting name.
*
* @return null|Settings|Settings[]
*/
public static function factory($module, $setting = null)
{
if (empty($setting)) {
return self::find(
[
'name LIKE :name:',
'bind' => [
'name' => $module .'_%'
]
]
);
} else {
return self::findFirst(
[
'name = :name:',
'bind' => [
'name' => $module .'_'. $setting
],
'cache' => [
'key' => self::CACHE_PREFIX . $module .'_'. $setting
]
]
);
}
}
/**
* @deprecated.. bla bla bla.. for b/c maybe
*/
public static function getSetting($name, $default = null) {
self::getValue(strstr($name, '_', true), ltrim(strstr($name, '_'), '_'), $default);
}
}
@pgasiorowski
Copy link
Author

|-------------------------------------
|name                     | value    |
|-------------------------------------
|system_default_language  | auto     |
|system_theme             | default  |
|system_title             | d        |
-------------------------------------|

Getting one

Settings::getValue('system', 'default_language')

'default'

Getting many

Settings::getValue('system')

array (
  'default_language' => 'auto',
  'theme' => 'default',
  'title' => 'd',
)

Removing one

Settings::factory('system', 'theme')->delete();

Removing many

Settings::factory('system')->delete();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment