<commit_before><?php

namespace mii\web;


class Session
{

    /**
     * @var  array  session instances
     */
    public static $_instance = NULL;
    /**
     * @var  string  cookie name
     */
    protected $_name = 'session';
    /**
     * @var  int  cookie lifetime
     */
    protected $_lifetime = 0;
    /**
     * @var  bool  encrypt session data?
     */
    protected $_encrypted = false;
    /**
     * @var  array  session data
     */
    protected $_data = [];
    /**
     * @var  bool  session destroyed?
     */
    protected $_destroyed = false;

    /**
     * Overloads the name, lifetime, and encrypted session settings.
     *
     * [!!] Sessions can only be created using the [Session::instance] method.
     *
     * @param   array $config configuration
     * @param   string $id session id
     * @return  void
     * @uses    Session::read
     */
    public function __construct(array $config = NULL, $id = NULL)
    {
        if (isset($config['name'])) {
            // Cookie name to store the session id in
            $this->_name = (string)$config['name'];
        }

        if (isset($config['lifetime'])) {
            // Cookie lifetime
            $this->_lifetime = (int)$config['lifetime'];
        }

        if (isset($config['encrypted'])) {
            if ($config['encrypted'] === true) {
                // Use the default Encrypt instance
                $config['encrypted'] = 'default';
            }

            // Enable or disable encryption of data
            $this->_encrypted = $config['encrypted'];
        }

        // Load the session
        //$this->read($id);
    }

    /**
     * Creates a singleton session of the given type. Some session types
     * (native, database) also support restarting a session by passing a
     * session id as the second parameter.
     *
     *     $session = Session::instance();
     *
     * [!!] [Session::write] will automatically be called when the request ends.
     *
     * @param   string $type type of session (native, cookie, etc)
     * @param   string $id session identifier
     * @return  Session
     */
    public static function instance($type = NULL, $id = NULL)
    {
        if (!isset(Session::$_instance)) {
            $config = \Mii::$app->config('session');


            // Create a new session instance
            Session::$_instance = $session = new self($config, $id);

            // Write the session at shutdown
            register_shutdown_function([$session, 'close']);
        }

        return Session::$_instance;
    }

    /**
     *
     * Checks for session cookie without starting session itself
     *
     * @return bool
     */

    public static function check_cookie()
    {
        $config = \Mii::$app->config('session');

        return isset($_COOKIE[$config['name']]);
    }

    /**
     * Get the current session cookie name.
     *
     *     $name = $session->name();
     *
     * @return  string
     */
    public function name()
    {
        return $this->_name;
    }

    /**
     * Get a variable from the session array.
     *
     *     $foo = $session->get('foo');
     *
     * @param   string $key variable name
     * @param   mixed $default default value to return
     * @return  mixed
     */
    public function get($key, $default = NULL)
    {
        $this->open();

        return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
    }

    public function open($id = null)
    {

        if ($this->is_active())
            return;

        // Sync up the session cookie with Cookie parameters
        session_set_cookie_params($this->_lifetime, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);

        // Do not allow PHP to send Cache-Control headers
        session_cache_limiter(false);

        // Set the session cookie name
        session_name($this->_name);

        if ($id) {
            // Set the session id
            session_id($id);
        }

        // Start the session
        @session_start();

        // Use the $_SESSION global for storing data
        $this->_data =& $_SESSION;

        return;
    }

    public function is_active()
    {
        return session_status() === PHP_SESSION_ACTIVE;
    }

    /**
     * Set a variable in the session array.
     *
     *     $session->set('foo', 'bar');
     *
     * @param   string $key variable name
     * @param   mixed $value value
     * @return  $this
     */
    public function set($key, $value)
    {
        $this->open();

        $this->_data[$key] = $value;

        return $this;
    }

    /**
     * Set a variable by reference.
     *
     *     $session->bind('foo', $foo);
     *
     * @param   string $key variable name
     * @param   mixed $value referenced value
     * @return  $this
     */
    public function bind($key, & $value)
    {
        $this->open();

        $this->_data[$key] =& $value;

        return $this;
    }

    /**
     * Removes a variable in the session array.
     *
     *     $session->delete('foo');
     *
     * @param   string $key,... variable name
     * @return  $this
     */
    public function delete(...$args)
    {
        $this->open();

        foreach ($args as $key) {
            unset($this->_data[$key]);
        }

        return $this;
    }

    /**
     * Generates a new session id and returns it.
     *
     *     $id = $session->regenerate();
     *
     * @return  string
     */
    public function regenerate($delete_old = false)
    {
        if($this->is_active()) {
            // Regenerate the session id
            @session_regenerate_id($delete_old);
        } else {
            $this->open();
        }

        return session_id();
    }

    /**
     * Sets the last_active timestamp and saves the session.
     *
     *     $session->close();
     *
     * [!!] Any errors that occur during session writing will be logged,
     * but not displayed, because sessions are written after output has
     * been sent.
     *
     * @return  boolean
     */
    public function close()
    {
        if ($this->is_active()) {

            // Set the last active timestamp
            $this->_data['last_active'] = time();

            // Write and close the session
            @session_write_close();
        }
    }

    /**
     * Restart the session.
     *
     *     $success = $session->restart();
     *
     * @return  boolean
     */
    public function restart()
    {
        if ($this->is_active()) {

            // Wipe out the current session.
            $this->destroy();
        }

        // Fire up a new session
        //$status = session_start();
        $this->open();

        // Use the $_SESSION global for storing data
    //    $this->_data =& $_SESSION;

        return ;//$status;
    }

    /**
     * Completely destroy the current session.
     *
     *     $success = $session->destroy();
     *
     * @return  boolean
     */
    public function destroy()
    {
        if ($this->is_active()) {

            session_destroy();
            $this->_data = [];

            // Make sure the session cannot be restarted
            Cookie::delete($this->_name);
        }
    }

    /**
     * @return  string
     */
    public function id()
    {
        return session_id();
    }


} // End Session
<commit_msg>more register_shutdown_function out from init<commit_after><?php

namespace mii\web;


class Session
{

    /**
     * @var  array  session instances
     */
    public static $_instance = NULL;
    /**
     * @var  string  cookie name
     */
    protected $_name = 'session';
    /**
     * @var  int  cookie lifetime
     */
    protected $_lifetime = 0;
    /**
     * @var  bool  encrypt session data?
     */
    protected $_encrypted = false;
    /**
     * @var  array  session data
     */
    protected $_data = [];
    /**
     * @var  bool  session destroyed?
     */
    protected $_destroyed = false;

    /**
     * Overloads the name, lifetime, and encrypted session settings.
     *
     * [!!] Sessions can only be created using the [Session::instance] method.
     *
     * @param   array $config configuration
     * @param   string $id session id
     * @return  void
     * @uses    Session::read
     */
    public function __construct(array $config = NULL, $id = NULL)
    {
        if (isset($config['name'])) {
            // Cookie name to store the session id in
            $this->_name = (string)$config['name'];
        }

        if (isset($config['lifetime'])) {
            // Cookie lifetime
            $this->_lifetime = (int)$config['lifetime'];
        }

        if (isset($config['encrypted'])) {
            if ($config['encrypted'] === true) {
                // Use the default Encrypt instance
                $config['encrypted'] = 'default';
            }

            // Enable or disable encryption of data
            $this->_encrypted = $config['encrypted'];
        }

        // Load the session
        //$this->read($id);
    }

    /**
     * Creates a singleton session of the given type. Some session types
     * (native, database) also support restarting a session by passing a
     * session id as the second parameter.
     *
     *     $session = Session::instance();
     *
     * [!!] [Session::write] will automatically be called when the request ends.
     *
     * @param   string $type type of session (native, cookie, etc)
     * @param   string $id session identifier
     * @return  Session
     */
    public static function instance($id = NULL)
    {
        if (!isset(Session::$_instance)) {

            $config = \Mii::$app->config('session');

            // Create a new session instance
            Session::$_instance = $session = new self($config, $id);

        }

        return Session::$_instance;
    }

    /**
     *
     * Checks for session cookie without starting session itself
     *
     * @return bool
     */

    public static function check_cookie()
    {
        $config = \Mii::$app->config('session');

        return isset($_COOKIE[$config['name']]);
    }

    /**
     * Get the current session cookie name.
     *
     *     $name = $session->name();
     *
     * @return  string
     */
    public function name()
    {
        return $this->_name;
    }

    /**
     * Get a variable from the session array.
     *
     *     $foo = $session->get('foo');
     *
     * @param   string $key variable name
     * @param   mixed $default default value to return
     * @return  mixed
     */
    public function get($key, $default = NULL)
    {
        $this->open();

        return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
    }

    public function open($id = null)
    {

        if ($this->is_active())
            return;

        // Sync up the session cookie with Cookie parameters
        session_set_cookie_params($this->_lifetime, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);

        // Do not allow PHP to send Cache-Control headers
        session_cache_limiter(false);

        // Set the session cookie name
        session_name($this->_name);

        if ($id) {
            // Set the session id
            session_id($id);
        }

        // Start the session
        @session_start();

        // Use the $_SESSION global for storing data
        $this->_data =& $_SESSION;


        // Write the session at shutdown
        register_shutdown_function([$this, 'close']);

        return;
    }

    public function is_active()
    {
        return session_status() === PHP_SESSION_ACTIVE;
    }

    /**
     * Set a variable in the session array.
     *
     *     $session->set('foo', 'bar');
     *
     * @param   string $key variable name
     * @param   mixed $value value
     * @return  $this
     */
    public function set($key, $value)
    {
        $this->open();

        $this->_data[$key] = $value;

        return $this;
    }

    /**
     * Set a variable by reference.
     *
     *     $session->bind('foo', $foo);
     *
     * @param   string $key variable name
     * @param   mixed $value referenced value
     * @return  $this
     */
    public function bind($key, & $value)
    {
        $this->open();

        $this->_data[$key] =& $value;

        return $this;
    }

    /**
     * Removes a variable in the session array.
     *
     *     $session->delete('foo');
     *
     * @param   string $key,... variable name
     * @return  $this
     */
    public function delete(...$args)
    {
        $this->open();

        foreach ($args as $key) {
            unset($this->_data[$key]);
        }

        return $this;
    }

    /**
     * Generates a new session id and returns it.
     *
     *     $id = $session->regenerate();
     *
     * @return  string
     */
    public function regenerate($delete_old = false)
    {
        if($this->is_active()) {
            // Regenerate the session id
            @session_regenerate_id($delete_old);
        } else {
            $this->open();
        }

        return session_id();
    }

    /**
     * Sets the last_active timestamp and saves the session.
     *
     *     $session->close();
     *
     * [!!] Any errors that occur during session writing will be logged,
     * but not displayed, because sessions are written after output has
     * been sent.
     *
     * @return  boolean
     */
    public function close()
    {
        if ($this->is_active()) {

            // Set the last active timestamp
            $this->_data['last_active'] = time();

            // Write and close the session
            @session_write_close();
        }
    }

    /**
     * Restart the session.
     *
     *     $success = $session->restart();
     *
     * @return  boolean
     */
    public function restart()
    {
        if ($this->is_active()) {

            // Wipe out the current session.
            $this->destroy();
        }

        // Fire up a new session
        //$status = session_start();
        $this->open();

        // Use the $_SESSION global for storing data
    //    $this->_data =& $_SESSION;

        return ;//$status;
    }

    /**
     * Completely destroy the current session.
     *
     *     $success = $session->destroy();
     *
     * @return  boolean
     */
    public function destroy()
    {
        if ($this->is_active()) {

            session_destroy();
            $this->_data = [];

            // Make sure the session cannot be restarted
            Cookie::delete($this->_name);
        }
    }

    /**
     * @return  string
     */
    public function id()
    {
        return session_id();
    }


} // End Session

{'fraction_non_alphanumeric': 0.13185991159469568, 'fraction_numerical': 0.0001360081604896294, 'mean_word_length': 1.6525974025974026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1833497', 'n_tokens_mistral': 4201, 'n_tokens_neox': 4066, 'n_words': 1144}