guibranco / pancake

🧰 🛠️ Pancake project - toolkit for PHP projects
https://guibranco.github.io/pancake/
MIT License
3 stars 1 forks source link

[FEATURE] Add a static `SessionManager` class to Pancake #234

Closed guibranco closed 2 weeks ago

guibranco commented 2 weeks ago

Description: I would like to add a SessionManager class to the Pancake toolkit library in PHP. The class should act as a static wrapper around PHP's built-in session management functions, providing an easy-to-use interface for handling session operations.

The SessionManager should handle tasks like starting sessions, setting, getting, and removing session data, and regenerating session IDs. The class will ensure that the session is started only when needed, reducing unnecessary overhead. Below is an example implementation:

<?php

namespace GuiBranco\Pancake;

class SessionManager
{
    public static function start()
    {
        if (session_status() == PHP_SESSION_NONE) {
            session_start();
        }
    }

    public static function set($key, $value)
    {
        self::start();
        $_SESSION[$key] = $value;
    }

    public static function get($key, $default = null)
    {
        self::start();
        return $_SESSION[$key] ?? $default;
    }

    public static function has($key)
    {
        self::start();
        return isset($_SESSION[$key]);
    }

    public static function remove($key)
    {
        self::start();
        unset($_SESSION[$key]);
    }

    public static function destroy()
    {
        if (session_status() != PHP_SESSION_NONE) {
            session_unset();
            session_destroy();
        }
    }

    public static function regenerate()
    {
        if (session_status() != PHP_SESSION_NONE) {
            session_regenerate_id(true);
        }
    }
}

This class will provide a convenient interface for session handling and improve the Pancake library's overall functionality.

Acceptance Criteria:

guibranco commented 2 weeks ago

@gstraccini create labels

gstraccini[bot] commented 2 weeks ago

Creating 13 labels and updating 16 labels! :label:

Humayun-23 commented 2 weeks ago

Hello, I want to work on this issue, please assign me this.

guibranco commented 2 weeks ago

Hi @Humayun-23 ,

Thanks for picking this up! 🙌 You have been assigned the issue.

If you have any questions or need clarification along the way, feel free to ask.

Good luck and happy coding! 😊