File Coverage

blib/lib/Bread/Board/Container/Role/WithSessions.pm
Criterion Covered Total %
statement 29 29 100.0
branch 7 10 70.0
condition 5 9 55.5
subroutine 6 6 100.0
pod 1 1 100.0
total 48 55 87.2


line stmt bran cond sub pod time code
1             package Bread::Board::Container::Role::WithSessions;
2              
3             our $AUTHORITY = 'cpan:GSG';
4             # ABSTRACT: Session helper methods for Bread::Board Containers
5 2     2   1918 use version;
  2         2072  
  2         14  
6             our $VERSION = 'v0.900.1'; # VERSION
7              
8 2     2   250 use Moose::Role;
  2         5  
  2         21  
9 2     2   11026 use namespace::autoclean;
  2         5  
  2         19  
10 2     2   171 use List::Util 1.33 ('any');
  2         52  
  2         719  
11              
12             our @LIFECYCLES_TO_FLUSH = qw(
13             Session
14             Session::WithParameters
15             +Bread::Board::LifeCycle::Session
16             +Bread::Board::LifeCycle::Session::WithParameters
17             );
18              
19             sub flush_session_instances {
20 2     2 1 9974 my $self = shift;
21              
22 2         7 my @containers = ($self);
23 2         5 my $flush_count = 0;
24              
25             # Traverse the sub containers to find any Session services
26 2         11 while (my $container = shift @containers) {
27 2         5 push @containers, values %{$container->sub_containers};
  2         78  
28 2         24 foreach my $service (values %{$container->services}) {
  2         61  
29 3 50       112 next unless defined $service->lifecycle;
30 3 50   4   48 next unless any { $service->lifecycle eq $_ } @LIFECYCLES_TO_FLUSH;
  4         112  
31             next unless (
32             $service->can('has_instance') && $service->has_instance ||
33 3 50 66     118 $service->can('instances') && values %{$service->instances}
  1   33     37  
      66        
34             );
35              
36 3 100       106 $service->flush_instance if $service->can('flush_instance');
37 3 100       273 $service->flush_instances if $service->can('flush_instances');
38 3         17 $flush_count++;
39             }
40             };
41              
42 2         9 return $flush_count;
43             }
44              
45             1;
46              
47             __END__
48              
49             =pod
50              
51             =encoding UTF-8
52              
53             =head1 NAME
54              
55             Bread::Board::Container::Role::WithSessions - Session helper methods for Bread::Board Containers
56              
57             =head1 VERSION
58              
59             version v0.900.1
60              
61             =head1 DESCRIPTION
62              
63             This role defines Session helper methods for Containers.
64              
65             =head1 METHODS
66              
67             =head2 flush_session_instances
68              
69             This method clears all Session instances from the container and any sub-containers. In most cases, this should be called on the root
70             container, but it can be called on a sub-container, if you want to only clear out services within that container.
71              
72             If successful, it will return the number of services that were flushed. Note that this may be zero.
73              
74             =head1 AUTHOR
75              
76             Grant Street Group <developers@grantstreet.com>
77              
78             =head1 COPYRIGHT AND LICENSE
79              
80             This software is Copyright (c) 2015 - 2020 by Grant Street Group.
81              
82             This is free software, licensed under:
83              
84             The Artistic License 2.0 (GPL Compatible)
85              
86             =cut