Skip to content

Commit

Permalink
Rework how URLs are injected into applications
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonge committed Aug 21, 2022
1 parent b775fdb commit 0dee04c
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 24 deletions.
3 changes: 2 additions & 1 deletion administrator/includes/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
->alias('JSession', 'session.web.administrator')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.administrator')
->alias(\Joomla\Session\Session::class, 'session.web.administrator')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.administrator');
->alias(\Joomla\Session\SessionInterface::class, 'session.web.administrator')
->alias('application.active', 'JApplicationAdministrator');

// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\AdministratorApplication::class);
Expand Down
3 changes: 2 additions & 1 deletion api/includes/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
->alias('JSession', 'session.cli')
->alias(\Joomla\CMS\Session\Session::class, 'session.cli')
->alias(\Joomla\Session\Session::class, 'session.cli')
->alias(\Joomla\Session\SessionInterface::class, 'session.cli');
->alias(\Joomla\Session\SessionInterface::class, 'session.cli')
->alias('application.active', 'JApplicationApi');

// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\ApiApplication::class);
Expand Down
3 changes: 2 additions & 1 deletion cli/joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
->alias('JSession', 'session.cli')
->alias(\Joomla\CMS\Session\Session::class, 'session.cli')
->alias(\Joomla\Session\Session::class, 'session.cli')
->alias(\Joomla\Session\SessionInterface::class, 'session.cli');
->alias(\Joomla\Session\SessionInterface::class, 'session.cli')
->alias(\Joomla\Console\Application::class, 'application.active');

$app = \Joomla\CMS\Factory::getContainer()->get(\Joomla\Console\Application::class);
\Joomla\CMS\Factory::$application = $app;
Expand Down
3 changes: 2 additions & 1 deletion includes/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
->alias('JSession', 'session.web.site')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site')
->alias('application.active', 'JApplicationSite');

// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
Expand Down
3 changes: 2 additions & 1 deletion installation/includes/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@

// Get the dependency injection container
$container = \Joomla\CMS\Factory::getContainer();
$container->registerServiceProvider(new \Joomla\CMS\Installation\Service\Provider\Application());
$container->alias('application.active', \Joomla\CMS\Installation\Application\InstallationApplication::class)
->registerServiceProvider(new \Joomla\CMS\Installation\Service\Provider\Application());

/*
* Alias the session service keys to the web session service as that is the primary session backend for this application
Expand Down
10 changes: 5 additions & 5 deletions installation/src/Application/InstallationApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ public function __construct(Input $input = null, Registry $config = null, WebCli

// Register the config to Factory.
Factory::$config = $this->config;

// Set the root in the URI one level up.
$parts = explode('/', Uri::base(true));
array_pop($parts);
Uri::root(null, implode('/', $parts));
}

/**
Expand Down Expand Up @@ -232,6 +227,11 @@ protected function doExecute()
*/
public function execute()
{
// Set the root in the URI one level up.
$parts = explode('/', Uri::base(true));
array_pop($parts);
Uri::root(null, implode('/', $parts));

try {
// Perform application routines.
$this->doExecute();
Expand Down
16 changes: 13 additions & 3 deletions libraries/src/Application/AdministratorApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ public function __construct(Input $input = null, Registry $config = null, WebCli

// Execute the parent constructor
parent::__construct($input, $config, $client, $container);

// Set the root in the URI based on the application name
Uri::root(null, rtrim(\dirname(Uri::base(true)), '/\\'));
}

/**
Expand Down Expand Up @@ -484,4 +481,17 @@ public function findOption(): string

return $option;
}

/**
* Hook to allow applications to configure anything inside the static variables of \Joomla\CMS\Uri\Uri.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function configureBaseUrlForApplication(): void
{
// Set the root in the URI based on the application name
Uri::root(null, rtrim(\dirname(Uri::base(true)), '/\\'));
}
}
16 changes: 13 additions & 3 deletions libraries/src/Application/ApiApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ public function __construct(JInputJson $input = null, Registry $config = null, W

$this->addFormatMap('application/json', 'json');
$this->addFormatMap('application/vnd.api+json', 'jsonapi');

// Set the root in the URI based on the application name
Uri::root(null, str_ireplace('/' . $this->getName(), '', Uri::base(true)));
}


Expand Down Expand Up @@ -405,4 +402,17 @@ public function dispatch($component = null)
PluginHelper::importPlugin('system');
$this->triggerEvent('onAfterDispatch');
}

/**
* Hook to allow applications to configure anything inside the static variables of \Joomla\CMS\Uri\Uri.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function configureBaseUrlForApplication(): void
{
// Set the root in the URI based on the application name
Uri::root(null, rtrim(\dirname(Uri::base(true)), '/\\'));
}
}
12 changes: 12 additions & 0 deletions libraries/src/Application/CMSApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ function ($systemVariable) use ($input) {
public function execute()
{
try {
$this->configureBaseUrlForApplication();
$this->sanityCheckSystemVariables();
$this->setupLogging();
$this->createExtensionNamespaceMap();
Expand Down Expand Up @@ -1284,4 +1285,15 @@ public function setMenuFactory(MenuFactoryInterface $menuFactory): void
{
$this->menuFactory = $menuFactory;
}

/**
* Hook to allow applications to configure anything inside the static variables of \Joomla\CMS\Uri\Uri.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function configureBaseUrlForApplication(): void
{
}
}
4 changes: 4 additions & 0 deletions libraries/src/Application/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public function __construct(
->alias(\Joomla\Session\SessionInterface::class, 'session.cli');
}

if (!$container->has('application.active')) {
$container->alias('application.active', static::class);
}

$this->input = new \Joomla\CMS\Input\Cli();
$this->language = Factory::getLanguage();
$this->output = $output ?: new Stdout();
Expand Down
9 changes: 1 addition & 8 deletions libraries/src/Application/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,7 @@ protected function populateHttpHost()
$uri = Uri::getInstance('https://joomla.invalid/set/by/console/application');
}

/**
* Yes, this is icky but it is the only way to trick WebApplication into compliance.
*
* @see \Joomla\Application\AbstractWebApplication::detectRequestUri
*/
$_SERVER['HTTP_HOST'] = $uri->toString(['host', 'port']);
$_SERVER['REQUEST_URI'] = $uri->getPath();
$_SERVER['HTTPS'] = $uri->getScheme() === 'https' ? 'on' : 'off';
$this->set('uri.request', $uri->toString());
}

/**
Expand Down
10 changes: 10 additions & 0 deletions libraries/src/Uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public static function getInstance($uri = 'SERVER')
if (empty(static::$instances[$uri])) {
// Are we obtaining the URI from the server?
if ($uri === 'SERVER') {
$applicationUriRequest = Factory::getContainer()->get('application.active')->get('uri.request');

if ($applicationUriRequest !== null) {
static::$instances[$uri] = new static($applicationUriRequest);

return static::$instances[$uri];
}

@trigger_error('The application should provide the request URI from Joomla 5.0.0', E_USER_DEPRECATED);

// Determine if the request was over SSL (HTTPS).
if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) !== 'off')) {
$https = 's://';
Expand Down

0 comments on commit 0dee04c

Please sign in to comment.