composer package updates
[openemr.git] / vendor / symfony / http-foundation / Session / Storage / MetadataBag.php
blob6f59af486981ebb1b2cb9c2687cc3ef2d40250d4
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage;
14 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
16 /**
17 * Metadata container.
19 * Adds metadata to the session.
21 * @author Drak <drak@zikula.org>
23 class MetadataBag implements SessionBagInterface
25 const CREATED = 'c';
26 const UPDATED = 'u';
27 const LIFETIME = 'l';
29 /**
30 * @var string
32 private $name = '__metadata';
34 /**
35 * @var string
37 private $storageKey;
39 /**
40 * @var array
42 protected $meta = array(self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0);
44 /**
45 * Unix timestamp.
47 * @var int
49 private $lastUsed;
51 /**
52 * @var int
54 private $updateThreshold;
56 /**
57 * @param string $storageKey The key used to store bag in the session
58 * @param int $updateThreshold The time to wait between two UPDATED updates
60 public function __construct($storageKey = '_sf2_meta', $updateThreshold = 0)
62 $this->storageKey = $storageKey;
63 $this->updateThreshold = $updateThreshold;
66 /**
67 * {@inheritdoc}
69 public function initialize(array &$array)
71 $this->meta = &$array;
73 if (isset($array[self::CREATED])) {
74 $this->lastUsed = $this->meta[self::UPDATED];
76 $timeStamp = time();
77 if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
78 $this->meta[self::UPDATED] = $timeStamp;
80 } else {
81 $this->stampCreated();
85 /**
86 * Gets the lifetime that the session cookie was set with.
88 * @return int
90 public function getLifetime()
92 return $this->meta[self::LIFETIME];
95 /**
96 * Stamps a new session's metadata.
98 * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
99 * will leave the system settings unchanged, 0 sets the cookie
100 * to expire with browser session. Time is in seconds, and is
101 * not a Unix timestamp.
103 public function stampNew($lifetime = null)
105 $this->stampCreated($lifetime);
109 * {@inheritdoc}
111 public function getStorageKey()
113 return $this->storageKey;
117 * Gets the created timestamp metadata.
119 * @return int Unix timestamp
121 public function getCreated()
123 return $this->meta[self::CREATED];
127 * Gets the last used metadata.
129 * @return int Unix timestamp
131 public function getLastUsed()
133 return $this->lastUsed;
137 * {@inheritdoc}
139 public function clear()
141 // nothing to do
145 * {@inheritdoc}
147 public function getName()
149 return $this->name;
153 * Sets name.
155 * @param string $name
157 public function setName($name)
159 $this->name = $name;
162 private function stampCreated($lifetime = null)
164 $timeStamp = time();
165 $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
166 $this->meta[self::LIFETIME] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime;