Skip to content

Commit df0d504

Browse files
committed
updated SettingsCache to use php-platform/json-cache package
1 parent 3e9b9e2 commit df0d504

File tree

5 files changed

+67
-110
lines changed

5 files changed

+67
-110
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
"homepage" : "https://github.com/PHPPlatform/config",
1010
"authors" : [{
1111
"name" : "Raghavendra K R",
12-
"email" : "[email protected]",
12+
"email" : "[email protected]",
1313
"homepage" : "http://krraghavendra.in",
1414
"role" : "Software Developer"
1515
}
1616
],
1717
"require" : {
18-
"php" : ">=5.3"
18+
"php" : ">=5.3",
19+
"php-platform/json-cache" : "~0.1"
1920
},
2021
"require-dev" : {
2122
"phpunit/phpunit" : "~4.8"

composer.lock

Lines changed: 48 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Config/Settings.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class Settings{
2323
public static function getSettings($package,$setting = null){
2424

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

5655
$absoluteSettings = $settings;
57-
foreach (array_reverse($packagePaths) as $packagePath){
56+
foreach (array_reverse(preg_split('/\/|\\\/', $package)) as $packagePath){
5857
$absoluteSettings = array($packagePath=>$absoluteSettings);
5958
}
6059
SettingsCache::getInstance()->setData($absoluteSettings);
6160
}
6261

6362
if(is_string($setting)){
64-
$settingPaths = self::getPaths($package_.".".$setting);
65-
$settings = SettingsCache::getInstance()->getData($settingPaths);
63+
$settings = SettingsCache::getInstance()->getData($package_.".".$setting);
6664
}
6765

6866
return $settings;
6967

7068
}
7169

72-
/**
73-
* this method get the path array from string key
74-
* @param string $key
75-
*/
76-
private static function getPaths($key){
77-
$paths = array();
78-
$sourcePaths = explode(".", $key);
79-
foreach ($sourcePaths as $sourcePath){
80-
$subPaths = preg_split('/\[(.*?)\]/',$sourcePath,-1,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
81-
if($subPaths !== FALSE){
82-
$paths = array_merge($paths,$subPaths);
83-
}
84-
}
85-
return $paths;
86-
}
87-
8870
}
8971

src/Config/SettingsCache.php

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,95 +7,24 @@
77

88
namespace PhpPlatform\Config;
99

10+
use PhpPlatform\JSONCache\Cache;
11+
1012
/**
1113
* Singleton implementation for caching the settings
1214
*
1315
* @author raghavendra
1416
*/
15-
final class SettingsCache{
17+
final class SettingsCache extends Cache{
1618

17-
private static $cacheObj = null;
18-
19-
private $settings = array();
20-
private $cacheFileName = "e412523shyugtr345";
21-
22-
23-
private function __construct(){
24-
$this->cacheFileName = sys_get_temp_dir()."/".$this->cacheFileName;
25-
if(is_file($this->cacheFileName)){
26-
$fileContents = file_get_contents($this->cacheFileName);
27-
$this->settings = json_decode($fileContents,true);
28-
if($this->settings === NULL){
29-
$this->settings = array();
30-
}
31-
}
32-
}
33-
34-
/**
35-
* @return \PhpPlatform\Config\SettingsCache
36-
*/
37-
public static function getInstance(){
38-
if(self::$cacheObj == null){
39-
self::$cacheObj = new SettingsCache();
40-
}
41-
return self::$cacheObj;
42-
}
43-
44-
/**
45-
* @param array $path , key path for finding the settings in cache
46-
* @return NULL|mixed
47-
*/
48-
function getData(array $path) {
49-
if(is_array($path) && count($path) > 0){
50-
$value = $this->settings;
51-
foreach($path as $pathElem){
52-
if (isset($value[$pathElem])) {
53-
$value = $value[$pathElem];
54-
} else {
55-
return NULL;
56-
}
57-
}
58-
return $value;
59-
}
60-
return NULL;
61-
}
19+
private static $cacheObj = null;
20+
protected $cacheFileName = "settingscache236512233125"; // cache for settings
6221

63-
/**
64-
* @param array $setting , setting to merge with current settings
65-
* @return boolean, TRUE on success , FALSE on failure
66-
*/
67-
function setData(array $setting) {
68-
$originalSettings = $this->settings;
69-
$this->settings = array_merge_recursive($this->settings,$setting);
70-
$jsonSettings = json_encode($this->settings);
71-
if($jsonSettings === FALSE){
72-
return FALSE;
73-
}
74-
if(file_put_contents($this->cacheFileName, $jsonSettings) === FALSE){
75-
$this->settings = $originalSettings;
76-
return FALSE;
77-
}
78-
return TRUE;
79-
}
80-
81-
/**
82-
* resets complete cache to empty
83-
* @return boolean, TRUE on success , FALSE on failure
84-
*/
85-
private function resetCache(){
86-
$originalSettings = $this->settings;
87-
$this->settings = array();
88-
if(file_put_contents($this->cacheFileName, "{}") === FALSE){
89-
$this->settings = $originalSettings;
90-
return FALSE;
91-
}
92-
return TRUE;
22+
public static function getInstance(){
23+
if(self::$cacheObj == null){
24+
self::$cacheObj = new SettingsCache();
25+
}
26+
return self::$cacheObj;
9327
}
94-
95-
public static function reset(){
96-
self::getInstance()->resetCache();
97-
}
98-
9928
}
10029

10130
?>

tests/Config/TestSettings.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testGetSettings1(){
3333
$vendorDir = dirname(dirname($classLoaderReflection->getFileName()));
3434
$thisPackageConfigFile = dirname($vendorDir).'/config.json';
3535

36-
SettingsCache::reset();
36+
SettingsCache::getInstance()->reset();
3737

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

76-
SettingsCache::reset();
77-
76+
SettingsCache::getInstance()->reset();
77+
7878
$setting = array("test"=>array("my"=>array("settings"=>array(1,array("here"=>"as a array"),3))));
7979
file_put_contents($packageConfigFile, json_encode($setting));
8080

@@ -98,7 +98,6 @@ public function testGetSettings2(){
9898
// clear data
9999
unlink($packageConfigFile);
100100
rmdir(dirname($packageConfigFile));
101-
rmdir(dirname(dirname($packageConfigFile)));
102101
}
103102

104103

0 commit comments

Comments
 (0)