Solstice::Cookie - An interface for managing cookies in your solstice apps.


Solstice documentation Contained in the Solstice distribution.

Index


Code Index:

NAME

Top

Solstice::Cookie - An interface for managing cookies in your solstice apps.

SYNOPSIS

Top

  my $cookie = Solstice::Cookie->new();
  $cookie->setName('name');
  $cookie->setValue('value');
  $cookie->setExpiration(Solstice::DateTime->new());
  $cookie->bake();

  $cookie->expire();

  my $cookie = Solstice::Cookie->new('name');
  my $value  = Solstice::Cookie->getValue();

DESCRIPTION

Top

Creates a new cookie. If passed a name, it will try to find the value for that cookie.

bake()

Sends the cookie to the browser.

Modules Used

Solstice::Service, Solstice::LogService, Solstice::UserService, Solstice::ValidationParam, Solstice::CGI, Data::FormValidator.

AUTHOR

Top

Catalyst Group, <catalyst@u.washington.edu>

VERSION

Top

Version $Revision: 3177 $

COPYRIGHT

Top


Solstice documentation Contained in the Solstice distribution.
package Solstice::Cookie;

use strict;
use warnings;
use 5.006_000;

use constant TRUE   => 1;
use constant FALSE  => 0;

use base qw(Solstice::Model);

use CGI::Cookie;

sub new {
    my $obj = shift;
    my $name = shift;

    my $self = $obj->SUPER::new();

    if (defined $name) {
        $self->_init($name);
    }

    return $self;
}

sub _init {
    my $self = shift;
    my $name = shift;

    my $server = Solstice::Server->new();
    my %cookies = fetch CGI::Cookie;
    my $cookie = $cookies{$name};

    if (defined $cookie) {
        $self->setName($name);
        $self->setValue($cookie->value());
    }

    return TRUE;
}

sub bake {
    my $self = shift;

    my $expiration_seconds;
    my $expiration = $self->getExpiration();

    if (defined $expiration) {
        my $current = Solstice::DateTime->new(time);
        $expiration_seconds = $current->getTimeApart($expiration);
    }

    my $path = '/'.Solstice::Configure->new()->getVirtualRoot();
    $path =~ s/\/+/\//g;

    my $expiration_string;
    if (defined $expiration_seconds) {
        $expiration_string = '+'.$expiration_seconds.'s';
    }
    my $cookie = CGI::Cookie->new(
        -name    => $self->getName(),
        -value   => $self->getValue(),
        -expires => (defined $expiration_string ? $expiration_string : undef),
        -path    => $path,
    );

    my $server = Solstice::Server->new();
    $server->addHeader('Set-Cookie', $cookie);
}

sub _getAccessorDefinition {
    return [
    {
        name        => 'Name',
        type        => 'String',
    },
    {
        name        => 'Value',
        type        => 'String',
    },
    {
        name        => 'Expiration',
        type        => 'DateTime',
    },
    ];
}

1;
__END__