Browse Source

Add headers sent monitor.

Mark Scherer 11 years ago
parent
commit
0477d41710
1 changed files with 24 additions and 2 deletions
  1. 24 2
      src/Controller/Controller.php

+ 24 - 2
src/Controller/Controller.php

@@ -3,6 +3,7 @@ namespace Tools\Controller;
 
 use Cake\Controller\Controller as CakeController;
 use Cake\Core\Configure;
+use Cake\Event\Event;
 
 /**
  * DRY Controller stuff
@@ -10,7 +11,7 @@ use Cake\Core\Configure;
 class Controller extends CakeController {
 
 	/**
-	 * Add headers for IE8 etc to fix caching issues in those stupid browsers
+	 * Add headers for IE8 etc to fix caching issues in those stupid browsers.
 	 *
 	 * @return void
 	 */
@@ -24,7 +25,7 @@ class Controller extends CakeController {
 	/**
 	 * Handles automatic pagination of model records.
 	 *
-	 * @overwrite to support defaults like limit, querystring settings
+	 * @overwrite to support defaults like limit etc.
 	 * @param \Cake\ORM\Table|string|\Cake\ORM\Query $object Table to paginate
 	 *   (e.g: Table instance, 'TableName' or a Query object)
 	 * @return \Cake\ORM\ResultSet Query results
@@ -36,4 +37,25 @@ class Controller extends CakeController {
 		return parent::paginate($object);
 	}
 
+	/**
+	 * Hook to monitor headers being sent.
+	 *
+	 * This, if desired, adds a check if your controller actions are cleanly built and no headers
+	 * or output is being sent prior to the response class, which should be the only one doing this.
+	 *
+	 * @param Event $event An Event instance
+	 * @return void
+	 */
+	public function afterFilter(Event $event) {
+		if (Configure::read('App.monitorHeaders') && $this->name !== 'Error') {
+			if (headers_sent($filename, $linenum)) {
+				$message = sprintf('Headers already sent in %s on line %s', $filename, $linenum);
+				if (Configure::read('debug')) {
+					throw new \Exception($message);
+				}
+				trigger_error($message);
+			}
+		}
+	}
+
 }