Explorar el Código

Give possibility to register a new Controller.

- Add a Extension->registerController(name) method
- Controllers must be written in extension_dir/controllers/nameController.php
- Controllers must be named as FreshExtension_name_Controller
- Controllers must extend Minz_ActionController

See https://github.com/FreshRSS/FreshRSS/issues/252
Marien Fressinaud hace 11 años
padre
commit
f8aa66152f
Se han modificado 2 ficheros con 57 adiciones y 3 borrados
  1. 44 3
      lib/Minz/Dispatcher.php
  2. 13 0
      lib/Minz/Extension.php

+ 44 - 3
lib/Minz/Dispatcher.php

@@ -15,6 +15,7 @@ class Minz_Dispatcher {
 	/* singleton */
 	private static $instance = null;
 	private static $needsReset;
+	private static $registrations = array();
 
 	private $controller;
 
@@ -38,7 +39,7 @@ class Minz_Dispatcher {
 			self::$needsReset = false;
 
 			try {
-				$this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
+				$this->createController (Minz_Request::controllerName ());
 				$this->controller->init ();
 				$this->controller->firstAction ();
 				if (!self::$needsReset) {
@@ -73,8 +74,11 @@ class Minz_Dispatcher {
 	 *          > pas une instance de ActionController
 	 */
 	private function createController ($controller_name) {
-		$filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
-		          . $controller_name . '.php';
+		if (self::isRegistered($controller_name)) {
+			$controller_name = self::loadController($controller_name);
+		} else {
+			$controller_name = 'FreshRSS_' . $controller_name . '_Controller';
+		}
 
 		if (!class_exists ($controller_name)) {
 			throw new Minz_ControllerNotExistException (
@@ -114,4 +118,41 @@ class Minz_Dispatcher {
 			$action_name
 		));
 	}
+
+	/**
+	 * 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.
+	 */
+	public static function registerController($base_name, $controller_name, $filename) {
+		if (file_exists($filename)) {
+			self::$registrations[$base_name] = array(
+				$controller_name,
+				$filename,
+			);
+		}
+	}
+
+	/**
+	 * Return if a controller is registered.
+	 *
+	 * @param $base_name the base name of the controller.
+	 * @return true if the controller has been registered, false else.
+	 */
+	public static function isRegistered($base_name) {
+		return isset(self::$registrations[$base_name]);
+	}
+
+	/**
+	 * Load a controller file (include) and return its name.
+	 *
+	 * @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;
+	}
 }

+ 13 - 0
lib/Minz/Extension.php

@@ -136,4 +136,17 @@ class Minz_Extension {
 		       '&amp;' . $mtime;
 		return Minz_Url::display($url);
 	}
+
+	/**
+	 * Register a controller in the Dispatcher.
+	 *
+	 * @param @base_name the base name of the controller. Final name will be:
+	 *                   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);
+	}
 }