1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
13
14@trigger_error(sprintf('The %s class is deprecated since Symfony 3.4 and will be removed in 4.0. Implement `SessionUpdateTimestampHandlerInterface` or extend `AbstractSessionHandler` instead.', WriteCheckSessionHandler::class), E_USER_DEPRECATED);
15
16/**
17 * Wraps another SessionHandlerInterface to only write the session when it has been modified.
18 *
19 * @author Adrien Brault <adrien.brault@gmail.com>
20 *
21 * @deprecated since version 3.4, to be removed in 4.0. Implement `SessionUpdateTimestampHandlerInterface` or extend `AbstractSessionHandler` instead.
22 */
23class WriteCheckSessionHandler implements \SessionHandlerInterface
24{
25    private $wrappedSessionHandler;
26
27    /**
28     * @var array sessionId => session
29     */
30    private $readSessions;
31
32    public function __construct(\SessionHandlerInterface $wrappedSessionHandler)
33    {
34        $this->wrappedSessionHandler = $wrappedSessionHandler;
35    }
36
37    /**
38     * {@inheritdoc}
39     */
40    public function close()
41    {
42        return $this->wrappedSessionHandler->close();
43    }
44
45    /**
46     * {@inheritdoc}
47     */
48    public function destroy($sessionId)
49    {
50        return $this->wrappedSessionHandler->destroy($sessionId);
51    }
52
53    /**
54     * {@inheritdoc}
55     */
56    public function gc($maxlifetime)
57    {
58        return $this->wrappedSessionHandler->gc($maxlifetime);
59    }
60
61    /**
62     * {@inheritdoc}
63     */
64    public function open($savePath, $sessionName)
65    {
66        return $this->wrappedSessionHandler->open($savePath, $sessionName);
67    }
68
69    /**
70     * {@inheritdoc}
71     */
72    public function read($sessionId)
73    {
74        $session = $this->wrappedSessionHandler->read($sessionId);
75
76        $this->readSessions[$sessionId] = $session;
77
78        return $session;
79    }
80
81    /**
82     * {@inheritdoc}
83     */
84    public function write($sessionId, $data)
85    {
86        if (isset($this->readSessions[$sessionId]) && $data === $this->readSessions[$sessionId]) {
87            return true;
88        }
89
90        return $this->wrappedSessionHandler->write($sessionId, $data);
91    }
92}
93