Added Catalyst::IOC::LifeCycle::Request
André Walker [Wed, 3 Aug 2011 22:29:59 +0000 (19:29 -0300)]
lib/Catalyst.pm
lib/Catalyst/IOC/Container.pm
lib/Catalyst/IOC/LifeCycle/Request.pm [new file with mode: 0644]

index 8d566ec..83f5737 100644 (file)
@@ -1644,6 +1644,8 @@ sub finalize {
         $c->finalize_body;
     }
 
+    $c->finalize_ioc;
+
     $c->log_response;
 
     if ($c->use_stats) {
@@ -1761,6 +1763,14 @@ Finalizes uploads. Cleans up any temporary files.
 
 sub finalize_uploads { my $c = shift; $c->engine->finalize_uploads( $c, @_ ) }
 
+=head2 $c->finalize_ioc
+
+Flushes all services with Request lifecycle.
+
+=cut
+
+sub finalize_ioc { shift->container->flush_request_services() }
+
 =head2 $c->get_action( $action, $namespace )
 
 Gets an action in a given namespace.
index a1ca69a..c4c8b5a 100644 (file)
@@ -686,6 +686,19 @@ sub expand_component_module {
     return Devel::InnerPackage::list_packages( $module );
 }
 
+# copied from stevan's OX
+sub flush_request_services {
+    my $self = shift;
+    my @services = $self->get_service_list;
+
+    foreach my $service (@services) {
+        my $injection = $self->get_service($service);
+        if ($injection->does('Catalyst::IOC::LifeCycle::Request')) {
+            $injection->flush_instance;
+        }
+    }
+}
+
 1;
 
 __END__
@@ -893,6 +906,8 @@ Finds components that match a given regexp. Used internally, by find_component.
 Components found by C<locate_components> will be passed to this method, which
 is expected to return a list of component (package) names to be set up.
 
+=head2 flush_request_services
+
 =head2 setup_components
 
 =head1 AUTHORS
diff --git a/lib/Catalyst/IOC/LifeCycle/Request.pm b/lib/Catalyst/IOC/LifeCycle/Request.pm
new file mode 100644 (file)
index 0000000..f350e3a
--- /dev/null
@@ -0,0 +1,10 @@
+package Catalyst::IOC::LifeCycle::Request;
+use Moose::Role;
+use namespace::autoclean;
+
+# based on Bread::Board::LifeCycle::Request from OX
+# just behaves like a singleton - ::Request instances
+# will get flushed after the response is sent
+with 'Bread::Board::LifeCycle::Singleton';
+
+1;