PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.1
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Cookie / FileCookieJar.php

FileCookieJar.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.1, at vendor_prefixed/guzzlehttp/guzzle/src/Cookie/FileCookieJar.php

93 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Cookie;
4
5 use YoastSEO_Vendor\GuzzleHttp\Utils;
6 /**
7 * Persists non-session cookies using a JSON formatted file
8 */
9 class FileCookieJar extends \YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJar
10 {
11 /**
12 * @var string filename
13 */
14 private $filename;
15 /**
16 * @var bool Control whether to persist session cookies or not.
17 */
18 private $storeSessionCookies;
19 /**
20 * Create a new FileCookieJar object
21 *
22 * @param string $cookieFile File to store the cookie data
23 * @param bool $storeSessionCookies Set to true to store session cookies
24 * in the cookie jar.
25 *
26 * @throws \RuntimeException if the file cannot be found or created
27 */
28 public function __construct(string $cookieFile, bool $storeSessionCookies = \false)
29 {
30 parent::__construct();
31 $this->filename = $cookieFile;
32 $this->storeSessionCookies = $storeSessionCookies;
33 if (\file_exists($cookieFile)) {
34 $this->load($cookieFile);
35 }
36 }
37 /**
38 * Saves the file when shutting down
39 */
40 public function __destruct()
41 {
42 $this->save($this->filename);
43 }
44 /**
45 * Saves the cookies to a file.
46 *
47 * @param string $filename File to save
48 *
49 * @throws \RuntimeException if the file cannot be found or created
50 */
51 public function save(string $filename) : void
52 {
53 $json = [];
54 /** @var SetCookie $cookie */
55 foreach ($this as $cookie) {
56 if (\YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
57 $json[] = $cookie->toArray();
58 }
59 }
60 $jsonStr = \YoastSEO_Vendor\GuzzleHttp\Utils::jsonEncode($json);
61 if (\false === \file_put_contents($filename, $jsonStr, \LOCK_EX)) {
62 throw new \RuntimeException("Unable to save file {$filename}");
63 }
64 }
65 /**
66 * Load cookies from a JSON formatted file.
67 *
68 * Old cookies are kept unless overwritten by newly loaded ones.
69 *
70 * @param string $filename Cookie file to load.
71 *
72 * @throws \RuntimeException if the file cannot be loaded.
73 */
74 public function load(string $filename) : void
75 {
76 $json = \file_get_contents($filename);
77 if (\false === $json) {
78 throw new \RuntimeException("Unable to load file {$filename}");
79 }
80 if ($json === '') {
81 return;
82 }
83 $data = \YoastSEO_Vendor\GuzzleHttp\Utils::jsonDecode($json, \true);
84 if (\is_array($data)) {
85 foreach ($data as $cookie) {
86 $this->setCookie(new \YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie($cookie));
87 }
88 } elseif (\is_scalar($data) && !empty($data)) {
89 throw new \RuntimeException("Invalid cookie file: {$filename}");
90 }
91 }
92 }
93