f03560e82253c25ead7c68f4044167bb416d03c0
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Core;
3 use PayPal\Core\PPConfigManager;
4 use PayPal\Exception\PPConfigurationException;
5 /**
6  * PPConfigManager loads the SDK configuration file and
7  * hands out appropriate config params to other classes
8  */
9
10
11 class PPConfigManager {
12
13         public $config;
14         /**
15          * @var PPConfigManager
16          */
17         
18         //default config values
19         public static $defaults = array(
20                 "http.ConnectionTimeOut" => "30",
21                 "http.Retry" => "5",
22         );
23         
24         private static $instance;
25
26         private function __construct(){
27                 if(defined('PP_CONFIG_PATH')) {
28                         $configFile = constant('PP_CONFIG_PATH') . '/sdk_config.ini';
29                 } else {                
30                         $configFile = implode(DIRECTORY_SEPARATOR,
31                                 array(dirname(__FILE__), "..", "config", "sdk_config.ini"));
32                 }
33                 $this->load($configFile);
34         }
35
36         // create singleton object for PPConfigManager
37         public static function getInstance()
38         {
39                 if ( !isset(self::$instance) ) {
40                         self::$instance = new PPConfigManager();
41                 }
42                 return self::$instance;
43         }
44
45         //used to load the file
46         private function load($fileName) {
47
48                 $this->config = @parse_ini_file($fileName);
49                 if($this->config == NULL || count($this->config) == 0) {
50                         throw new PPConfigurationException("Config file $fileName not found","303");
51                 }
52         }
53
54         /**
55          * simple getter for configuration params
56          * If an exact match for key is not found,
57          * does a "contains" search on the key
58          */
59         public function get($searchKey){
60
61                 if(array_key_exists($searchKey, $this->config))
62                 {
63                         return $this->config[$searchKey];
64                 }
65                 else {
66                         $arr = array();
67                         foreach ($this->config as $k => $v){
68                                 if(strstr($k, $searchKey)){
69                                         $arr[$k] = $v;
70                                 }
71                         }
72                         
73                         return $arr;
74                 }
75
76         }
77
78         /**
79          * Utility method for handling account configuration
80          * return config key corresponding to the API userId passed in
81          *
82          * If $userId is null, returns config keys corresponding to
83          * all configured accounts
84          */
85         public function getIniPrefix($userId = null) {
86
87                 if($userId == null) {
88                         $arr = array();
89                         foreach ($this->config as $key => $value) {
90                                 $pos = strpos($key, '.');
91                                 if(strstr($key, "acct")){
92                                         $arr[] = substr($key, 0, $pos);
93                                 }
94                         }
95                         return array_unique($arr);
96                 } else {
97                         $iniPrefix = array_search($userId, $this->config);
98                         $pos = strpos($iniPrefix, '.');
99                         $acct = substr($iniPrefix, 0, $pos);
100                         
101                         return $acct;
102                 }
103         }
104         
105         /**
106          * returns the config file hashmap
107          * 
108          */
109         public function getConfigHashmap()
110         {
111                 return $this->config;
112         }
113         
114         /**
115          * use  the default configuration if it is not passed in hashmap
116          */
117         public static function mergrDefaults($config)
118         {
119                 return array_merge(PPConfigManager::$defaults, $config);
120         }
121 }
122
123