Skip to content

Instantly share code, notes, and snippets.

@chernjie
Last active December 18, 2015 09:59
Show Gist options
  • Save chernjie/5765204 to your computer and use it in GitHub Desktop.
Save chernjie/5765204 to your computer and use it in GitHub Desktop.
SystemProperty_model
<?php
abstract class M3SystemProperty_model extends MY_Model
{
/**
* @var M3SystemProperty_model
*/
protected $system_property_api = null;
protected $system_property_cache = array('enabled'=>FALSE, 'expiry'=>300, 'driver'=>null);
protected static $GET_SYSTEM_PROPERTY = '/system/property/%s';
public function __construct()
{
parent::__construct();
// System Property Config
$system_property_config = $this->config->item('system_property');
// Cache System Property
$this->system_property_cache['enabled'] = !empty($system_property_config['cache']['enabled']);
if($this->system_property_cache['enabled'])
{
$this->load->driver('cache');
$this->system_property_cache['expiry'] = (isset($system_property_config['cache']['expiry']) ? $system_property_config['cache']['expiry'] : 300);
$this->system_property_cache['driver'] = $this->cache->xcache;
}
}
const INVALID_KEY_HOLDER = '!!INVALID_SYSTEM_PROPERTY_KEY';
private function get_property($property_name)
{
if (! ($this->system_property_api instanceof M3api))
// meant to be an uncaught exception
throw new Exception('SystemProperty_model needs to be instantiated');
try
{
$key = sprintf(self::$GET_SYSTEM_PROPERTY, $property_name);
if($this->system_property_cache['enabled'])
{
$system_property = $this->system_property_cache['driver']->get($key);
if ($system_property == self::INVALID_KEY_HOLDER)
throw new Exception(self::INVALID_KEY_HOLDER . ': ' . $key);
if(is_null($system_property))
{
try
{
$system_property = $this->system_property_api->get($key);
$this->system_property_cache['driver']->save(
$key
, $system_property
, $this->system_property_cache['expiry']
);
}
catch (Exception $ex)
{
// Put a holder in place to preven repetitive calls to the API
$this->system_property_cache['driver']->save(
$key
, self::INVALID_KEY_HOLDER
, $this->system_property_cache['expiry']
);
}
}
return $system_property;
}
else
{
return $this->system_property_api->get($key);
}
}
catch(Exception $ex)
{
return null;
}
}
public function get_boolean($property_name, $property_default_value = null)
{
$value = $this->get_property($property_name);
if($value === 'false')
$value = false;
return is_null($value) ? $property_default_value : (boolean) trim($value);
}
public function get_float($property_name, $property_default_value = null)
{
$value = $this->get_property($property_name);
return is_null($value) ? $property_default_value : (float) trim($value);
}
public function get_integer($property_name, $property_default_value = null)
{
$value = $this->get_property($property_name);
return is_null($value) ? $property_default_value : (integer) trim($value);
}
public function get_string($property_name, $property_default_value = null)
{
$value = $this->get_property($property_name);
return is_null($value) ? $property_default_value : $value;
}
public function get_array($property_name, $property_default_value = null, $delimiter = ';')
{
$value = $this->get_property($property_name);
return is_null($value) ? $property_default_value: explode($delimiter, $value);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment