Source of file Session.php

Size: 2,507 Bytes - Last Modified: 2015-12-22T09:42:40-05:00

../src/Session.php

12345678910111213141516171819202122232425262728293031323334353637383940414243
Covered by 1 test(s):
  • MvcLite\SessionTest::testInit with data set "simple test"
44
Covered by 1 test(s):
  • MvcLite\SessionTest::testInit with data set "simple test"
45
Covered by 1 test(s):
  • MvcLite\SessionTest::testInit with data set "simple test"
464748495051525354
Covered by 2 test(s):
  • MvcLite\SessionTest::testGetParams with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
5556575859606162636465
Covered by 2 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testSetParam with data set "simple test"
666768697071727374757677
Covered by 3 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testSetParam with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
78
Covered by 3 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testSetParam with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
798081
Covered by 3 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testSetParam with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
828384858687888990919293
Covered by 2 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
94
Covered by 2 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
95
Covered by 2 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
969798
Covered by 2 test(s):
  • MvcLite\SessionTest::testSetParams with data set "simple test"
  • MvcLite\SessionTest::testDestroy with data set "simple test"
99100101102103104105106
Covered by 1 test(s):
  • MvcLite\SessionTest::testDestroy with data set "simple test"
107108
Covered by 1 test(s):
  • MvcLite\SessionTest::testDestroy with data set "simple test"
109110
Covered by 1 test(s):
  • MvcLite\SessionTest::testDestroy with data set "simple test"
111
Covered by 1 test(s):
  • MvcLite\SessionTest::testDestroy with data set "simple test"
112113114115116117118
Covered by 1 test(s):
  • MvcLite\SessionTest::testDestroy with data set "simple test"
119
Covered by 1 test(s):
  • MvcLite\SessionTest::testDestroy with data set "simple test"
120121
<?php
/**
 * Session Handler
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Request
 * @since       File available since release 1.0.1
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace MvcLite;

use MvcLite\Traits\Singleton as SingletonTrait;

/**
 * Session handling class
 *
 * This class handles all necessary interaction with session information
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Request
 * @since       Class available since release 1.0.1
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class Session extends ObjectAbstract
{
    use SingletonTrait;

    /**
     * property to store the data of the session
     *
     * @var array $_data
     */
    protected $data = [];

    /**
     * method to start the session.
     */
    public function init($data = [])
    {
        session_start();
        $this->data = $data;
    }

    /**
     * Method to retrieve the data param
     *
     * @return array
     */
    public function getParams()
    {
        return $this->data;
    }

    /**
     * Method to return a single parameter by name
     *
     * @param string $param
     * @return unknown_type
     */
    public function getParam($param)
    {
        return $this->data[$param];
    }

    /**
     * Method to set a single parameter value
     *
     * @param string $param
     * @param string $value
     * @return MvcLite\Session $this for object-chaining.
     */
    public function setParam($param, $value = '')
    {
        $this->data[$param] = $value;
        $_SESSION = $this->data;

        // return $this for object-chaining.
        return $this;
    }

    /**
     * Utility method to allow for the setting of multiple parameters
     *
     * @param array $params
     * @return MvcLite\Session $this for object-chaining.
     */
    public function setParams($params = [])
    {
        // iterate over the params, setting them using the setParam method
        foreach ($params as $param => $value) {
            $this->setParam($param, $value);
        }

        // return $this for object-chaining.
        return $this;
    }

    /**
     * Method to destroy the session
     */
    public function destroy()
    {
        $this->data = null;

        session_destroy();

        $this->__destruct();
    }

    /**
     * Implementation of the magic method __destruct, to save state
     */
    public function __destruct()
    {
        $_SESSION = $this->data;
    }
}