3 namespace Wallabag\CoreBundle\Helper
;
5 use GuzzleHttp\Cookie\FileCookieJar
as BaseFileCookieJar
;
6 use GuzzleHttp\Cookie\SetCookie
;
10 * Overidden Cookie behavior to:
11 * - fix multiple concurrent writes (see https://github.com/guzzle/guzzle/pull/1884)
12 * - ignore error when the cookie file is malformatted (resulting in clearing it).
14 class FileCookieJar
extends BaseFileCookieJar
17 * Saves the cookies to a file.
19 * @param string $filename File to save
21 * @throws \RuntimeException if the file cannot be found or created
23 public function save($filename)
26 foreach ($this as $cookie) {
27 if ($cookie->getExpires() && !$cookie->getDiscard()) {
28 $json[] = $cookie->toArray();
32 if (false === file_put_contents($filename, json_encode($json), LOCK_EX
)) {
33 // @codeCoverageIgnoreStart
34 throw new \
RuntimeException("Unable to save file {$filename}");
35 // @codeCoverageIgnoreEnd
40 * Load cookies from a JSON formatted file.
42 * Old cookies are kept unless overwritten by newly loaded ones.
44 * @param string $filename cookie file to load
46 * @throws \RuntimeException if the file cannot be loaded
48 public function load($filename)
50 $json = file_get_contents($filename);
51 if (false === $json) {
52 // @codeCoverageIgnoreStart
53 throw new \
RuntimeException("Unable to load file {$filename}");
54 // @codeCoverageIgnoreEnd
58 $data = Utils
::jsonDecode($json, true);
59 } catch (\InvalidArgumentException
$e) {
60 // cookie file is invalid, just ignore the exception and it'll reset the whole cookie file
64 if (\
is_array($data)) {
65 foreach (Utils
::jsonDecode($json, true) as $cookie) {
66 $this->setCookie(new SetCookie($cookie));
68 } elseif (\
strlen($data)) {
69 throw new \
RuntimeException("Invalid cookie file: {$filename}");