@@ -27,7 +27,8 @@ class sfContext implements ArrayAccess
2727 $ configuration = null ,
2828 $ mailerConfiguration = array (),
2929 $ serviceContainerConfiguration = array (),
30- $ factories = array ();
30+ $ factories = array (),
31+ $ hasShutdownUserAndStorage = false ;
3132
3233 protected static
3334 $ instances = array (),
@@ -87,6 +88,7 @@ public function initialize(sfApplicationConfiguration $configuration)
8788 }
8889
8990 $ this ->dispatcher ->connect ('template.filter_parameters ' , array ($ this , 'filterTemplateParameters ' ));
91+ $ this ->dispatcher ->connect ('response.fastcgi_finish_request ' , array ($ this , 'shutdownUserAndStorage ' ));
9092
9193 // register our shutdown function
9294 register_shutdown_function (array ($ this , 'shutdown ' ));
@@ -592,6 +594,22 @@ public function filterTemplateParameters(sfEvent $event, $parameters)
592594 return $ parameters ;
593595 }
594596
597+ /**
598+ * Shuts the user/storage down.
599+ *
600+ * @internal Should be called only via invoking "response.fastcgi_finish_request" or context shutting down.
601+ */
602+ public function shutdownUserAndStorage ()
603+ {
604+ if (!$ this ->hasShutdownUserAndStorage && $ this ->has ('user ' ))
605+ {
606+ $ this ->getUser ()->shutdown ();
607+ $ this ->getStorage ()->shutdown ();
608+
609+ $ this ->hasShutdownUserAndStorage = true ;
610+ }
611+ }
612+
595613 /**
596614 * Calls methods defined via sfEventDispatcher.
597615 *
@@ -635,12 +653,7 @@ public function __call($method, $arguments)
635653 */
636654 public function shutdown ()
637655 {
638- // shutdown all factories
639- if ($ this ->has ('user ' ))
640- {
641- $ this ->getUser ()->shutdown ();
642- $ this ->getStorage ()->shutdown ();
643- }
656+ $ this ->shutdownUserAndStorage ();
644657
645658 if ($ this ->has ('routing ' ))
646659 {
0 commit comments