2017 © Pedro Peláez
 

library session

Session registry component for the IceHawk framework

image

icehawk/session

Session registry component for the IceHawk framework

  • Tuesday, October 11, 2016
  • by hollodotme
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2,893 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 10 % Grown

The README.md

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

IceHawk Framework

IceHawk\Session

Session registry component for the IceHawk framework., (*2)

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., (*3)

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., (*4)

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., (*5)

The Versions

11/10 2016

dev-development

dev-development

Session registry component for the IceHawk framework

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

07/10 2016

dev-master

9999999-dev

Session registry component for the IceHawk framework

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

07/10 2016

v1.1.1

1.1.1.0

Session registry component for the IceHawk framework

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

10/07 2016

v1.1.0

1.1.0.0

Session registry component for the IceHawk framework

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

07/07 2016

v1.0.1

1.0.1.0

Session registry component for the IceHawk framework

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

05/07 2016

v1.0.0

1.0.0.0

Session registry component for the IceHawk framework

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires