Преглед изворни кода

Minz: remove one layer of ob_ (experimental)

https://github.com/marienfressinaud/FreshRSS/issues/303#issuecomment-38351311
https://github.com/marienfressinaud/FreshRSS/issues/163
* Remove Minz_Response (not needed anymore)
* Move Minz_Request::reseted to Minz_Dispatcher::reset()
Alexandre Alapetite пре 12 година
родитељ
комит
5081ffaf39

+ 4 - 10
app/Controllers/importExportController.php

@@ -65,7 +65,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 			$export_all = Minz_Request::param('export_all', false);
 
 			// code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
-			$file = tempnam("tmp", "zip");
+			$file = tempnam('tmp', 'zip');
 			$zip = new ZipArchive();
 			$zip->open($file, ZipArchive::OVERWRITE);
 
@@ -101,17 +101,11 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		}
 
 		$this->view->categories = $list;
-
-		// TODO: add a parameter to renderHelper in order to get a variable
-		ob_start();
-		$this->view->renderHelper('export/opml');
-		return ob_get_clean();
+		return $this->view->helperToString('export/opml');
 	}
 
 	private function generate_articles($type) {
-		// TODO: same here + we should get articles according to $type
-		ob_start();
-		$this->view->renderHelper('export/articles');
-		return ob_get_clean();
+		// TODO: we should get articles according to $type
+		return $this->view->helperToString('export/articles');
 	}
 }

+ 0 - 1
app/actualize_script.php

@@ -28,7 +28,6 @@ foreach ($users as $myUser) {
 	$_SERVER['HTTP_HOST'] = '';
 
 	$freshRSS = new FreshRSS();
-	$freshRSS->_useOb(false);
 
 	Minz_Configuration::_authType('none');
 

+ 32 - 41
lib/Minz/Dispatcher.php

@@ -14,6 +14,7 @@ class Minz_Dispatcher {
 
 	/* singleton */
 	private static $instance = null;
+	private static $needsReset;
 
 	private $router;
 	private $controller;
@@ -40,44 +41,36 @@ class Minz_Dispatcher {
 	 * Remplit le body de Response à partir de la Vue
 	 * @exception Minz_Exception
 	 */
-	public function run ($ob = true) {
-		// Le ob_start est dupliqué : sans ça il y a un bug sous Firefox
-		// ici on l'appelle avec 'ob_gzhandler', après sans.
-		// Vraisemblablement la compression fonctionne mais c'est sale
-		// J'ignore les effets de bord :(
-		if ($ob) {
-			ob_start ('ob_gzhandler');
-		}
-
-		$text = '';	//TODO: Clean this code
-		while (Minz_Request::$reseted) {
-			Minz_Request::$reseted = false;
+	public function run () {
+		do {
+			self::$needsReset = false;
 
 			try {
 				$this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
 				$this->controller->init ();
 				$this->controller->firstAction ();
-				$this->launchAction (
-					Minz_Request::actionName ()
-					. 'Action'
-				);
+				if (!self::$needsReset) {
+					$this->launchAction (
+						Minz_Request::actionName ()
+						. 'Action'
+					);
+				}
 				$this->controller->lastAction ();
 
-				if (!Minz_Request::$reseted) {
-					if ($ob) {
-						ob_start ();
-					}
-					$this->controller->view ()->build ();
-					if ($ob) {
-						$text = ob_get_clean();
-					}
+				if (!self::$needsReset) {
+					echo $this->controller->view ()->build ();
 				}
 			} catch (Minz_Exception $e) {
 				throw $e;
 			}
-		}
+		} while (self::$needsReset);
+	}
 
-		Minz_Response::setBody ($text);
+	/**
+	 * Informe le contrôleur qu'il doit recommancer car la requête a été modifiée
+	 */
+	public static function reset() {
+		self::$needsReset = true;
 	}
 
 	/**
@@ -114,21 +107,19 @@ class Minz_Dispatcher {
 	 *  le controller
 	 */
 	private function launchAction ($action_name) {
-		if (!Minz_Request::$reseted) {
-			if (!is_callable (array (
-				$this->controller,
-				$action_name
-			))) {
-				throw new Minz_ActionException (
-					get_class ($this->controller),
-					$action_name,
-					Minz_Exception::ERROR
-				);
-			}
-			call_user_func (array (
-				$this->controller,
-				$action_name
-			));
+		if (!is_callable (array (
+			$this->controller,
+			$action_name
+		))) {
+			throw new Minz_ActionException (
+				get_class ($this->controller),
+				$action_name,
+				Minz_Exception::ERROR
+			);
 		}
+		call_user_func (array (
+			$this->controller,
+			$action_name
+		));
 	}
 }

+ 24 - 8
lib/Minz/Error.php

@@ -23,13 +23,32 @@ class Minz_Error {
 		$logs = self::processLogs ($logs);
 		$error_filename = APP_PATH . '/Controllers/errorController.php';
 
+		switch ($code) {
+			case 200 :
+				header('HTTP/1.1 200 OK');
+				break;
+			case 403 :
+				header('HTTP/1.1 403 Forbidden');
+				break;
+			case 404 :
+				header('HTTP/1.1 404 Not Found');
+				break;
+			case 500 :
+				header('HTTP/1.1 500 Internal Server Error');
+				break;
+			case 503 :
+				header('HTTP/1.1 503 Service Unavailable');
+				break;
+			default :
+				header('HTTP/1.1 500 Internal Server Error');
+		}
+
 		if (file_exists ($error_filename)) {
 			$params = array (
 				'code' => $code,
 				'logs' => $logs
 			);
 
-			Minz_Response::setHeader ($code);
 			if ($redirect) {
 				Minz_Request::forward (array (
 					'c' => 'error'
@@ -41,19 +60,16 @@ class Minz_Error {
 				), false);
 			}
 		} else {
-			$text = '<h1>An error occured</h1>'."\n";
+			echo '<h1>An error occured</h1>' . "\n";
 
 			if (!empty ($logs)) {
-				$text .= '<ul>'."\n";
+				echo '<ul>' . "\n";
 				foreach ($logs as $log) {
-					$text .= '<li>' . $log . '</li>'."\n";
+					echo '<li>' . $log . '</li>' . "\n";
 				}
-				$text .= '</ul>'."\n";
+				echo '</ul>' . "\n";
 			}
 
-			Minz_Response::setHeader ($code);
-			Minz_Response::setBody ($text);
-			Minz_Response::send ();
 			exit ();
 		}
 	}

+ 1 - 15
lib/Minz/FrontController.php

@@ -26,8 +26,6 @@ class Minz_FrontController {
 	protected $dispatcher;
 	protected $router;
 
-	private $useOb = true;
-
 	/**
 	 * Constructeur
 	 * Initialise le router et le dispatcher
@@ -63,8 +61,7 @@ class Minz_FrontController {
 	 */
 	public function run () {
 		try {
-			$this->dispatcher->run ($this->useOb);
-			Minz_Response::send ();
+			$this->dispatcher->run();
 		} catch (Minz_Exception $e) {
 			try {
 				Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
@@ -96,15 +93,4 @@ class Minz_FrontController {
 		}
 		exit ('### Application problem ###<br />'."\n".$txt);
 	}
-
-	public function useOb() {
-		return $this->useOb;
-	}
-
-	/**
-	 * Use ob_start('ob_gzhandler') or not.
-	 */
-	public function _useOb($ob) {
-		return $this->useOb = (bool)$ob;
-	}
 }

+ 1 - 4
lib/Minz/Request.php

@@ -15,8 +15,6 @@ class Minz_Request {
 	private static $default_controller_name = 'index';
 	private static $default_action_name = 'index';
 
-	public static $reseted = true;
-
 	/**
 	 * Getteurs
 	 */
@@ -137,14 +135,13 @@ class Minz_Request {
 			header ('Location: ' . Minz_Url::display ($url, 'php'));
 			exit ();
 		} else {
-			self::$reseted = true;
-
 			self::_controllerName ($url['c']);
 			self::_actionName ($url['a']);
 			self::_params (array_merge (
 				self::$params,
 				$url['params']
 			));
+			Minz_Dispatcher::reset();
 		}
 	}
 

+ 0 - 60
lib/Minz/Response.php

@@ -1,60 +0,0 @@
-<?php
-/** 
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
-*/
-
-/**
- * Response représente la requête http renvoyée à l'utilisateur
- */
-class Minz_Response {
-	private static $header = 'HTTP/1.0 200 OK';
-	private static $body = '';
-	
-	/**
-	 * Mets à jour le body de la Response
-	 * @param $text le texte à incorporer dans le body
-	 */
-	public static function setBody ($text) {
-		self::$body = $text;
-	}
-	
-	/**
-	 * Mets à jour le header de la Response
-	 * @param $code le code HTTP, valeurs possibles
-	 *	- 200 (OK)
-	 *	- 403 (Forbidden)
-	 *	- 404 (Forbidden)
-	 *	- 500 (Forbidden) -> par défaut si $code erroné
-	 *	- 503 (Forbidden)
-	 */
-	public static function setHeader ($code) {
-		switch ($code) {
-		case 200 :
-			self::$header = 'HTTP/1.0 200 OK';
-			break;
-		case 403 :
-			self::$header = 'HTTP/1.0 403 Forbidden';
-			break;
-		case 404 :
-			self::$header = 'HTTP/1.0 404 Not Found';
-			break;
-		case 500 :
-			self::$header = 'HTTP/1.0 500 Internal Server Error';
-			break;
-		case 503 :
-			self::$header = 'HTTP/1.0 503 Service Unavailable';
-			break;
-		default :
-			self::$header = 'HTTP/1.0 500 Internal Server Error';
-		}
-	}
-
-	/**
-	 * Envoie la Response à l'utilisateur
-	 */
-	public static function send () {
-		header (self::$header);
-		echo self::$body;
-	}
-}

+ 10 - 0
lib/Minz/View.php

@@ -102,6 +102,16 @@ class Minz_View {
 		}
 	}
 
+	/**
+	 * Retourne renderHelper() dans une chaîne
+	 * @param $helper l'élément à traîter
+	 */
+	public function helperToString($helper) {
+		ob_start();
+		renderHelper($helper);
+		return ob_get_clean();
+	}
+
 	/**
 	 * Permet de choisir si on souhaite utiliser le layout
 	 * @param $use true si on souhaite utiliser le layout, false sinon

+ 1 - 1
lib/lib_rss.php

@@ -27,7 +27,7 @@ function classAutoloader($class) {
 				include(APP_PATH . '/Models/' . $components[1] . '.php');
 				return;
 			case 3:	//Controllers, Exceptions
-				include(APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php');
+				@include(APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php');
 				return;
 		}
 	} elseif (strpos($class, 'Minz') === 0) {