Skip to content

Commit

Permalink
updated SettingsCache to use php-platform/json-cache package
Browse files Browse the repository at this point in the history
  • Loading branch information
Raaghu committed Jan 12, 2017
1 parent 3e9b9e2 commit df0d504
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 110 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"homepage" : "https://github.com/PHPPlatform/config",
"authors" : [{
"name" : "Raghavendra K R",
"email" : "raaghu1987@gmail.com",
"email" : "krraghavendra@yahoo.co.in",
"homepage" : "http://krraghavendra.in",
"role" : "Software Developer"
}
],
"require" : {
"php" : ">=5.3"
"php" : ">=5.3",
"php-platform/json-cache" : "~0.1"
},
"require-dev" : {
"phpunit/phpunit" : "~4.8"
Expand Down
50 changes: 48 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 3 additions & 21 deletions src/Config/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class Settings{
public static function getSettings($package,$setting = null){

$package_ = preg_replace('/\/|\\\\/', ".", $package);
$packagePaths = self::getPaths($package_);
$settings = SettingsCache::getInstance()->getData($packagePaths);
$settings = SettingsCache::getInstance()->getData($package_);
if($settings === NULL){
$classLoaderReflection = new \ReflectionClass(new ClassLoader());
$vendorDir = dirname(dirname($classLoaderReflection->getFileName()));
Expand Down Expand Up @@ -54,36 +53,19 @@ public static function getSettings($package,$setting = null){
$settings = json_decode(file_get_contents($packageConfigFile),true);

$absoluteSettings = $settings;
foreach (array_reverse($packagePaths) as $packagePath){
foreach (array_reverse(preg_split('/\/|\\\/', $package)) as $packagePath){
$absoluteSettings = array($packagePath=>$absoluteSettings);
}
SettingsCache::getInstance()->setData($absoluteSettings);
}

if(is_string($setting)){
$settingPaths = self::getPaths($package_.".".$setting);
$settings = SettingsCache::getInstance()->getData($settingPaths);
$settings = SettingsCache::getInstance()->getData($package_.".".$setting);
}

return $settings;

}

/**
* this method get the path array from string key
* @param string $key
*/
private static function getPaths($key){
$paths = array();
$sourcePaths = explode(".", $key);
foreach ($sourcePaths as $sourcePath){
$subPaths = preg_split('/\[(.*?)\]/',$sourcePath,-1,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
if($subPaths !== FALSE){
$paths = array_merge($paths,$subPaths);
}
}
return $paths;
}

}

91 changes: 10 additions & 81 deletions src/Config/SettingsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,24 @@

namespace PhpPlatform\Config;

use PhpPlatform\JSONCache\Cache;

/**
* Singleton implementation for caching the settings
*
* @author raghavendra
*/
final class SettingsCache{
final class SettingsCache extends Cache{

private static $cacheObj = null;

private $settings = array();
private $cacheFileName = "e412523shyugtr345";


private function __construct(){
$this->cacheFileName = sys_get_temp_dir()."/".$this->cacheFileName;
if(is_file($this->cacheFileName)){
$fileContents = file_get_contents($this->cacheFileName);
$this->settings = json_decode($fileContents,true);
if($this->settings === NULL){
$this->settings = array();
}
}
}

/**
* @return \PhpPlatform\Config\SettingsCache
*/
public static function getInstance(){
if(self::$cacheObj == null){
self::$cacheObj = new SettingsCache();
}
return self::$cacheObj;
}

/**
* @param array $path , key path for finding the settings in cache
* @return NULL|mixed
*/
function getData(array $path) {
if(is_array($path) && count($path) > 0){
$value = $this->settings;
foreach($path as $pathElem){
if (isset($value[$pathElem])) {
$value = $value[$pathElem];
} else {
return NULL;
}
}
return $value;
}
return NULL;
}
private static $cacheObj = null;
protected $cacheFileName = "settingscache236512233125"; // cache for settings

/**
* @param array $setting , setting to merge with current settings
* @return boolean, TRUE on success , FALSE on failure
*/
function setData(array $setting) {
$originalSettings = $this->settings;
$this->settings = array_merge_recursive($this->settings,$setting);
$jsonSettings = json_encode($this->settings);
if($jsonSettings === FALSE){
return FALSE;
}
if(file_put_contents($this->cacheFileName, $jsonSettings) === FALSE){
$this->settings = $originalSettings;
return FALSE;
}
return TRUE;
}

/**
* resets complete cache to empty
* @return boolean, TRUE on success , FALSE on failure
*/
private function resetCache(){
$originalSettings = $this->settings;
$this->settings = array();
if(file_put_contents($this->cacheFileName, "{}") === FALSE){
$this->settings = $originalSettings;
return FALSE;
}
return TRUE;
public static function getInstance(){
if(self::$cacheObj == null){
self::$cacheObj = new SettingsCache();
}
return self::$cacheObj;
}

public static function reset(){
self::getInstance()->resetCache();
}

}

?>
7 changes: 3 additions & 4 deletions tests/Config/TestSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testGetSettings1(){
$vendorDir = dirname(dirname($classLoaderReflection->getFileName()));
$thisPackageConfigFile = dirname($vendorDir).'/config.json';

SettingsCache::reset();
SettingsCache::getInstance()->reset();

$setting = array("test"=>array("my"=>array("settings"=>array(1,array("here"=>"as a array"),3))));
file_put_contents($thisPackageConfigFile, json_encode($setting));
Expand Down Expand Up @@ -73,8 +73,8 @@ public function testGetSettings2(){
mkdir(dirname($packageConfigFile),"0777",true);
}

SettingsCache::reset();
SettingsCache::getInstance()->reset();

$setting = array("test"=>array("my"=>array("settings"=>array(1,array("here"=>"as a array"),3))));
file_put_contents($packageConfigFile, json_encode($setting));

Expand All @@ -98,7 +98,6 @@ public function testGetSettings2(){
// clear data
unlink($packageConfigFile);
rmdir(dirname($packageConfigFile));
rmdir(dirname(dirname($packageConfigFile)));
}


Expand Down

0 comments on commit df0d504

Please sign in to comment.