Skip to content

Commit

Permalink
Make routing work in console applications
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Dec 17, 2021
1 parent b5a0d58 commit ef15ffe
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions libraries/src/Application/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

use Joomla\CMS\Console;
use Joomla\CMS\Extension\ExtensionManagerTrait;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Language;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Router;
use Joomla\CMS\Version;
use Joomla\Console\Application;
use Joomla\DI\Container;
Expand Down Expand Up @@ -137,6 +139,9 @@ public function __construct(

// Set up the environment
$this->input->set('format', 'cli');

// Setting HOST to live site as it is needed when doing some routing
$_SERVER['HTTP_HOST'] = $this->get('live_site');
}

/**
Expand Down Expand Up @@ -434,4 +439,27 @@ public function setName(string $name): void
{
throw new \RuntimeException('The console application name cannot be changed');
}

/**
* Returns the application Router object.
*
* @param string $name The name of the application.
* @param array $options An optional associative array of configuration settings.
*
* @return Router
*
* @since __DEPLOY_VERSION__
*/
public static function getRouter($name = null, array $options = array())
{
if (!isset($name) || Factory::getApplication()->getName() === $name)
{
// Use site as default as there is no console router
$name = 'site';
}

$options['mode'] = Factory::getApplication()->get('sef');

return Router::getInstance($name, $options);
}
}

0 comments on commit ef15ffe

Please sign in to comment.