Author: maksim_ka
Date: 2010-05-21 15:22:16 +0200 (Fri, 21 May 2010)
New Revision: 29566

Added:
   
plugins/sfErrorNotifierPlugin/trunk/config/sfErrorNotifierPluginConfiguration.class.php
   plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifierErrorHandler.php
Removed:
   plugins/sfErrorNotifierPlugin/trunk/config/config.php
Modified:
   plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifier.php
Log:
[sfErrorNotifier] ability to handle php errors including fatal, parse and so on.

Deleted: plugins/sfErrorNotifierPlugin/trunk/config/config.php
===================================================================
--- plugins/sfErrorNotifierPlugin/trunk/config/config.php       2010-05-21 
13:00:05 UTC (rev 29565)
+++ plugins/sfErrorNotifierPlugin/trunk/config/config.php       2010-05-21 
13:22:16 UTC (rev 29566)
@@ -1,3 +0,0 @@
-<?php
-
-$this->dispatcher->connect('application.throw_exception', 
array('sfErrorNotifier', 'notify'));

Added: 
plugins/sfErrorNotifierPlugin/trunk/config/sfErrorNotifierPluginConfiguration.class.php
===================================================================
--- 
plugins/sfErrorNotifierPlugin/trunk/config/sfErrorNotifierPluginConfiguration.class.php
                             (rev 0)
+++ 
plugins/sfErrorNotifierPlugin/trunk/config/sfErrorNotifierPluginConfiguration.class.php
     2010-05-21 13:22:16 UTC (rev 29566)
@@ -0,0 +1,12 @@
+<?php
+
+class sfErrorNotifierPluginConfiguration extends sfPluginConfiguration
+{
+  public function initialize()
+  {
+    $this->dispatcher->connect(
+      'application.throw_exception', array('sfErrorNotifier', 'notify'));
+    
+    sfErrorNotifierErrorHandler::start();
+  }
+}
\ No newline at end of file

Modified: plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifier.php
===================================================================
--- plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifier.php 2010-05-21 
13:00:05 UTC (rev 29565)
+++ plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifier.php 2010-05-21 
13:22:16 UTC (rev 29566)
@@ -17,29 +17,35 @@
 {
   static public function notify(sfEvent $event)
   {
+    return self::notifyException($event->getSubject());
+  }
+  
+  static public function notifyException($exception)
+  {
+    // it's not an error.
+    if ($exception instanceof sfStopException) {
+      return;
+    } 
     
     $to = sfConfig::get('app_sfErrorNotifier_emailTo');
-    if(! $to)
-    {
+    if(! $to) {
       // this environment is not set to notify exceptions
       return; 
     }
-       
-    $exception = $event->getSubject();
+    
     $context = sfContext::getInstance();
-       $env = 'n/a';
-    if ($conf = sfContext::getInstance()->getConfiguration())
-    {
+    $env = 'n/a';
+    if ($conf = sfContext::getInstance()->getConfiguration()) {
       $env = $conf->getEnvironment(); 
     }
-
+    
     $data = array();      
     $data['className'] = get_class($exception);
     $data['message'] = !is_null($exception->getMessage()) ? 
$exception->getMessage() : 'n/a';
     $data['moduleName'] = $context->getModuleName();
     $data['actionName'] = $context->getActionName();
     $data['uri'] = $context->getRequest()->getUri();
-       
+  
     $subject = "ERROR: {$_SERVER['HTTP_HOST']} Exception - $env - 
{$data['message']}";
     
     $mail = new sfErrorNotifierMail($subject, $data, $exception, $context);

Added: plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifierErrorHandler.php
===================================================================
--- plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifierErrorHandler.php     
                        (rev 0)
+++ plugins/sfErrorNotifierPlugin/trunk/lib/sfErrorNotifierErrorHandler.php     
2010-05-21 13:22:16 UTC (rev 29566)
@@ -0,0 +1,79 @@
+<?php
+
+/**
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ *
+ * @package    symfony
+ * @subpackage plugin
+ * @author     Maksim Kotlyar <[email protected]>
+ */
+class sfErrorNotifierErrorHandler
+{
+  /**
+   * @see handlePhpError
+   */
+       public static function start()
+       {
+               set_error_handler(array(__CLASS__,'handlePhpError'), E_ALL);
+               set_exception_handler(array(__CLASS__,'handleException'));
+               register_shutdown_function(array(__CLASS__, 
'handlePhpFatalError'));
+               
+    self::_reserveMemory();
+       }
+
+       /**
+        * 
+        * @param unknown_type $errno
+        * @param unknown_type $errstr
+        * @param unknown_type $errfile
+        * @param unknown_type $errline
+        * 
+        * @throws ErrorException
+        */
+       public static function handlePhpError($errno, $errstr, $errfile, 
$errline)
+       {
+         sfErrorNotifier::notifyException(
+          new ErrorException($errstr, 0, $errno, $errfile, $errline));
+       } 
+       
+       public static function handlePhpFatalError()
+       { 
+    $lastError = error_get_last();
+    if (is_null($lastError)) return;
+    
+    self::_freeMemory();
+    
+    $errors = array(
+      E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, 
+      E_COMPILE_ERROR, E_COMPILE_WARNING, E_STRICT);
+
+    if (in_array($lastError['type'], $errors)) {
+       sfErrorNotifier::notifyException(new ErrorException(
+         @$lastError['message'], @$lastError['type'], @$lastError['type'], 
+         @$lastError['file'], @$lastError['line']));
+    }
+       }
+       
+  public static function handleException($e)
+  {
+    sfErrorNotifier::notifyException($e);
+  }
+       
+       /**
+        * This is allows to catch memory limit fatal errors.
+        */
+       protected static function _reserveMemory()
+       {
+         $GLOBALS['tmp_buf'] = str_repeat('x', 1024 * 500);
+       }
+       
+       protected static function _freeMemory()
+       {
+         unset($GLOBALS['tmp_buf']);
+       }
+}
\ No newline at end of file

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to