定制 icehawk/session 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

icehawk/session

最新稳定版本:v1.1.1

Composer 安装命令:

composer require icehawk/session

包简介

Session registry component for the IceHawk framework

README 文档

README

Join the chat at https://gitter.im/icehawk/session Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License phpstan enabled

IceHawk Framework

IceHawk\Session

Session registry component for the IceHawk framework.

Intention

This component is intended to wrap the super-global $_SESSION variable and give access to values by explicitly user-defined keys and value/return types and is therefor declared abstract.

Furthermore it provides the registration and interfaces for data mappers on all, several or single keys, e.g. to reduce data overhead stored in session.

Usage

Extend the class AbstractSession

<?php declare(strict_types=1);

namespace MyVendor\MyProject;

use IceHawk\Session\AbstractSession;

final class Session extends AbstractSession
{
    const KEY_SOME_STRING_VALUE = 'someStringValue';
    
    public function getSomeStringValue() : string
    {
        return $this->get( self::KEY_SOME_STRING_VALUE );
    }

    public function setSomeStringValue( string $value )
    {
        $this->set( self::KEY_SOME_STRING_VALUE, $value );
    }

    public function isSomeStringValueSet() : bool
    {
        return $this->isset( self::KEY_SOME_STRING_VALUE );
    }

    public function unsetSomeStringValue()
    {
        $this->unset( self::KEY_SOME_STRING_VALUE );
    }
}

Work with values

<?php declare(strict_types=1);

namespace MyVendor\MyProject;

$session = new Session( $_SESSION );

# Set
$session->setSomeStringValue( 'Hello world' );

# Isset
if ( $session->isSomeStringValueSet() )
{
    # Get
    $someString = $session->getSomeStringValue();
    
    echo $someString . ' was set.';
}

# Unset 
$session->unsetSomeStringValue();

Data mapping

<?php declare(strict_types=1);

namespace MyVendor\MyProject;

use IceHawk\Session\Interfaces\MapsSessionData;

# Create a data mapper class
final class MyDataMapper implements MapsSessionData
{
	public function toSessionData( $value ) 
	{
        return base64_encode( $value );
	}
	
	public function fromSessionData( $sessionData ) 
	{
		return base64_decode( $sessionData );
	}
}

$session = new Session( $_SESSION );

# Add the data mapper for all keys in the registry
$session->addDataMapper( new MyDataMapper() );

# Add the data mapper for one specific key in the registry
$session->addDataMapper( new MyDataMapper(), [Session::KEY_SOME_STRING_VALUE] );

# Add the data mapper for multiple keys in the registry
$session->addDataMapper( new MyDataMapper(), [Session::KEY_SOME_STRING_VALUE, Session::KEY_SOME_OTHER_VALUE] );
  • The data mapper's toSessionData() is called when the AbstractSesion::set() method gets invoked.
  • The data mapper's fromSessionData() is called when the AbstractSesion::get() method gets invoked.

Clear all session data

<?php declare(strict_types=1);

namespace MyVendor\MyProject;

$session = new Session( $_SESSION );

# ... put some data to the session ...

# Clear the session data
$session->clear();

Contributing

Contributions are welcome! Please see our Contribution guide.

统计信息

  • 总下载量: 15.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固