Przeglądaj źródła

Separate views registration from controllers one.

- Add an Extension->registerViews() method.
- Views are first searched in extension paths, then in APP_PATH.
- It gives a way to override easily existing controllers / views.
- Change include into an include_once in Dispatcher for new controllers.

See https://github.com/FreshRSS/FreshRSS/issues/252
Marien Fressinaud 11 lat temu
rodzic
commit
a08c382e06
3 zmienionych plików z 32 dodań i 9 usunięć
  1. 1 5
      lib/Minz/Dispatcher.php
  2. 7 0
      lib/Minz/Extension.php
  3. 24 4
      lib/Minz/View.php

+ 1 - 5
lib/Minz/Dispatcher.php

@@ -95,10 +95,6 @@ class Minz_Dispatcher {
 				Minz_Exception::ERROR
 			);
 		}
-
-		if (self::isRegistered($base_name)) {
-			$this->setViewPath($this->controller, $base_name);
-		}
 	}
 
 	/**
@@ -154,7 +150,7 @@ class Minz_Dispatcher {
 	private static function loadController($base_name) {
 		$base_path = self::$registrations[$base_name];
 		$controller_filename = $base_path . '/controllers/' . $base_name . 'Controller.php';
-		include($controller_filename);
+		include_once $controller_filename;
 	}
 
 	private static function setViewPath($controller, $base_name) {

+ 7 - 0
lib/Minz/Extension.php

@@ -146,4 +146,11 @@ class Minz_Extension {
 	public function registerController($base_name) {
 		Minz_Dispatcher::registerController($base_name, $this->path);
 	}
+
+	/**
+	 * Register the views in order to be accessible by the application.
+	 */
+	public function registerViews() {
+		Minz_View::addBasePathname($this->path);
+	}
 }

+ 24 - 4
lib/Minz/View.php

@@ -12,10 +12,10 @@ 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;
 
+	private static $base_pathnames = array(APP_PATH);
 	private static $title = '';
 	private static $styles = array ();
 	private static $scripts = array ();
@@ -41,8 +41,15 @@ class Minz_View {
 		                     . $action_name . '.phtml';
 	}
 
-	public function setBasePathname($base_pathname) {
-		$this->base_pathname = $base_pathname;
+	/**
+	 * Add a base pathname to search views.
+	 *
+	 * New pathnames will be added at the beginning of the list.
+	 *
+	 * @param $base_pathname the new base pathname.
+	 */
+	public static function addBasePathname($base_pathname) {
+		array_unshift(self::$base_pathnames, $base_pathname);
 	}
 
 	/**
@@ -74,7 +81,20 @@ class Minz_View {
 	 * Affiche la Vue en elle-même
 	 */
 	public function render () {
-		if ((include($this->base_pathname . $this->view_filename)) === false) {
+		$view_found = false;
+
+		// We search the view in the list of base pathnames. Only the first view
+		// found is considered.
+		foreach (self::$base_pathnames as $base) {
+			$filename = $base . $this->view_filename;
+			if (file_exists($filename)) {
+				include $filename;
+				$view_found = true;
+				break;
+			}
+		}
+
+		if (!$view_found) {
 			Minz_Log::notice('File not found: `' . $this->view_filename . '`');
 		}
 	}