| 1 |
<?php |
| 2 |
|
| 3 |
namespace RabbitLoader\SDK; |
| 4 |
|
| 5 |
/** |
| 6 |
* Polyfills |
| 7 |
*/ |
| 8 |
if (!defined('JSON_INVALID_UTF8_IGNORE')) { |
| 9 |
define('JSON_INVALID_UTF8_IGNORE', 0); //@since PHP 7.2 |
| 10 |
} |
| 11 |
|
| 12 |
if (!function_exists('str_contains')) { |
| 13 |
function str_contains($haystack, $needle) |
| 14 |
{ |
| 15 |
return $needle !== '' && mb_strpos($haystack, $needle) !== false; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
class RabbitLoader |
| 20 |
{ |
| 21 |
private $storageDirectory = ''; |
| 22 |
private $debug = false; |
| 23 |
private $request; |
| 24 |
|
| 25 |
public function __construct($licenseKey, $storageDirectory = '/tmp/rabbitloader') |
| 26 |
{ |
| 27 |
$this->storageDirectory = $storageDirectory; |
| 28 |
$this->request = new Request($licenseKey, $this->storageDirectory); |
| 29 |
Exc::setFile($this->storageDirectory, false); |
| 30 |
} |
| 31 |
|
| 32 |
public function setDebug($debug) |
| 33 |
{ |
| 34 |
$this->debug = $debug; |
| 35 |
$this->request->setDebug($this->debug); |
| 36 |
Exc::setFile($this->storageDirectory, $this->debug); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Pass array of param names to ignore for caching. The cache will be served by ignoring the parameters |
| 41 |
*/ |
| 42 |
public function ignoreParams($paramNames) |
| 43 |
{ |
| 44 |
$this->request->ignoreParams($paramNames); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Pass array of cookie names. If any cookie is found, the cache will be skipped |
| 49 |
*/ |
| 50 |
public function skipForCookies($cookieNames) |
| 51 |
{ |
| 52 |
$this->request->skipForCookies($cookieNames); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Pass array of path patterns. If any path match is found, the cache will be skipped |
| 57 |
*/ |
| 58 |
public function skipForPaths($pathPatterns) |
| 59 |
{ |
| 60 |
$this->request->skipForPaths($pathPatterns); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* To skip optimization and caching of current request. For example, later in a page you discover the page does not exist or it's a temporary search results page that should not be optimized, call this method. |
| 65 |
* Optionally you can pass a reason that will reflect in page headers |
| 66 |
*/ |
| 67 |
public function ignoreRequest($reason = '') |
| 68 |
{ |
| 69 |
$this->request->ignoreRequest($reason); |
| 70 |
} |
| 71 |
|
| 72 |
public function process() |
| 73 |
{ |
| 74 |
$this->request->process(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* When a page is modified, call this. It is responsibility of the caller to call this multiple times if the changes impacts other URLs, for example, change on a page may trigger home page content refresh as well. |
| 79 |
* @param string $url - The page for which content os changed |
| 80 |
* @param string $variant - The variant (same as used by setVariant()) |
| 81 |
*/ |
| 82 |
public function onContentChange($url, $variant = []) |
| 83 |
{ |
| 84 |
$cacheFile = new Cache($url, $this->storageDirectory); |
| 85 |
$cacheFile->setDebug($this->debug); |
| 86 |
$cacheFile->setVariant($variant); |
| 87 |
return $cacheFile->invalidate(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Purge external systems when RabbitLoader updates a page |
| 92 |
* Example - $cb = function($url){//do purge} |
| 93 |
*/ |
| 94 |
public function registerPurgeCallback($cb) |
| 95 |
{ |
| 96 |
$this->request->registerPurgeCallback($cb); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Delete cached file if exists for a given URL |
| 101 |
* @return int deleted cache count |
| 102 |
*/ |
| 103 |
public function delete($url) |
| 104 |
{ |
| 105 |
$cacheFile = new Cache($url, $this->storageDirectory); |
| 106 |
$cacheFile->setDebug($this->debug); |
| 107 |
return $cacheFile->delete(Cache::TTL_LONG); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Delete all cached files and returns the count |
| 112 |
* @return int deleted cache count |
| 113 |
*/ |
| 114 |
public function deleteAll() |
| 115 |
{ |
| 116 |
$cacheFile = new Cache('', $this->storageDirectory); |
| 117 |
$cacheFile->setDebug($this->debug); |
| 118 |
return $cacheFile->deleteAll(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* setVariant sets additional keys to the cached file. For example, if the website shows different content based on country code, country code can be one of the variant to set here. |
| 123 |
* The more keys is set here, cache hit ratio will reduce. |
| 124 |
* If a page has two variants, based on currency and viewer device, this can be set - setVariant(["currency"=>"USD", "screen"=>"MOBILE"]) or setVariant(["currency"=>"GBP", "screen"=>"DESKTOP"]) |
| 125 |
* setVariant must be called before process() |
| 126 |
*/ |
| 127 |
public function setVariant($variant) |
| 128 |
{ |
| 129 |
$this->request->setVariant($variant); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns the number of URLs for which cache exists |
| 134 |
*/ |
| 135 |
public function getCacheCount() |
| 136 |
{ |
| 137 |
$cacheFile = new Cache('', $this->storageDirectory); |
| 138 |
$cacheFile->setDebug($this->debug); |
| 139 |
return $cacheFile->getCacheCount(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns if the page was warmup |
| 144 |
*/ |
| 145 |
public function isWarmUp() |
| 146 |
{ |
| 147 |
return $this->request->isWarmUp(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Activate ME mode |
| 152 |
*/ |
| 153 |
public function setMeMode() |
| 154 |
{ |
| 155 |
return $this->request->setMeMode(); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Set exc catch |
| 160 |
*/ |
| 161 |
public function excCatch($e, $data = [], $limit = 8) |
| 162 |
{ |
| 163 |
Exc:: catch($e, $data, $limit); |
| 164 |
} |
| 165 |
|
| 166 |
public function setPlatform($data) |
| 167 |
{ |
| 168 |
return $this->request->setPlatform($data); |
| 169 |
} |
| 170 |
} |
| 171 |
|