Skip to content

Commit

Permalink
Implement SessionInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 17, 2018
1 parent 00376d3 commit a0946c6
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 116 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* Set minimum requirements to [PHP 5.6.4](https://getgrav.org/blog/raising-php-requirements-2018)
* Updated Doctrine Collections to 1.4
* Updated Symfony Components to 3.4 (with compatibility mode to fall back to Symfony YAML 2.8)
* Added new `Grav\Framework\File\Formatter` classes for encoding/decoding YAML, Markdown, JSON, INI and PHP serialized strings
* Added `Grav\Framework\File\Formatter` classes for encoding/decoding YAML, Markdown, JSON, INI and PHP serialized strings
* Added `Grav\Framework\Session` class to replace `RocketTheme\Toolbox\Session\Session`
* Added `Grav\Common\Media` interfaces and trait; use those in `Page` and `Media` classes
* Added `Grav\Common\Page` interface to allow custom page types in the future
* Added `Grav\Framework\Session` class to replace `RocketTheme\Toolbox\Session\Session`
1. [](#improved)
* Improved session handling, allow all session configuration options in `system.session.options`
1. [](#bugfix)
Expand Down
31 changes: 31 additions & 0 deletions system/src/Grav/Common/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class Session extends \Grav\Framework\Session\Session
/** @var bool */
protected $autoStart = false;

/**
* @return \Grav\Framework\Session\Session
* @deprecated 1.5
*/
public static function instance()
{
return static::getInstance();
}

/**
* Initialize session.
*
Expand All @@ -38,6 +47,28 @@ public function setAutoStart($auto)
return $this;
}

/**
* Returns attributes.
*
* @return array Attributes
* @deprecated 1.5
*/
public function all()
{
return $this->getAll();
}

/**
* Checks if the session was started.
*
* @return Boolean
* @deprecated 1.5
*/
public function started()
{
return $this->isStarted();
}

/**
* Store something in session temporarily.
*
Expand Down
179 changes: 65 additions & 114 deletions system/src/Grav/Framework/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Class Session
* @package Grav\Framework\Session
*/
class Session implements \IteratorAggregate
class Session implements SessionInterface
{
/**
* @var bool
Expand All @@ -25,12 +25,9 @@ class Session implements \IteratorAggregate
protected static $instance;

/**
* Get current session instance.
*
* @return Session
* @throws \RuntimeException
* @inheritdoc
*/
public static function instance()
public static function getInstance()
{
if (null === self::$instance) {
throw new \RuntimeException("Session hasn't been initialized.", 500);
Expand All @@ -40,8 +37,7 @@ public static function instance()
}

/**
* @param array $options Array of session configuration options with their values
* @throws \RuntimeException
* @inheritdoc
*/
public function __construct(array $options = [])
{
Expand Down Expand Up @@ -79,11 +75,43 @@ public function __construct(array $options = [])
}

/**
* Sets session.* ini variables.
*
* @param array $options
*
* @see http://php.net/session.configuration
* @inheritdoc
*/
public function getId()
{
return session_id();
}

/**
* @inheritdoc
*/
public function setId($id)
{
session_id($id);

return $this;
}

/**
* @inheritdoc
*/
public function getName()
{
return session_name();
}

/**
* @inheritdoc
*/
public function setName($name)
{
session_name($name);

return $this;
}

/**
* @inheritdoc
*/
public function setOptions(array $options)
{
Expand Down Expand Up @@ -145,11 +173,7 @@ public function setOptions(array $options)
}

/**
* Starts the session storage
*
* @param bool $readonly
* @return $this
* @throws \RuntimeException
* @inheritdoc
*/
public function start($readonly = false)
{
Expand Down Expand Up @@ -185,58 +209,7 @@ public function start($readonly = false)
}

/**
* Get session ID
*
* @return string|null Session ID
*/
public function getId()
{
return session_id();
}

/**
* Set session Id
*
* @param string $id Session ID
*
* @return $this
*/
public function setId($id)
{
session_id($id);

return $this;
}


/**
* Get session name
*
* @return string|null
*/
public function getName()
{
return session_name();
}

/**
* Set session name
*
* @param string $name
*
* @return $this
*/
public function setName($name)
{
session_name($name);

return $this;
}

/**
* Invalidates the current session.
*
* @return $this
* @inheritdoc
*/
public function invalidate()
{
Expand All @@ -260,9 +233,7 @@ public function invalidate()
}

/**
* Force the session to be saved and closed
*
* @return $this
* @inheritdoc
*/
public function close()
{
Expand All @@ -276,79 +247,59 @@ public function close()
}

/**
* Checks if an attribute is defined.
*
* @param string $name The attribute name
*
* @return bool True if the attribute is defined, false otherwise
* @inheritdoc
*/
public function __isset($name)
public function getAll()
{
return isset($_SESSION[$name]);
return $_SESSION;
}

/**
* Returns an attribute.
*
* @param string $name The attribute name
*
* @return mixed
* @inheritdoc
*/
public function __get($name)
public function getIterator()
{
return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
return new \ArrayIterator($_SESSION);
}

/**
* Sets an attribute.
*
* @param string $name
* @param mixed $value
* @inheritdoc
*/
public function __set($name, $value)
public function isStarted()
{
$_SESSION[$name] = $value;
return $this->started;
}

/**
* Removes an attribute.
*
* @param string $name
* @inheritdoc
*/
public function __unset($name)
public function __isset($name)
{
unset($_SESSION[$name]);
return isset($_SESSION[$name]);
}

/**
* Returns attributes.
*
* @return array Attributes
* @inheritdoc
*/
public function all()
public function __get($name)
{
return $_SESSION;
return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
}


/**
* Retrieve an external iterator
*
* @return \ArrayIterator Return an ArrayIterator of $_SESSION
* @inheritdoc
*/
public function getIterator()
public function __set($name, $value)
{
return new \ArrayIterator($_SESSION);
$_SESSION[$name] = $value;
}

/**
* Checks if the session was started.
*
* @return Boolean
* @inheritdoc
*/
public function started()
public function __unset($name)
{
return $this->started;
unset($_SESSION[$name]);
}

/**
Expand Down
Loading

0 comments on commit a0946c6

Please sign in to comment.