forked from LiveCarta/PayPal-PHP-SDK
Merge branch 'angelcoding-customcachepath' into 1.0.0-beta
This commit is contained in:
12
README.md
12
README.md
@@ -144,16 +144,22 @@ Please visit our [sample sdk_config.ini](https://github.com/paypal/PayPal-PHP-SD
|
|||||||
;Caching Configuration
|
;Caching Configuration
|
||||||
[cache]
|
[cache]
|
||||||
; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the
|
; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the
|
||||||
; server into a file provided in constant $CACHE_PATH in PayPal/Cache/AuthorizationCache.
|
; server into a file provided by the cache.FileName option or by using
|
||||||
|
; the constant $CACHE_PATH value in PayPal/Cache/AuthorizationCache if the option is omitted/empty.
|
||||||
; If the value is set to 'true', it would try to create a file and store the information.
|
; If the value is set to 'true', it would try to create a file and store the information.
|
||||||
; For any other value, it would disable it
|
; For any other value, it would disable it
|
||||||
; Please note, this is a very good performance improvement, and we would encourage you to
|
; Please note, this is a very good performance improvement, and we would encourage you to
|
||||||
; set this up properly to reduce the number of calls, to almost 50% on normal use cases
|
; set this up properly to reduce the number of calls, to almost 50% on normal use cases
|
||||||
; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on
|
; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on
|
||||||
; your hosting server
|
; your hosting server or whichever custom directory you choose
|
||||||
cache.enabled=true
|
cache.enabled=true
|
||||||
|
; When using a relative path, the cache file is created
|
||||||
|
; relative to the .php file that is the entry point
|
||||||
|
; for this request. You can also provide an absolute
|
||||||
|
; path here
|
||||||
|
cache.FileName=../auth.cache
|
||||||
```
|
```
|
||||||
##### PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on your hosting server
|
##### PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK (or your custom directory) on your hosting server
|
||||||
|
|
||||||
### API Model Constructor
|
### API Model Constructor
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ abstract class AuthorizationCache
|
|||||||
if (!self::isEnabled($config)) { return null; }
|
if (!self::isEnabled($config)) { return null; }
|
||||||
|
|
||||||
$tokens = null;
|
$tokens = null;
|
||||||
if (file_exists(__DIR__ . self::$CACHE_PATH)) {
|
$cachePath = self::cachePath($config);
|
||||||
|
if (file_exists($cachePath)) {
|
||||||
// Read from the file
|
// Read from the file
|
||||||
$cachedToken = file_get_contents(__DIR__ . self::$CACHE_PATH);
|
$cachedToken = file_get_contents($cachePath);
|
||||||
if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
|
if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
|
||||||
$tokens = json_decode($cachedToken, true);
|
$tokens = json_decode($cachedToken, true);
|
||||||
if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
|
if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
|
||||||
@@ -54,8 +55,9 @@ abstract class AuthorizationCache
|
|||||||
// Return if not enabled
|
// Return if not enabled
|
||||||
if (!self::isEnabled($config)) { return; }
|
if (!self::isEnabled($config)) { return; }
|
||||||
|
|
||||||
if (!is_dir(dirname(__DIR__ . self::$CACHE_PATH))) {
|
$cachePath = self::cachePath($config);
|
||||||
if (mkdir(dirname(__DIR__ . self::$CACHE_PATH), 0755, true) == false) {
|
if (!is_dir(dirname($cachePath))) {
|
||||||
|
if (mkdir(dirname($cachePath), 0755, true) == false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,7 +73,7 @@ abstract class AuthorizationCache
|
|||||||
'tokenExpiresIn' => $tokenExpiresIn
|
'tokenExpiresIn' => $tokenExpiresIn
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
file_put_contents(__DIR__ . self::$CACHE_PATH, json_encode($tokens));
|
file_put_contents($cachePath, json_encode($tokens));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,13 +83,35 @@ abstract class AuthorizationCache
|
|||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function isEnabled($config)
|
public static function isEnabled($config)
|
||||||
|
{
|
||||||
|
$value = self::getConfigValue('cache.enabled', $config);
|
||||||
|
return empty($value) ? false : ((trim($value) == true || trim($value) == 'true') ? true : false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the cache file path
|
||||||
|
*
|
||||||
|
* @param $config
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function cachePath($config)
|
||||||
|
{
|
||||||
|
$cachePath = self::getConfigValue('cache.FileName', $config);
|
||||||
|
return empty($cachePath) ? __DIR__ . self::$CACHE_PATH : $cachePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Value of the key if found in given config, or from PayPal Config Manager
|
||||||
|
* Returns null if not found
|
||||||
|
*
|
||||||
|
* @param $key
|
||||||
|
* @param $config
|
||||||
|
* @return null|string
|
||||||
|
*/
|
||||||
|
private static function getConfigValue($key, $config)
|
||||||
{
|
{
|
||||||
$config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap();
|
$config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap();
|
||||||
if (array_key_exists("cache.enabled", $config)) {
|
return (array_key_exists($key, $config)) ? trim($config[$key]) : null;
|
||||||
$value = $config['cache.enabled'];
|
|
||||||
return (trim($value) == true || trim($value) == 'true') ? true : false;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -49,11 +49,17 @@ validation.level=log
|
|||||||
;Caching Configuration
|
;Caching Configuration
|
||||||
[cache]
|
[cache]
|
||||||
; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the
|
; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the
|
||||||
; server into a file provided in constant $CACHE_PATH in PayPal/Cache/AuthorizationCache.
|
; server into a file provided by the cache.FileName option or by using
|
||||||
|
; the constant $CACHE_PATH value in PayPal/Cache/AuthorizationCache if the option is omitted/empty.
|
||||||
; If the value is set to 'true', it would try to create a file and store the information.
|
; If the value is set to 'true', it would try to create a file and store the information.
|
||||||
; For any other value, it would disable it
|
; For any other value, it would disable it
|
||||||
; Please note, this is a very good performance improvement, and we would encourage you to
|
; Please note, this is a very good performance improvement, and we would encourage you to
|
||||||
; set this up properly to reduce the number of calls, to almost 50% on normal use cases
|
; set this up properly to reduce the number of calls, to almost 50% on normal use cases
|
||||||
; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on
|
; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on
|
||||||
; your hosting server
|
; your hosting server or whichever custom directory you choose
|
||||||
cache.enabled=true
|
cache.enabled=true
|
||||||
|
; When using a relative path, the cache file is created
|
||||||
|
; relative to the .php file that is the entry point
|
||||||
|
; for this request. You can also provide an absolute
|
||||||
|
; path here
|
||||||
|
cache.FileName=../auth.cache
|
||||||
|
|||||||
68
tests/PayPal/Test/Cache/AuthorizationCacheTest.php
Normal file
68
tests/PayPal/Test/Cache/AuthorizationCacheTest.php
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PayPal\Test\Cache;
|
||||||
|
use PayPal\Cache\AuthorizationCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for AuthorizationCacheTest.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the fixture, for example, opens a network connection.
|
||||||
|
* This method is called before a test is executed.
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tears down the fixture, for example, closes a network connection.
|
||||||
|
* This method is called after a test is executed.
|
||||||
|
*/
|
||||||
|
protected function tearDown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function EnabledProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(array('cache.enabled' => 'true'), true),
|
||||||
|
array(array('cache.enabled' => true), true),
|
||||||
|
array(array(), false),
|
||||||
|
array(null, false)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function CachePathProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(array('cache.FileName' => 'temp.cache'), 'temp.cache'),
|
||||||
|
array(array(), 'auth.cache'),
|
||||||
|
array(null, 'auth.cache')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @dataProvider EnabledProvider
|
||||||
|
*/
|
||||||
|
public function testIsEnabled($config, $expected)
|
||||||
|
{
|
||||||
|
$result = AuthorizationCache::isEnabled($config);
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider CachePathProvider
|
||||||
|
*/
|
||||||
|
public function testCachePath($config, $expected)
|
||||||
|
{
|
||||||
|
$result = AuthorizationCache::cachePath($config);
|
||||||
|
$this->assertContains($expected, $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user