JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add: atexit.php
authorJason Woofenden <jason@jasonwoof.com>
Wed, 5 Oct 2011 02:59:53 +0000 (22:59 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 5 Oct 2011 02:59:53 +0000 (22:59 -0400)
atexit.php [new file with mode: 0644]
http.php
run.php

diff --git a/atexit.php b/atexit.php
new file mode 100644 (file)
index 0000000..4a8e291
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+# this file is much like register_shutdown_function(), except:
+#
+# 1.   it uses the standard name you know and love
+#
+# 2.   wfpl will call it before calling display_messages() for you, or before
+#      calling session_save_messages() (when it's about to redirect.
+
+$GLOBALS['wfpl_atexit_queue'] = array();
+
+# your passed function will be called by run.php just before display_messages
+# (or right before an http redirect)
+#
+# order is fifo
+function atexit($func, $args) {
+       $GLOBALS['wfpl_atexit_queue'][] = array($func, $args);
+}
+
+# call the functions queued up by atexit()
+# atexit() can be called from one of these functions (even the last)
+function atexit_now() {
+       while(count($GLOBALS['wfpl_atexit_queue'])) {
+               list($func, $args) = array_shift($GLOBALS['wfpl_atexit_queue']);
+               call_user_func_array($func, $args);
+       }
+}
+
+# for those cases where display_messages() and session_save_messages() don't
+# get called automatically by wfpl:
+register_shutdown_function("atexit_now");
index 1abd50b..51eb4d3 100644 (file)
--- a/http.php
+++ b/http.php
@@ -83,6 +83,9 @@ function redirect($url, $status = '302 Moved Temporarily', $message = '') {
        }
                        
        if(function_exists('session_save_messages')) {
+               if(function_exists('atexit_now')) {
+                       atexit_now();
+               }
                session_save_messages();
        }
 
diff --git a/run.php b/run.php
index 60e4eab..1561446 100644 (file)
--- a/run.php
+++ b/run.php
@@ -158,14 +158,13 @@ function run_php($dest = false) {
                        $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template'];
                }
 
-               # If you have a site-wide template (template.html) then messages will
-               # be displayed there. If you instead want messages displayed on your
-               # page, call display_messages() from your page_main().
-               #
-               # Either way, you'll need to require_once('code/wfpl/messages.php')
-               # or require_once('code/wfpl/session_messages.php'). code/config.php
-               # is a nice place to do this.
+
+               # You'll probably want to require_once('code/wfpl/messages.php') or
+               # require_once('code/wfpl/session_messages.php') in code/config.php
                if(function_exists('display_messages')) {
+                       if(function_exists('atexit_now')) {
+                               atexit_now();
+                       }
                        display_messages();
                }