joomla-oauth-server/plg_system_oauthserver/src/Extension/Plugin.php

61 lines
1.8 KiB
PHP
Raw Normal View History

2024-03-02 04:44:33 +03:00
<?php
namespace Webmasterskaya\Plugin\System\OauthServer\Extension;
use Joomla\CMS\Factory;
2024-03-02 04:44:33 +03:00
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\SiteRouter;
2024-03-02 04:44:33 +03:00
use Joomla\CMS\Uri\Uri;
use Joomla\Event\SubscriberInterface;
class Plugin extends CMSPlugin implements SubscriberInterface
{
public static function getSubscribedEvents(): array
{
return ['onAfterInitialise' => 'attachOauthRouter'];
2024-03-02 04:44:33 +03:00
}
public function attachOauthRouter(): void
2024-03-02 04:44:33 +03:00
{
/** @var \Joomla\CMS\Application\SiteApplication $app */
$app = $this->getApplication();
if (!$app->isClient('site')) {
2024-03-02 04:44:33 +03:00
return;
}
/** @var \Joomla\CMS\Router\SiteRouter $siteRouter */
$siteRouter = Factory::getContainer()->get(SiteRouter::class);
$siteRouter->attachParseRule([$this, 'parseOauthRoute'], $siteRouter::PROCESS_BEFORE);
}
2024-03-02 04:44:33 +03:00
/**
* @param \Joomla\CMS\Router\SiteRouter $router
* @param \Joomla\CMS\Uri\Uri $uri
* @return void
* @since version
*/
public function parseOauthRoute(SiteRouter &$router, Uri &$uri): void
{
$route = trim($uri->getPath(), '/');
if (empty($route)) {
2024-03-02 04:44:33 +03:00
return;
}
2024-03-06 12:53:09 +03:00
// TODO: Переделать на ComponentRouter (пример в Joomla\CMS\Router\SiteRouter::parseSefRoute())
// 1. Зарегистрировать роутер компонента $router->setComponentRouter()
// 2. Вызвать $router->getComponentRouter($component)
if (!str_starts_with($route, 'login/oauth')) {
2024-03-02 04:44:33 +03:00
return;
}
$segments = explode('/', $route);
$uri->setVar('option', 'com_oauthserver');
$uri->setVar('task', 'login.' . $segments[2]);
$uri->setVar('view', 'default');
2024-03-02 04:44:33 +03:00
$uri->setPath('');
2024-03-02 04:44:33 +03:00
}
}