Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 1... Línea 1...
1
<?php
1
<?php
Línea 2... Línea 2...
2
 
2
 
Línea 3... Línea 3...
3
namespace Kevinrob\GuzzleCache;
3
namespace Kevinrob\GuzzleCache;
-
 
4
 
4
 
5
use GuzzleHttp\Psr7\PumpStream;
5
use GuzzleHttp\Psr7\PumpStream;
6
use Psr\Http\Message\MessageInterface;
Línea 6... Línea 7...
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ResponseInterface;
8
 
9
 
9
class CacheEntry
10
class CacheEntry implements \Serializable
10
{
11
{
11
    /**
12
    /**
Línea 54... Línea 55...
54
     */
55
     */
55
    public function __construct(
56
    public function __construct(
56
        RequestInterface $request,
57
        RequestInterface $request,
57
        ResponseInterface $response,
58
        ResponseInterface $response,
58
        \DateTime $staleAt,
59
        \DateTime $staleAt,
59
        \DateTime $staleIfErrorTo = null,
60
        ?\DateTime $staleIfErrorTo = null,
60
        \DateTime $staleWhileRevalidateTo = null
61
        ?\DateTime $staleWhileRevalidateTo = null
61
    ) {
62
    ) {
62
        $this->dateCreated = new \DateTime();
63
        $this->dateCreated = new \DateTime();
Línea 63... Línea 64...
63
 
64
 
64
        $this->request = $request;
65
        $this->request = $request;
Línea 254... Línea 255...
254
    public function getAge()
255
    public function getAge()
255
    {
256
    {
256
        return time() - $this->dateCreated->getTimestamp();
257
        return time() - $this->dateCreated->getTimestamp();
257
    }
258
    }
Línea 258... Línea 259...
258
 
259
 
259
    public function __sleep()
260
    public function __serialize(): array
-
 
261
    {
-
 
262
        return [
260
    {
263
            'request' => self::toSerializeableMessage($this->request),
261
        // Stream/Resource can't be serialized... So we copy the content into an implementation of `Psr\Http\Message\StreamInterface`
264
            'response' => $this->response !== null ? self::toSerializeableMessage($this->response) : null,
262
        if ($this->response !== null) {
265
            'staleAt' => $this->staleAt,
263
            $responseBody = (string)$this->response->getBody();
266
            'staleIfErrorTo' => $this->staleIfErrorTo,
264
            $this->response = $this->response->withBody(
267
            'staleWhileRevalidateTo' => $this->staleWhileRevalidateTo,
265
                new PumpStream(
268
            'dateCreated' => $this->dateCreated,
266
                    new BodyStore($responseBody),
269
            'timestampStale' => $this->timestampStale,
-
 
270
        ];
-
 
271
    }
267
                    [
272
 
-
 
273
    public function __unserialize(array $data): void
268
                        'size' => mb_strlen($responseBody),
274
    {
269
                    ]
275
        $prefix = '';
-
 
276
        if (isset($data["\0*\0request"])) {
270
                )
277
            // We are unserializing a cache entry which was serialized with a version < 4.1.1
271
            );
278
            $prefix = "\0*\0";
-
 
279
        }
-
 
280
        $this->request = self::restoreStreamBody($data[$prefix.'request']);
-
 
281
        $this->response = $data[$prefix.'response'] !== null ? self::restoreStreamBody($data[$prefix.'response']) : null;
-
 
282
        $this->staleAt = $data[$prefix.'staleAt'];
-
 
283
        $this->staleIfErrorTo = $data[$prefix.'staleIfErrorTo'];
-
 
284
        $this->staleWhileRevalidateTo = $data[$prefix.'staleWhileRevalidateTo'];
-
 
285
        $this->dateCreated = $data[$prefix.'dateCreated'];
-
 
286
        $this->timestampStale = $data[$prefix.'timestampStale'];
Línea -... Línea 287...
-
 
287
    }
-
 
288
 
-
 
289
    /**
-
 
290
     * Stream/Resource can't be serialized... So we copy the content into an implementation of `Psr\Http\Message\StreamInterface`
-
 
291
     *
-
 
292
     * @template T of MessageInterface
-
 
293
     *
-
 
294
     * @param T $message
-
 
295
     * @return T
-
 
296
     */
272
        }
297
    private static function toSerializeableMessage(MessageInterface $message): MessageInterface
-
 
298
    {
273
 
299
        $bodyString = (string)$message->getBody();
274
        $requestBody = (string)$this->request->getBody();
300
 
275
        $this->request = $this->request->withBody(
301
        return $message->withBody(
276
            new PumpStream(
302
            new PumpStream(
277
                new BodyStore($requestBody),
303
                new BodyStore($bodyString),
278
                [
304
                [
279
                    'size' => mb_strlen($requestBody)
305
                    'size' => mb_strlen($bodyString),
280
                ]
306
                ]
-
 
307
            )
Línea -... Línea 308...
-
 
308
        );
-
 
309
    }
-
 
310
 
-
 
311
    /**
-
 
312
     * @template T of MessageInterface
-
 
313
     *
-
 
314
     * @param T $message
-
 
315
     * @return T
281
            )
316
     */
-
 
317
    private static function restoreStreamBody(MessageInterface $message): MessageInterface
-
 
318
    {
282
        );
319
        return $message->withBody(
Línea 283... Línea 320...
283
 
320
            \GuzzleHttp\Psr7\Utils::streamFor((string) $message->getBody())
284
        return array_keys(get_object_vars($this));
321
        );
285
    }
-
 
286
 
-
 
287
    public function __wakeup()
-
 
288
    {
-
 
289
        // We re-create the stream of the response
-
 
290
        if ($this->response !== null) {
-
 
291
            $this->response = $this->response
-
 
292
                ->withBody(
322
    }
293
                    \GuzzleHttp\Psr7\Utils::streamFor((string) $this->response->getBody())
-
 
294
                );
-
 
295
        }
-
 
296
        $this->request = $this->request
323
 
Línea -... Línea 324...
-
 
324
    public function serialize()
-
 
325
    {
-
 
326
        return serialize($this->__serialize());
-
 
327
    }
297
            ->withBody(
328