Enabler PHP natives assertion in debug and use Platal::assert() as a
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 27 Feb 2011 18:38:13 +0000 (19:38 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sun, 27 Feb 2011 18:38:13 +0000 (19:38 +0100)
backend when assertions fail.

Thus, you can write the following code:
 assert ('$toto != blah')

If you run in debug mode, this will be evaluated and will raise an
exception in case of error. If you run in production mode, this will juste
be skipped.

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
classes/plglobals.php
include/platal.inc.php

index d168037..fd11a25 100644 (file)
@@ -135,6 +135,7 @@ class PlGlobals
             $this->baseurl      = @trim($base    .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
             $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
         }
+        assert_options(ASSERT_ACTIVE, $this->debug != 0);
 
         $this->setLocale();
     }
index 0f18429..900220c 100644 (file)
@@ -144,7 +144,14 @@ function pl_print_errors($html = false)
     }
 }
 
+function pl_assert_cb($file, $line, $message)
+{
+    Platal::assert(false, "Assertion failed at $file:$line with message: $message");
+}
+
 set_error_handler('pl_error_handler', E_ALL | E_STRICT);
+assert_options(ASSERT_CALLBACK, 'pl_assert_cb');
+assert_options(ASSERT_WARNING, false);
 if (php_sapi_name() == 'cli') {
     register_shutdown_function('pl_print_errors');
 }