Skip to content

Commit

Permalink
[imp] Routing: using the dependency injected $app and $menu in the co…
Browse files Browse the repository at this point in the history
…re routers. Fixes #5168

Changing default parameter from constructor from false to null
  • Loading branch information
Hackwar authored and phproberto committed Nov 27, 2014
1 parent 3fe5a4b commit eb91897
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 40 deletions.
10 changes: 3 additions & 7 deletions components/com_contact/router.php
Expand Up @@ -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'];
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 3 additions & 7 deletions components/com_content/router.php
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 1 addition & 9 deletions components/com_finder/router.php
Expand Up @@ -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
Expand All @@ -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'])
Expand Down
10 changes: 3 additions & 7 deletions components/com_newsfeeds/router.php
Expand Up @@ -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'];
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 3 additions & 7 deletions components/com_tags/router.php
Expand Up @@ -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'];
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions components/com_users/router.php
Expand Up @@ -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++)
Expand Down
45 changes: 45 additions & 0 deletions libraries/cms/component/router/base.php
Expand Up @@ -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
*
Expand Down

0 comments on commit eb91897

Please sign in to comment.