The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

Web::ComposableRequest::Session - Session object base class

Synopsis

   my $class_stash = {};

   my $_build_session_class = sub {
      my $self         = shift;
      my $base         = $self->_config->session_class;
      my $session_attr = $self->_config->session_attr;
      my @session_attr = keys %{ $session_attr };

      @session_attr > 0 or return $base;

      my $class = "${base}::".(substr md5_hex( join q(), @session_attr ), 0, 8);

      exists $class_stash->{ $class } and return $class_stash->{ $class };

      my @attrs;

      for my $name (@session_attr) {
         my ($type, $default) = @{ $session_attr->{ $name } };
         my $props            = [ is => 'rw', isa => $type ];

         defined $default and push @{ $props }, 'default', $default;
         push @attrs, $name, $props;
      }

      return $class_stash->{ $class } = subclass_of
         ( $base, -package => $class, -has => [ @attrs ] );
   };

   has 'session'   => is => 'lazy', isa => Object, builder => sub {
      return $_[ 0 ]->session_class->new
         ( config  => $_[ 0 ]->_config,
           log     => $_[ 0 ]->_log,
           session => $_[ 0 ]->_env->{ 'psgix.session' }, ) },
      handles      => [ 'authenticated', 'username' ];

Description

Session object base class

Configuration and Environment

Defines the following attributes;

authenticated

A boolean which defaults to false.

messages

A hash reference of messages keyed by message id

updated

The unix time this session was last updated

username

The name of the authenticated user. Defaults to NUL if the user is anonymous

Subroutines/Methods

BUILD

Tests to see if the session has expired and if so sets the "authenticated" boolean to false

BUILDARGS

Copies the session values into the hash reference used to instantiate the object from the Plack environment

add_status_message

   $message_id = $session->add_status_message( $message );

Appends the message to the message queue for this session. The $message argument is an array reference, first the message then the positional parameters

collect_message_id

   $mid = $session->collect_message_id( $req );

Return any pending message id

collect_status_message

   $localised_message = $session->collect_status_message( $req );

Returns the next message in the queue (if there is one) for the given request

collect_status_messages

   \@localised_messages = $session->collect_status_messages( $req );

Returns previous messages in the queue (if there are any) for the given request or any previous requests

trim_message_queue

   $session->trim_message_queue;

Reduce the size of the message queue the maximum allowed by the configuration

update

   $session->update;

Copy the attribute values back to the Plack environment

Diagnostics

None

Dependencies

Unexpected

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There are no known bugs in this module. Please report problems to http://rt.cpan.org/NoAuth/Bugs.html?Dist=Web-ComposableRequest. Patches are welcome

Acknowledgements

Larry Wall - For the Perl programming language

Author

Peter Flanigan, <pjfl@cpan.org>

License and Copyright

Copyright (c) 2017 Peter Flanigan. All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE