driver.php 1.49 KB
Newer Older
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.
Taylor Otwell committed
7
	 *
8
	 * If no session is found for the ID, null will be returned.
9
	 *
10 11 12
	 * @param  string  $id
	 * @return array
	 */
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 22
	 * @return void
	 */
23
	abstract public function save($session, $config, $exists);
Taylor Otwell committed
24 25

	/**
26
	 * Delete a session from storage by a given ID.
27
	 *
28
	 * @param  string  $id
29 30
	 * @return void
	 */
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	abstract public function delete($id);

	/**
	 * Insert a fresh session and return the payload array.
	 *
	 * @return array
	 */
	public function fresh()
	{
		// We will simply generate an empty session payload array, using an ID
		// that is not currently assigned to any existing session within the
		// application and return it to the driver.
		return array('id' => $this->id(), 'data' => array(
			':new:' => array(),
			':old:' => array(),
		));
	}

	/**
	 * Get a new session ID that isn't assigned to any current session.
	 *
	 * @return string
	 */
	public function id()
	{
		$session = array();

		// We'll containue generating random IDs until we find an ID that is
		// not currently assigned to a session. This is almost definitely
		// going to happen on the first iteration.
		do {

			$session = $this->load($id = Str::random(40));			

		} while ( ! is_null($session));

		return $id;
	}
69

70
}