Reuse single router instance instead of repeatedly creating it
Reuse single router instance instead of repeatedly creating it

--- a/include/Router.class.php
+++ b/include/Router.class.php
@@ -599,6 +599,8 @@
 			$controller->SetParam($paramname, $queryval);
 		}
 
+		$controller->SetRouter($this);
+
 		return $controller;
 	}
 
@@ -632,6 +634,8 @@
 
 			$controller->SetParam($paramname, $queryval);
 		}
+
+		$controller->SetRouter($this);
 
 		return $controller;
 	}

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -86,6 +86,13 @@
 	 * @var GitPHP_GitExe
 	 */
 	protected $exe;
+
+	/**
+	 * Url router instance
+	 *
+	 * @var GitPHP_Router
+	 */
+	protected $router;
 
 	/**
 	 * Initialize controller
@@ -276,6 +283,16 @@
 	}
 
 	/**
+	 * Set router instance
+	 *
+	 * @param GitPHP_Router $router router
+	 */
+	public function SetRouter($router)
+	{
+		$this->router = $router;
+	}
+
+	/**
 	 * Get config instance
 	 *
 	 * @return GitPHP_Config
@@ -534,6 +551,10 @@
 		if ($this->config->GetValue('cleanurl')) {
 			$this->tpl->assign('cleanurl', true);
 		}
+		if ($this->router) {
+			$this->router->SetCleanUrl($this->config->GetValue('cleanurl') ? true : false);
+			$this->tpl->assign('router', $this->router);
+		}
 
 		$getvars = explode('&', $_SERVER['QUERY_STRING']);
 		$getvarsmapped = array();

--- a/include/smartyplugins/function.geturl.php
+++ b/include/smartyplugins/function.geturl.php
@@ -37,10 +37,11 @@
 	unset($params['escape']);
 
 	$abbreviate = $template->getTemplateVars('abbreviateurl');
-
-	$clean = $template->getTemplateVars('cleanurl');
-
-	$router = new GitPHP_Router($clean);
+	$router = $template->getTemplateVars('router');
+	if (!$router) {
+		$clean = $template->getTemplateVars('cleanurl');
+		$router = new GitPHP_Router($clean);
+	}
 	$fullurl = $router->GetUrl($url, $params, $abbreviate);
 	if ($escape)
 		$fullurl = htmlspecialchars($fullurl);

comments