Skip to content

Commit dac4c55

Browse files
authored
[5.1] Implement onAfterInitialiseRouter event (#42692)
* Implement onAfterInitialiseRouter event * Update libraries/src/Event/Router/RouterEvent.php
1 parent ae47b89 commit dac4c55

File tree

5 files changed

+141
-8
lines changed

5 files changed

+141
-8
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Router;
11+
12+
// phpcs:disable PSR1.Files.SideEffects
13+
14+
\defined('_JEXEC') or die;
15+
// phpcs:enable PSR1.Files.SideEffects
16+
17+
/**
18+
* Event class for AfterInitialiseRouter event
19+
*
20+
* @since __DEPLOY_VERSION__
21+
*/
22+
class AfterInitialiseRouterEvent extends RouterEvent
23+
{
24+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Router;
11+
12+
use Joomla\CMS\Event\AbstractImmutableEvent;
13+
use Joomla\CMS\Router\Router;
14+
15+
// phpcs:disable PSR1.Files.SideEffects
16+
\defined('_JEXEC') or die;
17+
// phpcs:enable PSR1.Files.SideEffects
18+
19+
/**
20+
* Class for Application's Router events
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
abstract class RouterEvent extends AbstractImmutableEvent
25+
{
26+
/**
27+
* Constructor.
28+
*
29+
* @param string $name The event name.
30+
* @param array $arguments The event arguments.
31+
*
32+
* @throws \BadMethodCallException
33+
*
34+
* @since __DEPLOY_VERSION__
35+
*/
36+
public function __construct($name, array $arguments = [])
37+
{
38+
if (!\array_key_exists('router', $arguments)) {
39+
throw new \BadMethodCallException("Argument 'router' of event {$name} is required but has not been provided");
40+
}
41+
42+
parent::__construct($name, $arguments);
43+
}
44+
45+
/**
46+
* Setter for the router argument.
47+
*
48+
* @param Router $value The value to set
49+
*
50+
* @return Router
51+
*
52+
* @since __DEPLOY_VERSION__
53+
*/
54+
protected function onSetRouter(Router $value): Router
55+
{
56+
return $value;
57+
}
58+
59+
/**
60+
* Get the event's router object
61+
*
62+
* @return Router
63+
*
64+
* @since __DEPLOY_VERSION__
65+
*/
66+
public function getRouter(): Router
67+
{
68+
return $this->arguments['router'];
69+
}
70+
}

libraries/src/Service/Provider/Router.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
use Joomla\CMS\Application\ApiApplication;
1313
use Joomla\CMS\Application\SiteApplication;
14+
use Joomla\CMS\Event\Router\AfterInitialiseRouterEvent;
1415
use Joomla\CMS\Router\AdministratorRouter;
1516
use Joomla\CMS\Router\ApiRouter;
1617
use Joomla\CMS\Router\SiteRouter;
1718
use Joomla\DI\Container;
1819
use Joomla\DI\ServiceProviderInterface;
20+
use Joomla\Event\DispatcherInterface;
1921

2022
// phpcs:disable PSR1.Files.SideEffects
2123
\defined('_JEXEC') or die;
@@ -44,7 +46,12 @@ public function register(Container $container)
4446
->share(
4547
SiteRouter::class,
4648
function (Container $container) {
47-
return new SiteRouter($container->get(SiteApplication::class));
49+
$router = new SiteRouter($container->get(SiteApplication::class));
50+
$container->get(DispatcherInterface::class)->dispatch(
51+
'onAfterInitialiseRouter',
52+
new AfterInitialiseRouterEvent('onAfterInitialiseRouter', ['router' => $router])
53+
);
54+
return $router;
4855
},
4956
true
5057
);

plugins/system/languagefilter/services/provider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Joomla\CMS\Factory;
1515
use Joomla\CMS\Language\LanguageFactoryInterface;
1616
use Joomla\CMS\Plugin\PluginHelper;
17-
use Joomla\CMS\Router\SiteRouter;
1817
use Joomla\DI\Container;
1918
use Joomla\DI\ServiceProviderInterface;
2019
use Joomla\Event\DispatcherInterface;
@@ -41,7 +40,6 @@ function (Container $container) {
4140
Factory::getApplication(),
4241
$container->get(LanguageFactoryInterface::class)
4342
);
44-
$plugin->setSiteRouter($container->get(SiteRouter::class));
4543

4644
return $plugin;
4745
}

plugins/system/languagefilter/src/Extension/LanguageFilter.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Joomla\CMS\Application\CMSApplicationInterface;
1515
use Joomla\CMS\Association\AssociationServiceInterface;
1616
use Joomla\CMS\Component\ComponentHelper;
17+
use Joomla\CMS\Event\Router\AfterInitialiseRouterEvent;
1718
use Joomla\CMS\Factory;
1819
use Joomla\CMS\Filesystem\Folder;
1920
use Joomla\CMS\Language\Associations;
@@ -23,10 +24,12 @@
2324
use Joomla\CMS\Plugin\CMSPlugin;
2425
use Joomla\CMS\Router\Route;
2526
use Joomla\CMS\Router\Router;
27+
use Joomla\CMS\Router\SiteRouter;
2628
use Joomla\CMS\Router\SiteRouterAwareTrait;
2729
use Joomla\CMS\Uri\Uri;
2830
use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
2931
use Joomla\Event\DispatcherInterface;
32+
use Joomla\Event\SubscriberInterface;
3033
use Joomla\Filesystem\Path;
3134
use Joomla\Registry\Registry;
3235
use Joomla\String\StringHelper;
@@ -40,7 +43,7 @@
4043
*
4144
* @since 1.6
4245
*/
43-
final class LanguageFilter extends CMSPlugin
46+
final class LanguageFilter extends CMSPlugin implements SubscriberInterface
4447
{
4548
use SiteRouterAwareTrait;
4649

@@ -158,15 +161,44 @@ public function __construct(
158161
}
159162

160163
/**
161-
* After initialise.
164+
* Returns an array of CMS events this plugin will listen to and the respective handlers.
165+
*
166+
* @return array
167+
*
168+
* @since __DEPLOY_VERSION__
169+
*/
170+
public static function getSubscribedEvents(): array
171+
{
172+
/**
173+
* Note that onAfterInitialise must be the first handlers to run for this
174+
* plugin to operate as expected. These handlers load compatibility code which
175+
* might be needed by other plugins
176+
*/
177+
return [
178+
'onAfterInitialiseRouter' => 'onAfterInitialiseRouter',
179+
'onAfterDispatch' => 'onAfterDispatch',
180+
'onAfterRoute' => 'onAfterRoute',
181+
'onPrivacyCollectAdminCapabilities' => 'onPrivacyCollectAdminCapabilities',
182+
'onUserAfterSave' => 'onUserAfterSave',
183+
'onUserBeforeSave' => 'onUserBeforeSave',
184+
'onUserLogin' => 'onUserLogin',
185+
];
186+
}
187+
188+
/**
189+
* After initialise router.
162190
*
163191
* @return void
164192
*
165-
* @since 1.6
193+
* @since __DEPLOY_VERSION__
166194
*/
167-
public function onAfterInitialise()
195+
public function onAfterInitialiseRouter(AfterInitialiseRouterEvent $event)
168196
{
169-
$router = $this->getSiteRouter();
197+
$router = $event->getRouter();
198+
199+
if (!is_a($router, SiteRouter::class)) {
200+
return;
201+
}
170202

171203
// Attach build rules for language SEF.
172204
$router->attachBuildRule([$this, 'preprocessBuildRule'], Router::PROCESS_BEFORE);
@@ -180,6 +212,8 @@ public function onAfterInitialise()
180212

181213
// Attach parse rule.
182214
$router->attachParseRule([$this, 'parseRule'], Router::PROCESS_BEFORE);
215+
216+
$this->setSiteRouter($router);
183217
}
184218

185219
/**

0 commit comments

Comments
 (0)