Dancer2-Plugin-Interchange6
view release on metacpan or search on metacpan
lib/Dancer2/Plugin/Interchange6.pm view on Meta::CPAN
package Dancer2::Plugin::Interchange6;
use strict;
use warnings;
use Dancer2::Plugin;
use Dancer2::Plugin::Interchange6::Business::OnlinePayment;
use Module::Runtime 'use_module';
use Scalar::Util 'weaken';
=head1 NAME
Dancer2::Plugin::Interchange6 - Interchange6 Shop Plugin for Dancer2
=head1 VERSION
Version 0.204
=cut
our $VERSION = '0.204';
=head1 REQUIREMENTS
All Interchange6 Dancer2 applications need to use the L<Dancer2::Session::DBIC>
engine.
The easiest way to configure this is in your C<config.yml> (or whatever other
configuration file you prefer):
plugins
DBIC:
default:
schema_class: Interchange6::Schema
# ... other DBIC plugin config here
engines:
session:
DBIC:
db_connection_name: default # connection name from DBIC plugin
session: DBIC
=head1 CONFIGURATION
Available configuration options:
plugins:
Interchange6:
cart_class: MyApp::Cart
carts_var_name: some_other_var
=over
=item * cart_class
If you wish to subclass the cart you can have L</shop_cart> return your
subclassed cart instead. You set the cart class via C<cart_class>.
Defaults to L<Dancer2::Plugin::Interchange6::Cart>.
=item * carts_var_name
The plugin caches carts in a L<Dancer2/var> and the name of the var used can
be set via C<carts_var_name>. Defaults to C<ic6_carts>.
=back
=head1 ROUTES
You can use the L<Dancer2::Plugin::Interchange6::Routes> plugin bundled with this
plugin to setup standard routes for:
lib/Dancer2/Plugin/Interchange6.pm view on Meta::CPAN
is => 'ro',
from_config => sub { 'ic6_carts' },
);
# plugins we use
has plugin_auth_extensible => (
is => 'ro',
is => 'lazy',
default => sub {
$_[0]->app->with_plugin('Dancer2::Plugin::Auth::Extensible');
},
handles => ['logged_in_user'],
init_arg => undef,
);
has plugin_dbic => (
is => 'ro',
is => 'lazy',
default => sub {
$_[0]->app->with_plugin('Dancer2::Plugin::DBIC');
},
handles => [ 'resultset', 'schema' ],
init_arg => undef,
);
# hooks
plugin_hooks(
qw/before_cart_add_validate
before_cart_add after_cart_add
before_cart_update after_cart_update
before_cart_remove_validate
before_cart_remove after_cart_remove
before_cart_rename after_cart_rename
before_cart_clear after_cart_clear
before_cart_set_users_id after_cart_set_users_id
before_cart_set_sessions_id after_cart_set_sessions_id
before_cart_display
before_checkout_display
before_login_display
/
);
plugin_keywords 'shop_address',
'shop_attribute',
[ 'shop_cart', 'cart' ],
'shop_charge',
'shop_country',
'shop_message',
'shop_navigation',
'shop_order',
'shop_product',
'shop_redirect',
'shop_schema',
'shop_state',
'shop_user';
sub BUILD {
my $plugin = shift;
weaken ( my $weak_plugin = $plugin );
$plugin->app->add_hook(
Dancer2::Core::Hook->new(
name => 'before',
code => sub {
# D2PAE::Provider::DBIC returns logged_in_user as hashref
# instead of a proper user result so we have to mess about.
# At some point in the future D2PAE will be fixed to allow
# user objects to be returned.
my $user = $weak_plugin->logged_in_user || undef;
if ( $user ) {
$user = $weak_plugin->shop_user->find(
{
username => $user->{username}
}
);
}
$weak_plugin->shop_schema->set_current_user($user);
},
)
);
}
sub shop_address {
shift->_shop_resultset( 'Address', @_ );
}
sub shop_attribute {
shift->_shop_resultset( 'Attribute', @_ );
}
sub shop_cart {
my $plugin = shift;
my %args;
# cart name from arg or default 'main'
$args{name} = @_ == 1 ? $_[0] : 'main';
# set name of var we will stash carts in
my $var = $plugin->carts_var_name;
$plugin->app->log( "debug", "carts_var_name: $var" );
my $carts = $plugin->app->request->var($var) || {};
if ( !defined $carts->{ $args{name} } ) {
# can't find this cart in stash
$args{plugin} = $plugin;
$args{schema} = $plugin->schema;
$args{sessions_id} = $plugin->app->session->id;
if ( my $user_ref = $plugin->logged_in_user ) {
# user is logged in
# FIXME: D2PAE currently returns a hashref
$args{users_id} = $user_ref->{users_id};
}
$carts->{ $args{name} } = use_module($plugin->cart_class)->new(%args);
( run in 3.007 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )