Here is a hastily written plugin for Apache::Session.  I'm willing to
maintain it, but I'm not sure if I have the time, so if someone else wants
to own it, please be my guest.

Hopefully someone will find it useful, since perl is now off-limits :~(

++t

Tony Payne  :  Sr. Software Engineer  :  PETsMART.com  :  626-817-7151
#==============================================================================
#
# Template::Plugin::Session
#
# DESCRIPTION
#
# A Template Plugin to provide a Template Interface to Apache::Session
#
# AUTHOR
#   Tony Payne <[EMAIL PROTECTED]>
#
# COPYRIGHT
#
#   Copyright (C) 2001 Tony Payne.  All Rights Reserved
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
#------------------------------------------------------------------------------
#
# $Id: $
#

package Template::Plugin::Session;

require 5.004;

use strict;
use Template::Plugin;
use Apache::Session;

use vars qw($VERSION $DEBUG);
use base qw(Template::Plugin);

$VERSION = sprintf("%.02f", (q$Revision: 2.4 $ =~ /(\d+.\d+)/) - 1);
$DEBUG   = 0 unless defined $DEBUG;

# Load all known Apache Session implementations.  Ignore errors.
BEGIN {
  eval {
    require Apache::Session::MySql;
  };
  eval {
    require Apache::Session::Postgres;
  };
  eval {
    require Apache::Session::Oracle;
  };
  eval {
    require Apache::Session::File;
  };
  eval {
    require Apache::Session::DB_File;
  };
}

sub load {
  my ($class, $context) = shift;
  return $class;
}

sub new {
  my ($class, $context, $session_id, $type, $options) = @_;

  my %session;

  $session_id ||= undef;

  $type ||= 'Apache::Session::DB_File';
  $options = { } unless ref $options && ref $options eq 'HASH';
  $options->{FileName} ||= '/tmp/apache_sessions/sessions.db' 
    if $type eq 'Apache::Session::DB_File';
  $options->{LockDirectory} ||= '/tmp/apache_sessions/lock' 
    if $type eq 'Apache::Session::DB_File';
   
  tie %session, $type, $session_id, $options;
  $session{session_id} = $session{_session_id};
  return \%session;
}

1;

__END__
[% TAGS star -%]
=head1 SYNOPSIS

    [% USE Session(session_id) %]

    [% Session.timestamp = time %]

    [% Session.session_id %]
    
=head1 DESCRIPTION

This is a Template Toolkit Plugin Interface to the Apache::Session
module.  A Session object will be instantiated via the following directive:

    [% USE Session %]

This will create a new session and will assign a new session id.  To connect to
a pre-existing session, specify the session id:

    [% USE Session(session_id) %]

By default, the Session plugin defaults to using a berkley db database for the 
session store.  You can override this, and the Apache::Session options by 
using the second and third arguments to the constructor:

    [% USE Session(
            session_id, 
            'Apache::Session::MySQL', 
            { DataSource => 'dbi:mysql:sessoins' }) %]

Please see L<Apache::Session> for more information on implementations of 
Apache::Session.

=head1 NOTE

Apache::Session automatically stores changes only to the top layer of the hash.
If changes are made to data structures stored as values in the hash, there is
no way for those changes to be noticed.  If you have a complicated session 
object, you should touch a top-level attribute each time you change session
data. 

=head1 SPECIAL ATTRIBUTES

Apache::Session defines a _session_id data member, which holds the session id.
However, since this is incompatible with Template-Toolkit, 
Template::Plugin::Session defines a session_id data member as well.

=head1 AUTHOR

Tony Payne E<lt>[EMAIL PROTECTED]<gt>

[* PROCESS misc/version *]

=head1 COPYRIGHT

Copyright (C) 2001 Tony Payne All Rights Reserved.

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

[* PROCESS misc/seealso
    seealso=page.seealso *]

Reply via email to