File Coverage

blib/lib/Bread/Board/LifeCycle/Session.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Bread::Board::LifeCycle::Session;
2              
3             our $AUTHORITY = 'cpan:GSG';
4             # ABSTRACT: A short-lived singleton for Bread::Board
5 1     1   9955 use version;
  1         2054  
  1         6  
6             our $VERSION = 'v0.900.1'; # VERSION
7              
8 1     1   87 use Moose::Role;
  1         3  
  1         8  
9 1     1   5688 use Module::Runtime ();
  1         3  
  1         18  
10 1     1   5 use namespace::autoclean;
  1         2  
  1         6  
11              
12             our $FLUSHER_ROLE = 'Bread::Board::Container::Role::WithSessions';
13              
14             with 'Bread::Board::LifeCycle::Singleton';
15              
16             ### XXX: Lifecycle consumption happens after service construction,
17             ### so we have pick a method that would get called after
18             ### construction. The 'get' method is pretty hot, so this should
19             ### be done as fast as possible.
20              
21             before get => sub {
22             my $self = shift;
23              
24             # Assume we've already done this if an instance exists
25             return if $self->has_instance;
26              
27             Module::Runtime::require_module($FLUSHER_ROLE);
28              
29             my @containers = ($self->get_root_container);
30              
31             # Traverse the sub containers and apply the WithSessions role
32             while (my $container = shift @containers) {
33             push @containers, values %{$container->sub_containers};
34              
35             Class::MOP::class_of($FLUSHER_ROLE)->apply($container)
36             unless $container->meta->does_role($FLUSHER_ROLE);
37             }
38             };
39              
40             1;
41              
42             __END__
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             Bread::Board::LifeCycle::Session - A short-lived singleton for Bread::Board
51              
52             =head1 VERSION
53              
54             version v0.900.1
55              
56             =head1 SYNOPSIS
57              
58             use Bread::Board;
59              
60             my $c = container 'Reports' => as {
61             service generic_report => (
62             class => 'Report',
63             lifecycle => 'Session',
64             );
65             };
66              
67             sub dispatch {
68             # ... dispatch code ...
69              
70             my $services_flushed = $c->flush_session_instances;
71             }
72              
73             =head1 DESCRIPTION
74              
75             This implements a short-term "Session" lifecycle for Bread::Board. Services with this lifecycle will exist as a singleton until they
76             are flushed with the L<flush_session_instances|Bread::Board::Container::Role::WithSessions/flush_session_instances> method. The idea is
77             that this method would be called at the end of a web request, but a "session" could be defined as any sort of short-term cycle.
78              
79             The L<Bread::Board::Container::Role::WithSessions> role is applied to all containers that exist in or around the service.
80              
81             This module is similar to L<Bread::Board::LifeCycle::Request>, but has no connections to L<OX>.
82              
83             =head1 ACKNOWLEDGEMENTS
84              
85             Thanks to Grant Street Group L<http://www.grantstreet.com> for funding development of this code.
86              
87             Thanks to Steve Grazzini (C<< <GRAZZ@CPAN.org> >>) for discussion of the concept.
88              
89             =head1 AUTHOR
90              
91             Grant Street Group <developers@grantstreet.com>
92              
93             =head1 COPYRIGHT AND LICENSE
94              
95             This software is Copyright (c) 2015 - 2020 by Grant Street Group.
96              
97             This is free software, licensed under:
98              
99             The Artistic License 2.0 (GPL Compatible)
100              
101             =cut