Skip to content

Commit 811c29d

Browse files
committed
fix: remove static config assignments
Using static properties for configs means that you can never initialize more than one SDK object at a time. closes phpclassic#72 and phpclassic#61
1 parent d3c7f3b commit 811c29d

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

lib/AuthHelper.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public static function getCurrentUrl()
3939
*
4040
* @return bool
4141
*/
42-
public static function verifyShopifyRequest()
42+
public static function verifyShopifyRequest( &$config )
4343
{
4444
$data = $_GET;
4545

46-
if(!isset(ShopifySDK::$config['SharedSecret'])) {
46+
if(!isset($config['SharedSecret'])) {
4747
throw new SdkException("Please provide SharedSecret while configuring the SDK client.");
4848
}
4949

50-
$sharedSecret = ShopifySDK::$config['SharedSecret'];
50+
$sharedSecret = $config['SharedSecret'];
5151

5252
//Get the hmac and remove it from array
5353
if (isset($data['hmac'])) {
@@ -87,10 +87,8 @@ public static function verifyShopifyRequest()
8787
*
8888
* @return void|string
8989
*/
90-
public static function createAuthRequest($scopes, $redirectUrl = null, $state = null, $options = null, $return = false)
90+
public static function createAuthRequest(&$config, $scopes, $redirectUrl = null, $state = null, $options = null, $return = false)
9191
{
92-
$config = ShopifySDK::$config;
93-
9492
if(!isset($config['ShopUrl']) || !isset($config['ApiKey'])) {
9593
throw new SdkException("ShopUrl and ApiKey are required for authentication request. Please check SDK configuration!");
9694
}
@@ -136,10 +134,8 @@ public static function createAuthRequest($scopes, $redirectUrl = null, $state =
136134
*
137135
* @return string
138136
*/
139-
public static function getAccessToken()
137+
public static function getAccessToken(&$config)
140138
{
141-
$config = ShopifySDK::$config;
142-
143139
if(!isset($config['SharedSecret']) || !isset($config['ApiKey'])) {
144140
throw new SdkException("SharedSecret and ApiKey are required for getting access token. Please check SDK configuration!");
145141
}

lib/ShopifyResource.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ abstract class ShopifyResource
3939
*/
4040
protected $resourceUrl;
4141

42+
protected $config;
43+
4244
/**
4345
* Key of the API Resource which is used to fetch data from request responses
4446
*
@@ -108,17 +110,17 @@ abstract class ShopifyResource
108110
/**
109111
* Create a new Shopify API resource instance.
110112
*
113+
* @param array $config
111114
* @param integer $id
112115
* @param string $parentResourceUrl
113116
*
114117
* @throws SdkException if Either AccessToken or ApiKey+Password Combination is not found in configuration
115118
*/
116-
public function __construct($id = null, $parentResourceUrl = '')
119+
public function __construct($config, $id = null, $parentResourceUrl = '')
117120
{
121+
$this->config = $config;
118122
$this->id = $id;
119123

120-
$config = ShopifySDK::$config;
121-
122124
$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['AdminUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');
123125

124126
if (isset($config['AccessToken'])) {
@@ -181,7 +183,7 @@ public function __call($name, $arguments)
181183
$resourceID = !empty($arguments) ? $arguments[0] : null;
182184

183185

184-
$api = new $childClass($resourceID, $this->resourceUrl);
186+
$api = new $childClass($this->config, $resourceID, $this->resourceUrl);
185187

186188
return $api;
187189
} else {

lib/ShopifySDK.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class ShopifySDK
147147
*
148148
* @var array
149149
*/
150-
public static $config = array();
150+
public $config = array();
151151

152152
/**
153153
* List of available resources which can be called from this client
@@ -223,8 +223,8 @@ class ShopifySDK
223223
public function __construct($config = array())
224224
{
225225
if(!empty($config)) {
226-
ShopifySDK::$config = $config;
227-
ShopifySDK::setAdminUrl();
226+
$this->config = $config;
227+
$this->setAdminUrl();
228228
}
229229
}
230230

@@ -271,7 +271,7 @@ public function __call($resourceName, $arguments)
271271
$resourceID = !empty($arguments) ? $arguments[0] : null;
272272

273273
//Initiate the resource object
274-
$resource = new $resourceClassName($resourceID);
274+
$resource = new $resourceClassName( $this->config, $resourceID);
275275

276276
return $resource;
277277
}
@@ -283,15 +283,15 @@ public function __call($resourceName, $arguments)
283283
*
284284
* @return ShopifySDK
285285
*/
286-
public static function config($config)
286+
public function config($config)
287287
{
288288
foreach ($config as $key => $value) {
289-
self::$config[$key] = $value;
289+
$this->config[$key] = $value;
290290
}
291291

292292
//Re-set the admin url if shop url is changed
293293
if(isset($config['ShopUrl'])) {
294-
self::setAdminUrl();
294+
$this->setAdminUrl();
295295
}
296296

297297
//If want to keep more wait time than .5 seconds for each call
@@ -307,22 +307,22 @@ public static function config($config)
307307
*
308308
* @return string
309309
*/
310-
public static function setAdminUrl()
310+
public function setAdminUrl()
311311
{
312-
$shopUrl = self::$config['ShopUrl'];
312+
$shopUrl = $this->config['ShopUrl'];
313313

314314
//Remove https:// and trailing slash (if provided)
315315
$shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
316316

317-
if(isset(self::$config['ApiKey']) && isset(self::$config['Password'])) {
318-
$apiKey = self::$config['ApiKey'];
319-
$apiPassword = self::$config['Password'];
317+
if(isset($this->config['ApiKey']) && isset($this->config['Password'])) {
318+
$apiKey = $this->config['ApiKey'];
319+
$apiPassword = $this->config['Password'];
320320
$adminUrl = "https://$apiKey:$apiPassword@$shopUrl/admin/";
321321
} else {
322322
$adminUrl = "https://$shopUrl/admin/";
323323
}
324324

325-
self::$config['AdminUrl'] = $adminUrl;
325+
$this->config['AdminUrl'] = $adminUrl;
326326

327327
return $adminUrl;
328328
}
@@ -332,8 +332,8 @@ public static function setAdminUrl()
332332
*
333333
* @return string
334334
*/
335-
public static function getAdminUrl() {
336-
return self::$config['AdminUrl'];
335+
public function getAdminUrl() {
336+
return $this->config['AdminUrl'];
337337
}
338338

339339
/**

0 commit comments

Comments
 (0)