| 1 | package Bread::Board::Container::Role::WithSessions; | ||
| 2 | |||
| 3 | our $AUTHORITY = 'cpan:GSG'; | ||
| 4 | our $VERSION = '0.90'; | ||
| 5 | |||
| 6 | 1 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use List::Util 1.33 ('any'); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 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 | my $self = shift; | ||
| 19 | |||
| 20 | my @containers = ($self); | ||
| 21 | my $flush_count = 0; | ||
| 22 | |||
| 23 | # Traverse the sub containers to find any Session services | ||
| 24 | while (my $container = shift @containers) { | ||
| 25 | push @containers, values %{$container->sub_containers}; | ||
| 26 | foreach my $service (values %{$container->services}) { | ||
| 27 | next unless defined $service->lifecycle; | ||
| 28 | next unless any { $service->lifecycle eq $_ } @LIFECYCLES_TO_FLUSH; | ||
| 29 | next unless ( | ||
| 30 | $service->can('has_instance') && $service->has_instance || | ||
| 31 | $service->can('instances') && values $service->instances | ||
| 32 | ); | ||
| 33 | |||
| 34 | $service->flush_instance if $service->can('flush_instance'); | ||
| 35 | $service->flush_instances if $service->can('flush_instances'); | ||
| 36 | $flush_count++; | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | 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. | ||