diff --git a/components/com_contact/router.php b/components/com_contact/router.php index 7d981365a6da2..6fd7610fc83a5 100644 --- a/components/com_contact/router.php +++ b/components/com_contact/router.php @@ -30,18 +30,16 @@ public function build(&$query) $segments = array(); // Get a menu item based on Itemid or currently active - $app = JFactory::getApplication(); - $menu = $app->getMenu(); $params = JComponentHelper::getParams('com_contact'); $advanced = $params->get('sef_advanced_link', 0); if (empty($query['Itemid'])) { - $menuItem = $menu->getActive(); + $menuItem = $this->menu->getActive(); } else { - $menuItem = $menu->getItem($query['Itemid']); + $menuItem = $this->menu->getItem($query['Itemid']); } $mView = (empty($menuItem->query['view'])) ? null : $menuItem->query['view']; @@ -178,9 +176,7 @@ public function parse(&$segments) } // Get the active menu item. - $app = JFactory::getApplication(); - $menu = $app->getMenu(); - $item = $menu->getActive(); + $item = $this->menu->getActive(); $params = JComponentHelper::getParams('com_contact'); $advanced = $params->get('sef_advanced_link', 0); diff --git a/components/com_content/router.php b/components/com_content/router.php index 7791acfc98039..13a4560129bcb 100644 --- a/components/com_content/router.php +++ b/components/com_content/router.php @@ -30,20 +30,18 @@ public function build(&$query) $segments = array(); // Get a menu item based on Itemid or currently active - $app = JFactory::getApplication(); - $menu = $app->getMenu(); $params = JComponentHelper::getParams('com_content'); $advanced = $params->get('sef_advanced_link', 0); // We need a menu item. Either the one specified in the query, or the current active one if none specified if (empty($query['Itemid'])) { - $menuItem = $menu->getActive(); + $menuItem = $this->menu->getActive(); $menuItemGiven = false; } else { - $menuItem = $menu->getItem($query['Itemid']); + $menuItem = $this->menu->getItem($query['Itemid']); $menuItemGiven = true; } @@ -284,9 +282,7 @@ public function parse(&$segments) } // Get the active menu item. - $app = JFactory::getApplication(); - $menu = $app->getMenu(); - $item = $menu->getActive(); + $item = $this->menu->getActive(); $params = JComponentHelper::getParams('com_content'); $advanced = $params->get('sef_advanced_link', 0); $db = JFactory::getDbo(); diff --git a/components/com_finder/router.php b/components/com_finder/router.php index 2ad3ab7f288dd..bec821400e72a 100644 --- a/components/com_finder/router.php +++ b/components/com_finder/router.php @@ -27,16 +27,8 @@ class FinderRouter extends JComponentRouterBase */ public function build(&$query) { - static $menu; - $segments = array(); - // Load the menu if necessary. - if (!$menu) - { - $menu = JFactory::getApplication('site')->getMenu(); - } - /* * First, handle menu item routes first. When the menu system builds a * route, it only provides the option and the menu item id. We don't have @@ -57,7 +49,7 @@ public function build(&$query) if (!empty($query['Itemid'])) { // Get the menu item. - $item = $menu->getItem($query['Itemid']); + $item = $this->menu->getItem($query['Itemid']); // Check if the view matches. if ($item && @$item->query['view'] === @$query['view']) diff --git a/components/com_newsfeeds/router.php b/components/com_newsfeeds/router.php index cce98a416d59e..00a53e3b1a01c 100644 --- a/components/com_newsfeeds/router.php +++ b/components/com_newsfeeds/router.php @@ -30,18 +30,16 @@ public function build(&$query) $segments = array(); // Get a menu item based on Itemid or currently active - $app = JFactory::getApplication(); - $menu = $app->getMenu(); $params = JComponentHelper::getParams('com_newsfeeds'); $advanced = $params->get('sef_advanced_link', 0); if (empty($query['Itemid'])) { - $menuItem = $menu->getActive(); + $menuItem = $this->menu->getActive(); } else { - $menuItem = $menu->getItem($query['Itemid']); + $menuItem = $this->menu->getItem($query['Itemid']); } $mView = (empty($menuItem->query['view'])) ? null : $menuItem->query['view']; @@ -178,9 +176,7 @@ public function parse(&$segments) } // Get the active menu item. - $app = JFactory::getApplication(); - $menu = $app->getMenu(); - $item = $menu->getActive(); + $item = $this->menu->getActive(); $params = JComponentHelper::getParams('com_newsfeeds'); $advanced = $params->get('sef_advanced_link', 0); diff --git a/components/com_tags/router.php b/components/com_tags/router.php index ee484aa5df69f..e6a9493588721 100644 --- a/components/com_tags/router.php +++ b/components/com_tags/router.php @@ -30,19 +30,17 @@ public function build(&$query) $segments = array(); // Get a menu item based on Itemid or currently active - $app = JFactory::getApplication(); - $menu = $app->getMenu(); $params = JComponentHelper::getParams('com_tags'); $advanced = $params->get('sef_advanced_link', 0); // We need a menu item. Either the one specified in the query, or the current active one if none specified if (empty($query['Itemid'])) { - $menuItem = $menu->getActive(); + $menuItem = $this->menu->getActive(); } else { - $menuItem = $menu->getItem($query['Itemid']); + $menuItem = $this->menu->getItem($query['Itemid']); } $mView = (empty($menuItem->query['view'])) ? null : $menuItem->query['view']; @@ -144,9 +142,7 @@ public function parse(&$segments) } // Get the active menu item. - $app = JFactory::getApplication(); - $menu = $app->getMenu(); - $item = $menu->getActive(); + $item = $this->menu->getActive(); // Count route segments $count = count($segments); diff --git a/components/com_users/router.php b/components/com_users/router.php index 641fd4201f495..59ba43db5067f 100644 --- a/components/com_users/router.php +++ b/components/com_users/router.php @@ -43,9 +43,7 @@ public function build(&$query) if (empty($items)) { // Get all relevant menu items. - $app = JFactory::getApplication(); - $menu = $app->getMenu(); - $items = $menu->getItems('component', 'com_users'); + $items = $this->menu->getItems('component', 'com_users'); // Build an array of serialized query strings to menu item id mappings. for ($i = 0, $n = count($items); $i < $n; $i++) diff --git a/libraries/cms/component/router/base.php b/libraries/cms/component/router/base.php index ebe9f007be42e..f885761f59aa4 100644 --- a/libraries/cms/component/router/base.php +++ b/libraries/cms/component/router/base.php @@ -16,6 +16,51 @@ */ abstract class JComponentRouterBase implements JComponentRouterInterface { + /** + * Application object to use in the router + * + * @var JApplicationCms + * @since 3.4 + */ + public $app; + + /** + * Menu object to use in the router + * + * @var JMenu + * @since 3.4 + */ + public $menu; + + /** + * Class constructor. + * + * @param JApplicationCms $app Application-object that the router should use + * @param JMenu $menu Menu-object that the router should use + * + * @since 3.4 + */ + public function __construct($app = null, $menu = null) + { + if ($app) + { + $this->app = $app; + } + else + { + $this->app = JFactory::getApplication(); + } + + if ($menu) + { + $this->menu = $menu; + } + else + { + $this->menu = $this->app->getMenu(); + } + } + /** * Generic method to preprocess a URL *