File Coverage

blib/lib/Bread/Board/Container/Role/WithSessions.pm
Criterion Covered Total %
statement 25 25 100.0
branch 7 10 70.0
condition 5 9 55.5
subroutine 5 5 100.0
pod 1 1 100.0
total 43 50 86.0


line stmt bran cond sub pod time code
1             package Bread::Board::Container::Role::WithSessions;
2              
3             our $AUTHORITY = 'cpan:GSG';
4             our $VERSION = '0.90';
5              
6 2     2   1234 use Moose::Role;
  2         4  
  2         15  
7 2     2   9375 use namespace::autoclean;
  2         3  
  2         22  
8 2     2   179 use List::Util 1.33 ('any');
  2         67  
  2         872  
9              
10             our @LIFECYCLES_TO_FLUSH = qw(
11             Session
12             Session::WithParameters
13             +Bread::Board::LifeCycle::Session
14             +Bread::Board::LifeCycle::Session::WithParameters
15             );
16              
17             sub flush_session_instances {
18 2     2 1 9849 my $self = shift;
19              
20 2         7 my @containers = ($self);
21 2         4 my $flush_count = 0;
22              
23             # Traverse the sub containers to find any Session services
24 2         12 while (my $container = shift @containers) {
25 2         5 push @containers, values %{$container->sub_containers};
  2         87  
26 2         19 foreach my $service (values %{$container->services}) {
  2         71  
27 3 50       133 next unless defined $service->lifecycle;
28 3 50   4   43 next unless any { $service->lifecycle eq $_ } @LIFECYCLES_TO_FLUSH;
  4         126  
29             next unless (
30 3 50 66     175 $service->can('has_instance') && $service->has_instance ||
      33        
      66        
31             $service->can('instances') && values $service->instances
32             );
33              
34 3 100       139 $service->flush_instance if $service->can('flush_instance');
35 3 100       262 $service->flush_instances if $service->can('flush_instances');
36 3         13 $flush_count++;
37             }
38             };
39              
40 2         7 return $flush_count;
41             }
42              
43             1;
44              
45             __END__
46              
47             =pod
48              
49             =encoding UTF-8
50              
51             =head1 NAME
52              
53             Bread::Board::Container::Role::WithSessions
54              
55             =head1 VERSION
56              
57             version 0.90
58              
59             =head1 DESCRIPTION
60              
61             This role defines Session helper methods for Containers.
62              
63             =head1 METHODS
64              
65             =head2 flush_session_instances
66              
67             This method clears all Session instances from the container and any sub-containers. In most cases, this should be called on the root
68             container, but it can be called on a sub-container, if you want to only clear out services within that container.
69              
70             If successful, it will return the number of services that were flushed. Note that this may be zero.