1: <?php namespace Laravel\Session\Drivers; use Laravel\Config, Laravel\Str;
2:
3: abstract class Driver {
4:
5: /**
6: * Load a session from storage by a given ID.
7: *
8: * If no session is found for the ID, null will be returned.
9: *
10: * @param string $id
11: * @return array
12: */
13: abstract public function load($id);
14:
15: /**
16: * Save a given session to storage.
17: *
18: * @param array $session
19: * @param array $config
20: * @param bool $exists
21: * @return void
22: */
23: abstract public function save($session, $config, $exists);
24:
25: /**
26: * Delete a session from storage by a given ID.
27: *
28: * @param string $id
29: * @return void
30: */
31: abstract public function delete($id);
32:
33: /**
34: * Create a fresh session array with a unique ID.
35: *
36: * @return array
37: */
38: public function fresh()
39: {
40: // We will simply generate an empty session payload array, using an ID
41: // that is not currently assigned to any existing session within the
42: // application and return it to the driver.
43: return array('id' => $this->id(), 'data' => array(
44: ':new:' => array(),
45: ':old:' => array(),
46: ));
47: }
48:
49: /**
50: * Get a new session ID that isn't assigned to any current session.
51: *
52: * @return string
53: */
54: public function id()
55: {
56: $session = array();
57:
58: // If the driver is an instance of the Cookie driver, we are able to
59: // just return any string since the Cookie driver has no real idea
60: // of a server side persisted session with an ID.
61: if ($this instanceof Cookie)
62: {
63: return Str::random(40);
64: }
65:
66: // We'll continue generating random IDs until we find an ID that is
67: // not currently assigned to a session. This is almost definitely
68: // going to happen on the first iteration.
69: do {
70:
71: $session = $this->load($id = Str::random(40));
72:
73: } while ( ! is_null($session));
74:
75: return $id;
76: }
77:
78: }