cse/helpers-session

该辅助工具允许您轻松管理会话数据。START、SET、GET、DELETE、HAS方法会话 - 所有这些都在这个库中可用。

1.1.3 2019-04-29 04:11 UTC

This package is auto-updated.

Last update: 2024-09-13 03:48:42 UTC


README

英语 | 俄语

SESSION CSE 辅助工具

Travis (.org) Codecov Scrutinizer code quality

Packagist Minimum PHP Version Packagist GitHub repo size

该辅助工具允许您轻松管理会话数据。START、SET、GET、DELETE、HAS方法会话 - 所有这些都在这个库中可用。

项目仓库: https://github.com/cs-eliseev/helpers-session

演示

Session::set(
    'example_key',
    Session::getNotEmpty('example_key', 'default_value')
);

if (is_int(Session::get('example_key'))) {
    Session::remove('example_key');
}

$is_not_int = Session::has('example_key');

简介

CSE 辅助工具 是一系列用PHP编写的简单函数的集合,供人们使用。

尽管PHP是互联网的主要编程语言,但其功能并不足够。SESSION CSE 辅助工具允许您轻松地START、SET、GET和DELETE会话。

CSE 辅助工具 是为了快速开发Web应用程序而创建的。

CSE 辅助工具项目

以下将提供有关如何初始化库和执行常见命令的一些信息。

安装

您可以在此处找到此项目的最新版本。

Composer

执行以下命令以获取该软件包的最新版本

composer require cse/helpers-session

或者 composer.json 文件应包含以下内容

{
    "require": {
        "cse/helpers-session": "*"
    }
}

Git

将此存储库克隆到本地

git clone https://github.com/cs-eliseev/helpers-session.git

下载

在此处下载最新版本.

使用方法

该类由静态方法组成,这些方法在任意项目中方便使用。请参阅示例 examples-session.php

开始会话

示例

Session::start();
// true

设置会话

示例

Session::set('example_key', 'example_value');
// ['example_key' => 'example_value']

使用多键

Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
// ['cse' => ['example_key_2' => 'example_value_2']]

检查会话

示例

Session::set('example_key', 'example_value');
Session::has('example_key');
// true

使用多键

Session::setMultiKey('cse');
Session::has('example_key');
// false

获取会话

示例

Session::set('example_key', 'example_value');
Session::get('example_key');
// example_value

使用多键

Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::get('example_key_2');
// example_value_2

设置不存在会话的默认值

Cookie::get('example_key_3', 'example_default_value_3');
// example_default_value_3

获取非空会话

示例

Session::set('example_key', 'example_value');
Session::getNotEmpty('example_key', 'example_default_value');
// example_value

使用多键

Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::getNotEmpty('example_key_2');
// example_value_2

设置不存在会话的默认值

Cookie::getNotEmpty('example_key_3', 'example_default_value_3');
// example_default_value_3

设置空会话数据的默认值

Session::set('example_key_4', '');
Cookie::getNotEmpty('example_key_4', 'example_default_value_4');
// example_default_value_4

删除会话

示例

Session::set('example_key', 'example_value');
Session::remove('example_key');
Session::has('example_key');
// false

使用多键

Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::remove('example_key_2');
Session::has('example_key_2');
// false

设置多键会话

示例

Session::set('example_key', 'example_value');
// ['example_key' => 'example_value']
Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
/**
* [
*     'example_key' => 'example_value',
*     'cse' => [
*         'example_key_2' => 'example_value_2'
*     ]
* ]
*/
Session::set('example_key_3', 'example_value_3');
/**
* [
*     'example_key' => 'example_value',
*     'cse' => [
*         'example_key_2' => 'example_value_2',
*         'example_key_3' => 'example_value_3'
*     ]
* ]
*/
Session::setMultiKey();
Session::set('example_key_4', 'example_value_4');
/**
* [
*     'example_key' => 'example_value',
*     'cse' => [
*         'example_key_2' => 'example_value_2',
*         'example_key_3' => 'example_value_3'
*     ],
*     'example_key_4' => 'example_value_4',
*     'example' => [
*         'example_key_5' => 'example_value_5'
*     ],
* ]
*/
Session::setMultiKey('example');
Session::set('example_key_5', 'example_value_5');

全局使用

class DefaultSessionData
{
    public function setSessionData(): void
    {
        Session::setMultiKey();
        Session::set('example_key_2', 'example_value_2');
    }
}

class CseSessionData
{
    public function setSessionData(): void
    {
        Session::setMultiKey('cse');
        Session::set('example_key_1', 'example_value_1');
    }
}

class ExtendSessionData
{
    public function setSessionData(string $key, string $value): void
    {
        Session::set($key, $value);
    }
}

$default = new DefaultSessionData();
$cse = new CseSessionData();
$extend = new ExtendSessionData();

$extend->setSessionData('example_key_0', 'example_value_0');
// ['example_key_0' => 'example_value_0']
$cse->setSessionData();
/**
* [
*     'example_key_0' => 'example_value_0',
*     'cse' => [
*         'example_key_1' => 'example_value_1'
*     ]
* ]
*/
$extend->setSessionData('example_key_1_1', 'example_value_1_1');
/**
* [
*     'example_key_0' => 'example_value_0',
*     'cse' => [
*         'example_key_1' => 'example_value_1',
*         'example_key_1_1' => 'example_value_1_1'
*     ]
* ]
*/
$default->setSessionData();
/**
* [
*     'example_key_0' => 'example_value_0',
*     'cse' => [
*         'example_key_1' => 'example_value_1',
*         'example_key_1_1' => 'example_value_1_1'
*     ],
*     'example_key_2' => 'example_value_2'
* ]
*/
$extend->setSessionData('example_key_2_1', 'example_value_2_1');
/**
* [
*     'example_key_0' => 'example_value_0',
*     'cse' => [
*         'example_key_1' => 'example_value_1',
*         'example_key_1_1' => 'example_value_1_1'
*     ],
*     'example_key_2' => 'example_value_2',
*     'example_key_2_1' => 'example_value_2_1'
* ]
*/

获取所有会话数据

示例

Session::set('example_key', 'example_value');
Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::setMultiKey();
Session::all();
/**
* [
*     'example_key' => 'example_key',
*     'cse' => [
*         'example_key_2' => 'example_value_2'
*     ]
* ]
*/

使用多键

Session::set('example_key', 'example_value');
Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::all();
// ['example_key_2' => 'example_value_2']

清除会话数据

示例

Session::set('example_key', 'example_value');
Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::setMultiKey();
Session::claer();
// []

使用多键

Session::set('example_key', 'example_value');
Session::setMultiKey('cse');
Session::set('example_key_2', 'example_value_2');
Session::claer();
// ['example_key' => 'example_value']

销毁会话

示例

Session::start();
// session_status() === PHP_SESSION_ACTIVE => true
Session::destroy();
// session_status() === PHP_SESSION_ACTIVE => false

检查会话是否开始

示例

Session::start();
Session::isStart();
// true
Session::destroy();
Session::isStart();
// false

测试与代码覆盖率

使用PHPUnit进行单元测试。单元测试确保类和方法确实执行了预期的操作。

PHPUnit的一般文档可以在https://phpunit.de/documentation.html找到。

要运行PHPUnit单元测试,请执行

phpunit PATH/TO/PROJECT/tests/

如果您想生成代码覆盖率报告,请使用以下

phpunit --coverage-html ./report PATH/TO/PROJECT/tests/

使用了PHPUnit默认配置

phpunit --configuration PATH/TO/PROJECT/phpunit.xml

捐赠

您可以通过此处支持此项目。您还可以通过为项目贡献力量或报告错误来帮助。提出关于功能的建议也很好。任何帮助都十分欢迎。

许可

SESSION CSE 辅助工具是MIT许可下的开源PHP库。有关更多信息,请参阅许可文件

GitHub @cs-eliseev