1 <?php
2
3 namespace PayPal\Core;
4
5 /**
6 * Class PayPalConfigManager
7 *
8 * PayPalConfigManager loads the SDK configuration file and
9 * hands out appropriate config params to other classes
10 *
11 * @package PayPal\Core
12 */
13 class PayPalConfigManager
14 {
15
16 /**
17 * Configuration Options
18 *
19 * @var array
20 */
21 private $configs = array(
22 );
23
24 /**
25 * Singleton Object
26 *
27 * @var $this
28 */
29 private static $instance;
30
31 /**
32 * Private Constructor
33 */
34 private function __construct()
35 {
36 if (defined('PP_CONFIG_PATH')) {
37 $configFile = constant('PP_CONFIG_PATH') . '/sdk_config.ini';
38 } else {
39 $configFile = implode(DIRECTORY_SEPARATOR,
40 array(dirname(__FILE__), "..", "config", "sdk_config.ini"));
41 }
42 if (file_exists($configFile)) {
43 $this->addConfigFromIni($configFile);
44 }
45 }
46
47 /**
48 * Returns the singleton object
49 *
50 * @return $this
51 */
52 public static function getInstance()
53 {
54 if (!isset(self::$instance)) {
55 self::$instance = new self();
56 }
57 return self::$instance;
58 }
59
60 /**
61 * Add Configuration from configuration.ini files
62 *
63 * @param string $fileName
64 * @return $this
65 */
66 public function addConfigFromIni($fileName)
67 {
68 if ($configs = parse_ini_file($fileName)) {
69 $this->addConfigs($configs);
70 }
71 return $this;
72 }
73
74 /**
75 * If a configuration exists in both arrays,
76 * then the element from the first array will be used and
77 * the matching key's element from the second array will be ignored.
78 *
79 * @param array $configs
80 * @return $this
81 */
82 public function addConfigs($configs = array())
83 {
84 $this->configs = $configs + $this->configs;
85 return $this;
86 }
87
88 /**
89 * Simple getter for configuration params
90 * If an exact match for key is not found,
91 * does a "contains" search on the key
92 *
93 * @param string $searchKey
94 * @return array
95 */
96 public function get($searchKey)
97 {
98 if (array_key_exists($searchKey, $this->configs)) {
99 return $this->configs[$searchKey];
100 } else {
101 $arr = array();
102 if ($searchKey !== '') {
103 foreach ($this->configs as $k => $v) {
104 if (strstr($k, $searchKey)) {
105 $arr[$k] = $v;
106 }
107 }
108 }
109
110 return $arr;
111 }
112 }
113
114 /**
115 * Utility method for handling account configuration
116 * return config key corresponding to the API userId passed in
117 *
118 * If $userId is null, returns config keys corresponding to
119 * all configured accounts
120 *
121 * @param string|null $userId
122 * @return array|string
123 */
124 public function getIniPrefix($userId = null)
125 {
126 if ($userId == null) {
127 $arr = array();
128 foreach ($this->configs as $key => $value) {
129 $pos = strpos($key, '.');
130 if (strstr($key, "acct")) {
131 $arr[] = substr($key, 0, $pos);
132 }
133 }
134 return array_unique($arr);
135 } else {
136 $iniPrefix = array_search($userId, $this->configs);
137 $pos = strpos($iniPrefix, '.');
138 $acct = substr($iniPrefix, 0, $pos);
139
140 return $acct;
141 }
142 }
143
144 /**
145 * returns the config file hashmap
146 */
147 public function getConfigHashmap()
148 {
149 return $this->configs;
150 }
151
152 /**
153 * Disabling __clone call
154 */
155 public function __clone()
156 {
157 trigger_error('Clone is not allowed.', E_USER_ERROR);
158 }
159 }
160