瀏覽代碼

Extensions can define new views

- View base pathname is set to the extension directory
- An extension can now override an existing controller / view

See https://github.com/FreshRSS/FreshRSS/issues/252
Marien Fressinaud 11 年之前
父節點
當前提交
c6a682deb9
共有 3 個文件被更改,包括 31 次插入24 次删除
  1. 23 17
      lib/Minz/Dispatcher.php
  2. 1 4
      lib/Minz/Extension.php
  3. 7 3
      lib/Minz/View.php

+ 23 - 17
lib/Minz/Dispatcher.php

@@ -68,16 +68,17 @@ class Minz_Dispatcher {
 
 	/**
 	 * Instancie le Controller
-	 * @param $controller_name le nom du controller à instancier
+	 * @param $base_name le nom du controller à instancier
 	 * @exception ControllerNotExistException le controller n'existe pas
 	 * @exception ControllerNotActionControllerException controller n'est
 	 *          > pas une instance de ActionController
 	 */
-	private function createController ($controller_name) {
-		if (self::isRegistered($controller_name)) {
-			$controller_name = self::loadController($controller_name);
+	private function createController ($base_name) {
+		if (self::isRegistered($base_name)) {
+			self::loadController($base_name);
+			$controller_name = 'FreshExtension_' . $base_name . '_Controller';
 		} else {
-			$controller_name = 'FreshRSS_' . $controller_name . '_Controller';
+			$controller_name = 'FreshRSS_' . $base_name . '_Controller';
 		}
 
 		if (!class_exists ($controller_name)) {
@@ -94,6 +95,10 @@ class Minz_Dispatcher {
 				Minz_Exception::ERROR
 			);
 		}
+
+		if (self::isRegistered($base_name)) {
+			$this->setViewPath($this->controller, $base_name);
+		}
 	}
 
 	/**
@@ -123,15 +128,11 @@ class Minz_Dispatcher {
 	 * Register a controller file.
 	 *
 	 * @param $base_name the base name of the controller (i.e. ./?c=<base_name>)
-	 * @param $controller_name the name of the controller (e.g. HelloWorldController).
-	 * @param $filename the file which contains the controller.
+	 * @param $base_path the base path where we should look into to find info.
 	 */
-	public static function registerController($base_name, $controller_name, $filename) {
-		if (file_exists($filename)) {
-			self::$registrations[$base_name] = array(
-				$controller_name,
-				$filename,
-			);
+	public static function registerController($base_name, $base_path) {
+		if (!self::isRegistered($base_name)) {
+			self::$registrations[$base_name] = $base_path;
 		}
 	}
 
@@ -146,13 +147,18 @@ class Minz_Dispatcher {
 	}
 
 	/**
-	 * Load a controller file (include) and return its name.
+	 * Load a controller file (include).
 	 *
 	 * @param $base_name the base name of the controller.
 	 */
 	private static function loadController($base_name) {
-		list($controller_name, $filename) = self::$registrations[$base_name];
-		include($filename);
-		return $controller_name;
+		$base_path = self::$registrations[$base_name];
+		$controller_filename = $base_path . '/controllers/' . $base_name . 'Controller.php';
+		include($controller_filename);
+	}
+
+	private static function setViewPath($controller, $base_name) {
+		$base_path = self::$registrations[$base_name];
+		$controller->view()->setBasePathname($base_path);
 	}
 }

+ 1 - 4
lib/Minz/Extension.php

@@ -144,9 +144,6 @@ class Minz_Extension {
 	 *                   FreshExtension_<base_name>_Controller.
 	 */
 	public function registerController($base_name) {
-		$controller_name = 'FreshExtension_' . $base_name . '_Controller';
-		$filename = $this->path . '/controllers/' . $base_name . 'Controller.php';
-
-		Minz_Dispatcher::registerController($base_name, $controller_name, $filename);
+		Minz_Dispatcher::registerController($base_name, $this->path);
 	}
 }

+ 7 - 3
lib/Minz/View.php

@@ -12,6 +12,7 @@ class Minz_View {
 	const LAYOUT_PATH_NAME = '/layout';
 	const LAYOUT_FILENAME = '/layout.phtml';
 
+	private $base_pathname = APP_PATH;
 	private $view_filename = '';
 	private $use_layout = null;
 
@@ -35,12 +36,15 @@ class Minz_View {
 	 * Change le fichier de vue en fonction d'un controller / action
 	 */
 	public function change_view($controller_name, $action_name) {
-		$this->view_filename = APP_PATH
-		                     . self::VIEWS_PATH_NAME . '/'
+		$this->view_filename = self::VIEWS_PATH_NAME . '/'
 		                     . $controller_name . '/'
 		                     . $action_name . '.phtml';
 	}
 
+	public function setBasePathname($base_pathname) {
+		$this->base_pathname = $base_pathname;
+	}
+
 	/**
 	 * Construit la vue
 	 */
@@ -70,7 +74,7 @@ class Minz_View {
 	 * Affiche la Vue en elle-même
 	 */
 	public function render () {
-		if ((include($this->view_filename)) === false) {
+		if ((include($this->base_pathname . $this->view_filename)) === false) {
 			Minz_Log::notice('File not found: `' . $this->view_filename . '`');
 		}
 	}