diff --git a/administrator/components/com_content/Extension/ContentComponent.php b/administrator/components/com_content/Extension/ContentComponent.php index 613e4180ea12b..79c67af984be3 100644 --- a/administrator/components/com_content/Extension/ContentComponent.php +++ b/administrator/components/com_content/Extension/ContentComponent.php @@ -16,19 +16,21 @@ use Joomla\CMS\Association\AssociationServiceInterface; use Joomla\CMS\Categories\CategoryServiceInterface; use Joomla\CMS\Categories\CategoryServiceTrait; +use Joomla\CMS\Component\Router\RouterServiceInterface; +use Joomla\CMS\Component\Router\RouterServiceTrait; use Joomla\CMS\Extension\BootableExtensionInterface; use Joomla\CMS\Extension\MVCComponent; +use Joomla\CMS\Factory; use Joomla\CMS\Fields\FieldsServiceInterface; use Joomla\CMS\Form\Form; use Joomla\CMS\HTML\HTMLRegistryAwareTrait; +use Joomla\CMS\Language\Text; use Joomla\CMS\Workflow\WorkflowServiceInterface; use Joomla\CMS\Workflow\WorkflowServiceTrait; use Joomla\Component\Content\Administrator\Helper\ContentHelper; use Joomla\Component\Content\Administrator\Service\HTML\AdministratorService; use Joomla\Component\Content\Administrator\Service\HTML\Icon; use Psr\Container\ContainerInterface; -use Joomla\CMS\Language\Text; -use Joomla\CMS\Factory; /** * Component class for com_content @@ -36,10 +38,12 @@ * @since __DEPLOY_VERSION__ */ class ContentComponent extends MVCComponent implements - BootableExtensionInterface, CategoryServiceInterface, FieldsServiceInterface, AssociationServiceInterface, WorkflowServiceInterface + BootableExtensionInterface, CategoryServiceInterface, FieldsServiceInterface, AssociationServiceInterface, + WorkflowServiceInterface, RouterServiceInterface { use CategoryServiceTrait; use AssociationServiceTrait; + use RouterServiceTrait; use HTMLRegistryAwareTrait; use WorkflowServiceTrait; diff --git a/administrator/components/com_content/services/provider.php b/administrator/components/com_content/services/provider.php index 897173bec18b3..9266e4d7f4ee3 100644 --- a/administrator/components/com_content/services/provider.php +++ b/administrator/components/com_content/services/provider.php @@ -11,11 +11,13 @@ use Joomla\CMS\Association\AssociationExtensionInterface; use Joomla\CMS\Categories\CategoryFactoryInterface; +use Joomla\CMS\Component\Router\RouterFactoryInterface; use Joomla\CMS\Dispatcher\DispatcherFactoryInterface; use Joomla\CMS\Extension\ComponentInterface; use Joomla\CMS\Extension\Service\Provider\CategoryFactory; use Joomla\CMS\Extension\Service\Provider\DispatcherFactory; use Joomla\CMS\Extension\Service\Provider\MVCFactoryFactory; +use Joomla\CMS\Extension\Service\Provider\RouterFactory; use Joomla\CMS\HTML\Registry; use Joomla\CMS\MVC\Factory\MVCFactoryFactoryInterface; use Joomla\Component\Content\Administrator\Extension\ContentComponent; @@ -46,6 +48,7 @@ public function register(Container $container) $container->registerServiceProvider(new CategoryFactory('\\Joomla\\Component\\Content')); $container->registerServiceProvider(new MVCFactoryFactory('\\Joomla\\Component\\Content')); $container->registerServiceProvider(new DispatcherFactory('\\Joomla\\Component\\Content')); + $container->registerServiceProvider(new RouterFactory('\\Joomla\\Component\\Content')); $container->set( ComponentInterface::class, @@ -57,6 +60,7 @@ function (Container $container) $component->setMvcFactoryFactory($container->get(MVCFactoryFactoryInterface::class)); $component->setCategoryFactory($container->get(CategoryFactoryInterface::class)); $component->setAssociationExtension($container->get(AssociationExtensionInterface::class)); + $component->setRouterFactory($container->get(RouterFactoryInterface::class)); return $component; } diff --git a/administrator/components/com_finder/Extension/FinderComponent.php b/administrator/components/com_finder/Extension/FinderComponent.php index 262801280703b..9aaffc85a740b 100644 --- a/administrator/components/com_finder/Extension/FinderComponent.php +++ b/administrator/components/com_finder/Extension/FinderComponent.php @@ -11,6 +11,8 @@ defined('JPATH_PLATFORM') or die; +use Joomla\CMS\Component\Router\RouterServiceInterface; +use Joomla\CMS\Component\Router\RouterServiceTrait; use Joomla\CMS\Extension\BootableExtensionInterface; use Joomla\CMS\Extension\MVCComponent; use Joomla\CMS\HTML\HTMLRegistryAwareTrait; @@ -24,8 +26,9 @@ * * @since __DEPLOY_VERSION__ */ -class FinderComponent extends MVCComponent implements BootableExtensionInterface +class FinderComponent extends MVCComponent implements BootableExtensionInterface, RouterServiceInterface { + use RouterServiceTrait; use HTMLRegistryAwareTrait; /** diff --git a/administrator/components/com_finder/services/provider.php b/administrator/components/com_finder/services/provider.php index 7f8289da6556a..6d251433b20b6 100644 --- a/administrator/components/com_finder/services/provider.php +++ b/administrator/components/com_finder/services/provider.php @@ -9,10 +9,12 @@ defined('_JEXEC') or die; +use Joomla\CMS\Component\Router\RouterFactoryInterface; use Joomla\CMS\Dispatcher\DispatcherFactoryInterface; use Joomla\CMS\Extension\ComponentInterface; use Joomla\CMS\Extension\Service\Provider\DispatcherFactory; use Joomla\CMS\Extension\Service\Provider\MVCFactoryFactory; +use Joomla\CMS\Extension\Service\Provider\RouterFactory; use Joomla\CMS\HTML\Registry; use Joomla\CMS\MVC\Factory\MVCFactoryFactoryInterface; use Joomla\Component\Finder\Administrator\Extension\FinderComponent; @@ -39,6 +41,7 @@ public function register(Container $container) { $container->registerServiceProvider(new MVCFactoryFactory('\\Joomla\\Component\\Finder')); $container->registerServiceProvider(new DispatcherFactory('\\Joomla\\Component\\Finder')); + $container->registerServiceProvider(new RouterFactory('\\Joomla\\Component\\Finder')); $container->set( ComponentInterface::class, @@ -46,6 +49,7 @@ function (Container $container) { $component = new FinderComponent($container->get(DispatcherFactoryInterface::class)); $component->setMvcFactoryFactory($container->get(MVCFactoryFactoryInterface::class)); + $component->setRouterFactory($container->get(RouterFactoryInterface::class)); $component->setRegistry($container->get(Registry::class)); return $component; diff --git a/administrator/components/com_newsfeeds/Extension/NewsfeedsComponent.php b/administrator/components/com_newsfeeds/Extension/NewsfeedsComponent.php index fa69fe2aeffb6..ede4ea911caaa 100644 --- a/administrator/components/com_newsfeeds/Extension/NewsfeedsComponent.php +++ b/administrator/components/com_newsfeeds/Extension/NewsfeedsComponent.php @@ -15,10 +15,11 @@ use Joomla\CMS\Association\AssociationServiceInterface; use Joomla\CMS\Categories\CategoryServiceInterface; use Joomla\CMS\Categories\CategoryServiceTrait; +use Joomla\CMS\Component\Router\RouterServiceInterface; +use Joomla\CMS\Component\Router\RouterServiceTrait; use Joomla\CMS\Extension\BootableExtensionInterface; use Joomla\CMS\Extension\MVCComponent; use Joomla\CMS\HTML\HTMLRegistryAwareTrait; -use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface; use Joomla\Component\Newsfeeds\Administrator\Service\HTML\AdministratorService; use Psr\Container\ContainerInterface; @@ -28,11 +29,12 @@ * @since __DEPLOY_VERSION__ */ class NewsfeedsComponent extends MVCComponent implements - BootableExtensionInterface, CategoryServiceInterface, AssociationServiceInterface + BootableExtensionInterface, CategoryServiceInterface, AssociationServiceInterface, RouterServiceInterface { use CategoryServiceTrait; use AssociationServiceTrait; use HTMLRegistryAwareTrait; + use RouterServiceTrait; /** * Booting the extension. This is the function to set up the environment of the extension like diff --git a/administrator/components/com_newsfeeds/services/provider.php b/administrator/components/com_newsfeeds/services/provider.php index d11769cdeeaed..7e5c0502593ab 100644 --- a/administrator/components/com_newsfeeds/services/provider.php +++ b/administrator/components/com_newsfeeds/services/provider.php @@ -11,11 +11,13 @@ use Joomla\CMS\Association\AssociationExtensionInterface; use Joomla\CMS\Categories\CategoryFactoryInterface; +use Joomla\CMS\Component\Router\RouterFactoryInterface; use Joomla\CMS\Dispatcher\DispatcherFactoryInterface; use Joomla\CMS\Extension\ComponentInterface; use Joomla\CMS\Extension\Service\Provider\CategoryFactory; use Joomla\CMS\Extension\Service\Provider\DispatcherFactory; use Joomla\CMS\Extension\Service\Provider\MVCFactoryFactory; +use Joomla\CMS\Extension\Service\Provider\RouterFactory; use Joomla\CMS\HTML\Registry; use Joomla\CMS\MVC\Factory\MVCFactoryFactoryInterface; use Joomla\Component\Newsfeeds\Administrator\Extension\NewsfeedsComponent; @@ -46,6 +48,7 @@ public function register(Container $container) $container->registerServiceProvider(new CategoryFactory('\\Joomla\\Component\\Newsfeeds')); $container->registerServiceProvider(new MVCFactoryFactory('\\Joomla\\Component\\Newsfeeds')); $container->registerServiceProvider(new DispatcherFactory('\\Joomla\\Component\\Newsfeeds')); + $container->registerServiceProvider(new RouterFactory('\\Joomla\\Component\\Newsfeeds')); $container->set( ComponentInterface::class, @@ -57,6 +60,7 @@ function (Container $container) $component->setMvcFactoryFactory($container->get(MVCFactoryFactoryInterface::class)); $component->setCategoryFactory($container->get(CategoryFactoryInterface::class)); $component->setAssociationExtension($container->get(AssociationExtensionInterface::class)); + $component->setRouterFactory($container->get(RouterFactoryInterface::class)); return $component; } diff --git a/administrator/components/com_users/Extension/UsersComponent.php b/administrator/components/com_users/Extension/UsersComponent.php new file mode 100644 index 0000000000000..2d32bbc084702 --- /dev/null +++ b/administrator/components/com_users/Extension/UsersComponent.php @@ -0,0 +1,26 @@ +registerServiceProvider(new MVCFactoryFactory('\\Joomla\\Component\\Users')); $container->registerServiceProvider(new DispatcherFactory('\\Joomla\\Component\\Users')); + $container->registerServiceProvider(new RouterFactory('\\Joomla\\Component\\Users')); + $container->set( ComponentInterface::class, function (Container $container) { - $component = new MVCComponent($container->get(DispatcherFactoryInterface::class)); + $component = new UsersComponent($container->get(DispatcherFactoryInterface::class)); $component->setMvcFactoryFactory($container->get(MVCFactoryFactoryInterface::class)); + $component->setRouterFactory($container->get(RouterFactoryInterface::class)); return $component; } diff --git a/components/com_contact/router.php b/components/com_contact/Service/Router.php similarity index 76% rename from components/com_contact/router.php rename to components/com_contact/Service/Router.php index 26621fe6e2963..89463f3c59c46 100644 --- a/components/com_contact/router.php +++ b/components/com_contact/Service/Router.php @@ -7,9 +7,12 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Component\Contact\Site\Service; + defined('_JEXEC') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\SiteApplication; +use Joomla\CMS\Categories\CategoryFactoryInterface; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Component\Router\RouterView; use Joomla\CMS\Component\Router\RouterViewConfiguration; @@ -17,26 +20,48 @@ use Joomla\CMS\Component\Router\Rules\NomenuRules; use Joomla\CMS\Component\Router\Rules\StandardRules; use Joomla\CMS\Menu\AbstractMenu; -use Joomla\CMS\Categories\Categories; -use Joomla\CMS\Factory; +use Joomla\Database\DatabaseInterface; /** * Routing class from com_contact * * @since 3.3 */ -class ContactRouter extends RouterView +class Router extends RouterView { protected $noIDs = false; /** - * Search Component router constructor + * The category factory + * + * @var CategoryFactoryInterface + * + * @since __DEPLOY_VERSION__ + */ + private $categoryFactory; + + /** + * The db * - * @param CMSApplication $app The application object - * @param AbstractMenu $menu The menu object to work with + * @var DatabaseInterface + * + * @since __DEPLOY_VERSION__ + */ + private $db; + + /** + * Content Component router constructor + * + * @param SiteApplication $app The application object + * @param AbstractMenu $menu The menu object to work with + * @param CategoryFactoryInterface $categoryFactory The category object + * @param DatabaseInterface $db The database object */ - public function __construct($app = null, $menu = null) + public function __construct(SiteApplication $app, AbstractMenu $menu, CategoryFactoryInterface $categoryFactory, DatabaseInterface $db) { + $this->categoryFactory = $categoryFactory; + $this->db = $db; + $params = ComponentHelper::getParams('com_contact'); $this->noIDs = (bool) $params->get('sef_ids'); $categories = new RouterViewConfiguration('categories'); @@ -67,7 +92,7 @@ public function __construct($app = null, $menu = null) */ public function getCategorySegment($id, $query) { - $category = Categories::getInstance($this->getName())->get($id); + $category = $this->categoryFactory->createCategory()->get($id); if ($category) { @@ -113,14 +138,13 @@ public function getContactSegment($id, $query) { if (!strpos($id, ':')) { - $db = Factory::getDbo(); - $dbquery = $db->getQuery(true); + $dbquery = $this->db->getQuery(true); $dbquery->select($dbquery->quoteName('alias')) ->from($dbquery->quoteName('#__contact_details')) ->where('id = ' . $dbquery->quote((int) $id)); - $db->setQuery($dbquery); + $this->db->setQuery($dbquery); - $id .= ':' . $db->loadResult(); + $id .= ':' . $this->db->loadResult(); } if ($this->noIDs) @@ -145,7 +169,7 @@ public function getCategoryId($segment, $query) { if (isset($query['id'])) { - $category = Categories::getInstance($this->getName(), array('access' => false))->get($query['id']); + $category = $this->categoryFactory->createCategory(['access' => false])->get($query['id']); if ($category) { @@ -197,15 +221,14 @@ public function getContactId($segment, $query) { if ($this->noIDs) { - $db = Factory::getDbo(); - $dbquery = $db->getQuery(true); + $dbquery = $this->db->getQuery(true); $dbquery->select($dbquery->quoteName('id')) ->from($dbquery->quoteName('#__contact_details')) ->where('alias = ' . $dbquery->quote($segment)) ->where('catid = ' . $dbquery->quote($query['id'])); - $db->setQuery($dbquery); + $this->db->setQuery($dbquery); - return (int) $db->loadResult(); + return (int) $this->db->loadResult(); } return (int) $segment; diff --git a/components/com_content/router.php b/components/com_content/Service/Router.php similarity index 79% rename from components/com_content/router.php rename to components/com_content/Service/Router.php index 87b024ed20790..77671d8e137ea 100644 --- a/components/com_content/router.php +++ b/components/com_content/Service/Router.php @@ -7,9 +7,12 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Component\Content\Site\Service; + defined('_JEXEC') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\SiteApplication; +use Joomla\CMS\Categories\CategoryFactoryInterface; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Component\Router\RouterView; use Joomla\CMS\Component\Router\RouterViewConfiguration; @@ -17,26 +20,48 @@ use Joomla\CMS\Component\Router\Rules\NomenuRules; use Joomla\CMS\Component\Router\Rules\StandardRules; use Joomla\CMS\Menu\AbstractMenu; -use Joomla\CMS\Categories\Categories; -use Joomla\CMS\Factory; +use Joomla\Database\DatabaseInterface; /** * Routing class of com_content * * @since 3.3 */ -class ContentRouter extends RouterView +class Router extends RouterView { protected $noIDs = false; + /** + * The category factory + * + * @var CategoryFactoryInterface + * + * @since __DEPLOY_VERSION__ + */ + private $categoryFactory; + + /** + * The db + * + * @var DatabaseInterface + * + * @since __DEPLOY_VERSION__ + */ + private $db; + /** * Content Component router constructor * - * @param CMSApplication $app The application object - * @param AbstractMenu $menu The menu object to work with + * @param SiteApplication $app The application object + * @param AbstractMenu $menu The menu object to work with + * @param CategoryFactoryInterface $categoryFactory The category object + * @param DatabaseInterface $db The database object */ - public function __construct($app = null, $menu = null) + public function __construct(SiteApplication $app, AbstractMenu $menu, CategoryFactoryInterface $categoryFactory, DatabaseInterface $db) { + $this->categoryFactory = $categoryFactory; + $this->db = $db; + $params = ComponentHelper::getParams('com_content'); $this->noIDs = (bool) $params->get('sef_ids'); $categories = new RouterViewConfiguration('categories'); @@ -71,7 +96,7 @@ public function __construct($app = null, $menu = null) */ public function getCategorySegment($id, $query) { - $category = Categories::getInstance($this->getName())->get($id); + $category = $this->categoryFactory->createCategory(['access' => true])->get($id); if ($category) { @@ -117,14 +142,13 @@ public function getArticleSegment($id, $query) { if (!strpos($id, ':')) { - $db = Factory::getDbo(); - $dbquery = $db->getQuery(true); + $dbquery = $this->db->getQuery(true); $dbquery->select($dbquery->quoteName('alias')) ->from($dbquery->quoteName('#__content')) ->where('id = ' . $dbquery->quote($id)); - $db->setQuery($dbquery); + $this->db->setQuery($dbquery); - $id .= ':' . $db->loadResult(); + $id .= ':' . $this->db->loadResult(); } if ($this->noIDs) @@ -164,7 +188,7 @@ public function getCategoryId($segment, $query) { if (isset($query['id'])) { - $category = Categories::getInstance($this->getName(), array('access' => false))->get($query['id']); + $category = $this->categoryFactory->createCategory(['access' => false])->get($query['id']); if ($category) { @@ -216,15 +240,14 @@ public function getArticleId($segment, $query) { if ($this->noIDs) { - $db = Factory::getDbo(); - $dbquery = $db->getQuery(true); + $dbquery = $this->db->getQuery(true); $dbquery->select($dbquery->quoteName('id')) ->from($dbquery->quoteName('#__content')) ->where('alias = ' . $dbquery->quote($segment)) ->where('catid = ' . $dbquery->quote($query['id'])); - $db->setQuery($dbquery); + $this->db->setQuery($dbquery); - return (int) $db->loadResult(); + return (int) $this->db->loadResult(); } return (int) $segment; diff --git a/components/com_finder/router.php b/components/com_finder/Service/Router.php similarity index 71% rename from components/com_finder/router.php rename to components/com_finder/Service/Router.php index dea9ae2ed9744..91e0a0b538820 100644 --- a/components/com_finder/router.php +++ b/components/com_finder/Service/Router.php @@ -7,28 +7,32 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Component\Finder\Site\Service; + defined('_JEXEC') or die; +use Joomla\CMS\Application\SiteApplication; use Joomla\CMS\Component\Router\RouterView; use Joomla\CMS\Component\Router\RouterViewConfiguration; use Joomla\CMS\Component\Router\Rules\MenuRules; use Joomla\CMS\Component\Router\Rules\NomenuRules; use Joomla\CMS\Component\Router\Rules\StandardRules; +use Joomla\CMS\Menu\AbstractMenu; /** * Routing class from com_finder * * @since 3.3 */ -class FinderRouter extends RouterView +class Router extends RouterView { /** * Finder Component router constructor * - * @param CMSApplication $app The application object - * @param AbstractMenu $menu The menu object to work with + * @param SiteApplication $app The application object + * @param AbstractMenu $menu The menu object to work with */ - public function __construct($app = null, $menu = null) + public function __construct(SiteApplication $app, AbstractMenu $menu) { $search = new RouterViewConfiguration('search'); $this->registerView($search); diff --git a/components/com_newsfeeds/router.php b/components/com_newsfeeds/Service/Router.php similarity index 77% rename from components/com_newsfeeds/router.php rename to components/com_newsfeeds/Service/Router.php index 770a40b0106ba..efc57a41838de 100644 --- a/components/com_newsfeeds/router.php +++ b/components/com_newsfeeds/Service/Router.php @@ -7,9 +7,12 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Component\Newsfeeds\Site\Service; + defined('_JEXEC') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\SiteApplication; +use Joomla\CMS\Categories\CategoryFactoryInterface; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Component\Router\RouterView; use Joomla\CMS\Component\Router\RouterViewConfiguration; @@ -17,26 +20,48 @@ use Joomla\CMS\Component\Router\Rules\NomenuRules; use Joomla\CMS\Component\Router\Rules\StandardRules; use Joomla\CMS\Menu\AbstractMenu; -use Joomla\CMS\Categories\Categories; -use Joomla\CMS\Factory; +use Joomla\Database\DatabaseInterface; /** * Routing class from com_newsfeeds * * @since 3.3 */ -class NewsfeedsRouter extends RouterView +class Router extends RouterView { protected $noIDs = false; + /** + * The category factory + * + * @var CategoryFactoryInterface + * + * @since __DEPLOY_VERSION__ + */ + private $categoryFactory; + + /** + * The db + * + * @var DatabaseInterface + * + * @since __DEPLOY_VERSION__ + */ + private $db; + /** * Newsfeeds Component router constructor * - * @param CMSApplication $app The application object - * @param AbstractMenu $menu The menu object to work with + * @param SiteApplication $app The application object + * @param AbstractMenu $menu The menu object to work with + * @param CategoryFactoryInterface $categoryFactory The category object + * @param DatabaseInterface $db The database object */ - public function __construct($app = null, $menu = null) + public function __construct(SiteApplication $app, AbstractMenu $menu, CategoryFactoryInterface $categoryFactory, DatabaseInterface $db) { + $this->categoryFactory = $categoryFactory; + $this->db = $db; + $params = ComponentHelper::getParams('com_newsfeeds'); $this->noIDs = (bool) $params->get('sef_ids'); $categories = new RouterViewConfiguration('categories'); @@ -66,7 +91,7 @@ public function __construct($app = null, $menu = null) */ public function getCategorySegment($id, $query) { - $category = Categories::getInstance($this->getName())->get($id); + $category = $this->categoryFactory->createCategory()->get($id); if ($category) { @@ -112,14 +137,13 @@ public function getNewsfeedSegment($id, $query) { if (!strpos($id, ':')) { - $db = Factory::getDbo(); - $dbquery = $db->getQuery(true); + $dbquery = $this->db->getQuery(true); $dbquery->select($dbquery->quoteName('alias')) ->from($dbquery->quoteName('#__newsfeeds')) ->where('id = ' . $dbquery->quote((int) $id)); - $db->setQuery($dbquery); + $this->db->setQuery($dbquery); - $id .= ':' . $db->loadResult(); + $id .= ':' . $this->db->loadResult(); } if ($this->noIDs) @@ -144,7 +168,7 @@ public function getCategoryId($segment, $query) { if (isset($query['id'])) { - $category = Categories::getInstance($this->getName(), array('access' => false))->get($query['id']); + $category = $this->categoryFactory->createCategory(['access' => false])->get($query['id']); if ($category) { @@ -196,15 +220,14 @@ public function getNewsfeedId($segment, $query) { if ($this->noIDs) { - $db = Factory::getDbo(); - $dbquery = $db->getQuery(true); + $dbquery = $this->db->getQuery(true); $dbquery->select($dbquery->quoteName('id')) ->from($dbquery->quoteName('#__newsfeeds')) ->where('alias = ' . $dbquery->quote($segment)) ->where('catid = ' . $dbquery->quote($query['id'])); - $db->setQuery($dbquery); + $this->db->setQuery($dbquery); - return (int) $db->loadResult(); + return (int) $this->db->loadResult(); } return (int) $segment; diff --git a/components/com_users/router.php b/components/com_users/Service/Router.php similarity index 79% rename from components/com_users/router.php rename to components/com_users/Service/Router.php index 5de01dca6495b..521208f8ec185 100644 --- a/components/com_users/router.php +++ b/components/com_users/Service/Router.php @@ -7,9 +7,11 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ +namespace Joomla\Component\Users\Site\Service; + defined('_JEXEC') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\SiteApplication; use Joomla\CMS\Component\Router\RouterView; use Joomla\CMS\Component\Router\RouterViewConfiguration; use Joomla\CMS\Component\Router\Rules\MenuRules; @@ -22,15 +24,15 @@ * * @since 3.2 */ -class UsersRouter extends RouterView +class Router extends RouterView { /** * Users Component router constructor * - * @param CMSApplication $app The application object - * @param AbstractMenu $menu The menu object to work with + * @param SiteApplication $app The application object + * @param AbstractMenu $menu The menu object to work with */ - public function __construct($app = null, $menu = null) + public function __construct(SiteApplication $app, AbstractMenu $menu) { $this->registerView(new RouterViewConfiguration('login')); $profile = new RouterViewConfiguration('profile'); diff --git a/libraries/src/Categories/CategoryFactory.php b/libraries/src/Categories/CategoryFactory.php index 9fb7c1f775d8f..8a4ff85ff7a42 100644 --- a/libraries/src/Categories/CategoryFactory.php +++ b/libraries/src/Categories/CategoryFactory.php @@ -50,7 +50,7 @@ public function __construct($namespace) * * @throws SectionNotFoundException */ - public function createCategory(array $options, string $section): CategoryInterface + public function createCategory(array $options = [], string $section = ''): CategoryInterface { $className = trim($this->namespace, '\\') . '\\Site\\Service\\' . ucfirst($section) . 'Category'; diff --git a/libraries/src/Categories/CategoryFactoryInterface.php b/libraries/src/Categories/CategoryFactoryInterface.php index dd8e57c20b3ee..3143cf1ab75e7 100644 --- a/libraries/src/Categories/CategoryFactoryInterface.php +++ b/libraries/src/Categories/CategoryFactoryInterface.php @@ -29,5 +29,5 @@ interface CategoryFactoryInterface * * @throws SectionNotFoundException */ - public function createCategory(array $options, string $section): CategoryInterface; + public function createCategory(array $options = [], string $section = ''): CategoryInterface; } diff --git a/libraries/src/Component/Router/RouterFactory.php b/libraries/src/Component/Router/RouterFactory.php new file mode 100644 index 0000000000000..4e1d3635a274b --- /dev/null +++ b/libraries/src/Component/Router/RouterFactory.php @@ -0,0 +1,89 @@ +namespace = $namespace; + $this->categoryFactory = $categoryFactory; + $this->db = $db; + } + + /** + * Creates a router. + * + * @param CMSApplicationInterface $application The application + * @param AbstractMenu $menu The menu object to work with + * + * @return RouterInterface + * + * @since __DEPLOY_VERSION__ + */ + public function createRouter(CMSApplicationInterface $application, AbstractMenu $menu): RouterInterface + { + $className = trim($this->namespace, '\\') . '\\' . ucfirst($application->getName()) . '\\Service\\Router'; + + if (!class_exists($className)) + { + throw new \RuntimeException('No router available for this application.'); + } + + return new $className($application, $menu, $this->categoryFactory, $this->db); + } +} diff --git a/libraries/src/Component/Router/RouterFactoryInterface.php b/libraries/src/Component/Router/RouterFactoryInterface.php new file mode 100644 index 0000000000000..d9ee966b52555 --- /dev/null +++ b/libraries/src/Component/Router/RouterFactoryInterface.php @@ -0,0 +1,34 @@ +routerFactory->createRouter($application, $menu); + } + + /** + * The router factory. + * + * @param RouterFactoryInterface $routerFactory The router factory + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function setRouterFactory(RouterFactoryInterface $routerFactory) + { + $this->routerFactory = $routerFactory; + } +} diff --git a/libraries/src/Component/Router/RouterView.php b/libraries/src/Component/Router/RouterView.php index e463d0645efb4..17835784e9190 100644 --- a/libraries/src/Component/Router/RouterView.php +++ b/libraries/src/Component/Router/RouterView.php @@ -10,6 +10,7 @@ defined('JPATH_PLATFORM') or die; +use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Component\Router\Rules\RulesInterface; /** @@ -287,7 +288,7 @@ public function getName() throw new \Exception('JLIB_APPLICATION_ERROR_ROUTER_GET_NAME', 500); } - $this->name = strtolower($r[1]); + $this->name = str_replace('com_', '', ComponentHelper::getComponentName($this, strtolower($r[1]))); } return $this->name; diff --git a/libraries/src/Extension/LegacyComponent.php b/libraries/src/Extension/LegacyComponent.php index 32627659ddb8d..ecdd6a6855b5d 100644 --- a/libraries/src/Extension/LegacyComponent.php +++ b/libraries/src/Extension/LegacyComponent.php @@ -23,13 +23,18 @@ use Joomla\CMS\MVC\Factory\MVCFactory; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface; +use Joomla\CMS\Component\Router\RouterServiceInterface; +use Joomla\CMS\Menu\AbstractMenu; +use Joomla\CMS\Component\Router\RouterInterface; +use Joomla\CMS\Component\Router\RouterLegacy; /** * Access to component specific services. * * @since 4.0.0 */ -class LegacyComponent implements ComponentInterface, MVCFactoryServiceInterface, CategoryServiceInterface, FieldsServiceInterface +class LegacyComponent + implements ComponentInterface, MVCFactoryServiceInterface, CategoryServiceInterface, FieldsServiceInterface, RouterServiceInterface { use CategoryServiceTrait; @@ -209,6 +214,46 @@ public function getContexts(): array return $helper::getContexts(); } + /** + * Returns the router. + * + * @param CMSApplicationInterface $application The application object + * @param AbstractMenu $menu The menu object to work with + * + * @return RouterInterface + * + * @since __DEPLOY_VERSION__ + */ + public function createRouter(CMSApplicationInterface $application, AbstractMenu $menu): RouterInterface + { + $compname = ucfirst($this->component); + $class = $compname . 'Router'; + + if (!class_exists($class)) + { + // Use the component routing handler if it exists + $path = JPATH_SITE . '/components/com_' . $this->component . '/router.php'; + + // Use the custom routing handler if it exists + if (file_exists($path)) + { + require_once $path; + } + } + + if (class_exists($class)) + { + $reflection = new \ReflectionClass($class); + + if (in_array('Joomla\\CMS\\Component\\Router\\RouterInterface', $reflection->getInterfaceNames())) + { + return new $class($application, $menu); + } + } + + return new RouterLegacy($compname); + } + /** * Returns the classname of the legacy helper class. If none is found it returns false. * diff --git a/libraries/src/Extension/Service/Provider/RouterFactory.php b/libraries/src/Extension/Service/Provider/RouterFactory.php new file mode 100644 index 0000000000000..da27ea37786e0 --- /dev/null +++ b/libraries/src/Extension/Service/Provider/RouterFactory.php @@ -0,0 +1,77 @@ +namespace = $namespace; + } + + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container) + { + $container->set( + RouterFactoryInterface::class, + function (Container $container) + { + $categoryFactory = null; + + if ($container->has(CategoryFactoryInterface::class)) + { + $categoryFactory = $container->get(CategoryFactoryInterface::class); + } + + return new \Joomla\CMS\Component\Router\RouterFactory( + $this->namespace, + $categoryFactory, + $container->get(DatabaseInterface::class) + ); + } + ); + } +} diff --git a/libraries/src/Router/SiteRouter.php b/libraries/src/Router/SiteRouter.php index dbf3f0ec0de08..10ba6da7f1d28 100644 --- a/libraries/src/Router/SiteRouter.php +++ b/libraries/src/Router/SiteRouter.php @@ -18,6 +18,7 @@ use Joomla\CMS\Menu\AbstractMenu; use Joomla\CMS\Uri\Uri; use Joomla\String\StringHelper; +use Joomla\CMS\Component\Router\RouterServiceInterface; /** * Class to create and parse routes for the site application @@ -609,34 +610,16 @@ public function getComponentRouter($component) { if (!isset($this->componentRouters[$component])) { - $compname = ucfirst(substr($component, 4)); - $class = $compname . 'Router'; + $componentInstance = $this->app->bootComponent($component); - if (!class_exists($class)) + if ($componentInstance instanceof RouterServiceInterface) { - // Use the component routing handler if it exists - $path = JPATH_SITE . '/components/' . $component . '/router.php'; - - // Use the custom routing handler if it exists - if (file_exists($path)) - { - require_once $path; - } - } - - if (class_exists($class)) - { - $reflection = new \ReflectionClass($class); - - if (in_array('Joomla\\CMS\\Component\\Router\\RouterInterface', $reflection->getInterfaceNames())) - { - $this->componentRouters[$component] = new $class($this->app, $this->menu); - } + $this->componentRouters[$component] = $componentInstance->createRouter($this->app, $this->menu); } if (!isset($this->componentRouters[$component])) { - $this->componentRouters[$component] = new RouterLegacy($compname); + $this->componentRouters[$component] = new RouterLegacy(ucfirst(substr($component, 4))); } }