Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Admin/Form/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function rebuildMenu() {
$config = CRM_Core_Config::singleton(TRUE, TRUE);

// rebuild menu items
CRM_Core_Menu::store();
\Civi::router()->rebuild();;
}

}
4 changes: 2 additions & 2 deletions CRM/Core/Invoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static function getItem($args) {
else {
$path = $args;
}
$item = CRM_Core_Menu::get($path);
$item = \Civi::router()->get($path);

return $item;
}
Expand Down Expand Up @@ -242,7 +242,7 @@ public static function runItem($item) {
return $result;
}

CRM_Core_Menu::store();
\Civi::router()->rebuild();;
CRM_Core_Session::setStatus(ts('Menu has been rebuilt'), ts('Complete'), 'success');
return CRM_Utils_System::redirect();
}
Expand Down
5 changes: 5 additions & 0 deletions CRM/Core/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ public static function isPublicRoute(string $path): bool {
return FALSE;
}

/**
* @internal
*/
public static function clear() {
// Take the rebuild lock: TRUNCATE is a writer too, and clearing the table out from under an
// in-flight self::store() would leave it holding only the rows that store() inserts after the
Expand Down Expand Up @@ -360,6 +363,7 @@ private static function clearMenu() {

/**
* This function recomputes menu from xml and populates civicrm_menu.
* @internal
*/
public static function store() {
// Take the rebuild lock: without it, concurrent rebuilds collide on the (path, domain_id)
Expand Down Expand Up @@ -622,6 +626,7 @@ public static function fillComponentIds(&$menu, $path) {
}

/**
* @internal
* @param string $path
* Path of menu item to retrieve.
*
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected static function filterUri(\Psr\Http\Message\UriInterface $oldUri) {
}

if ($scheme === 'route') {
$menu = CRM_Core_Menu::get($hostPath);
$menu = \Civi::router()->get($hostPath);
$scheme = ($menu && !empty($menu['is_public'])) ? 'frontend' : 'backend';
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System/DrupalBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ public function isFrontEndPage() {
$path = CRM_Utils_System::currentPath() ?? '';

// Get the menu for above URL.
$item = CRM_Core_Menu::get($path);
$item = \Civi::router()->get($path);
// In case the URL is not a civicrm page (a drupal page) we set the FE theme to TRUE - covering the corner case
return (empty($item) || !empty($item['is_public']));
}
Expand Down
7 changes: 7 additions & 0 deletions Civi.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ public static function resources() {
return CRM_Core_Resources::singleton();
}

/**
* @return Civi\Core\Router
*/
public static function router() {
return \Civi::service('civi.router');
}

/**
* Obtain the contact's personal settings.
*
Expand Down
3 changes: 1 addition & 2 deletions Civi/Core/Rebuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use CRM_Core_DAO;
use CRM_Core_DAO_AllCoreTables;
use CRM_Core_ManagedEntities;
use CRM_Core_Menu;
use CRM_Core_OptionGroup;
use CRM_Core_Resources;
use CRM_Core_Session;
Expand Down Expand Up @@ -184,7 +183,7 @@ public function execute(): void {
$session->reset(2);
}
if (!empty($targets['router'])) {
CRM_Core_Menu::store();
\Civi::router()->rebuild();;
}
if (!empty($targets['navigation'])) {
Civi::cache('navigation')->flush();
Expand Down
41 changes: 41 additions & 0 deletions Civi/Core/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Civi\Core;

use Civi\Core\Service\AutoService;

/**
* @service civi.router
*/
class Router extends AutoService {

/**
* Clear the routing table.
*
* It will be rebuilt on next access
*/
public function clear(): void {
\CRM_Core_Menu::clear();
}

/**
* Rebuild the routing table.
*
* This is costly so please consider using `clear` instead
*/
public function rebuild(): void {
\CRM_Core_Menu::store();
}

/**
* Get route for a path from the routing table.
*
* @param string $path e.g. civicrm/mailing/subscribe
*
* @return ?array best matching route, or NULL
*/
public function get(string $path): ?array {
return \CRM_Core_Menu::get($path);
}

}
2 changes: 1 addition & 1 deletion ext/afform/core/Civi/Api4/Action/Afform/Revert.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function processBatch(Result $result, array $items) {
\CRM_Core_ManagedEntities::singleton()->reconcile(E::LONG_NAME);
}
if ($this->flushMenu) {
\CRM_Core_Menu::clear();
\Civi::router()->clear();;
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function writeRecord($item) {
}

if (Utils::shouldClearMenuCache($item, $orig ?? [])) {
\CRM_Core_Menu::clear();
\Civi::router()->clear();;
}

$item['module_name'] = _afform_angular_module_name($item['name'], 'camel');
Expand Down
2 changes: 1 addition & 1 deletion ext/afform/core/afform.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ function afform_civicrm_post($op, $entityName, $id, $object, $params) {
// Adding a new custom field to an empty field group may auto-generate afforms with menu routes.
// @see Civi\Api4\Action\CustomGroup\GetAfforms::getCustomGroupAfforms
if ($op === 'create' && $entityName === 'CustomField') {
\CRM_Core_Menu::clear();
\Civi::router()->clear();;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function addContributionTasks(GenericHookEvent $event): void {
foreach (\CRM_Contribute_Task::tasks() as $id => $task) {
if (!empty($task['url'])) {
$path = explode('?', $task['url'], 2)[0];
$menu = \CRM_Core_Menu::get($path);
$menu = \Civi::router()->get($path);
$key = $menu ? \CRM_Core_Key::get($menu['page_callback'], TRUE) : '';

$event->tasks['Contribution']['contribution.' . $id] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function addMembershipTasks(GenericHookEvent $event): void {
foreach (\CRM_Member_Task::tasks() as $id => $task) {
if (!empty($task['url'])) {
$path = explode('?', $task['url'], 2)[0];
$menu = \CRM_Core_Menu::get($path);
$menu = \Civi::router()->get($path);
$key = $menu ? \CRM_Core_Key::get($menu['page_callback'], TRUE) : '';

$event->tasks['Membership']['membership.' . $id] = [
Expand Down
2 changes: 1 addition & 1 deletion ext/oembed/Civi/Oembed/Oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function create(string $path, array $query = [], array $options = []): ar
$options = $this->normalizeOptions($options);
$query = $this->findPropagatedParams($query);

$route = \CRM_Core_Menu::get($path);
$route = \Civi::router()->get($path);
$result = [
'type' => 'rich',
'version' => '1.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ private function getQfKey(string $pathExpr): ?string {

$result = NULL;
if ($routeName = parse_url($pathExpr, PHP_URL_PATH)) {
if ($routeItem = \CRM_Core_Menu::get($routeName)) {
if ($routeItem = \Civi::router()->get($routeName)) {
if (!empty($routeItem['page_callback'])) {
$result = \CRM_Core_Key::get($routeItem['page_callback']);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/CRM/Core/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function testReadXML_IDS(): void {
* stored and loaded.
*/
public function testModuleData(): void {
CRM_Core_Menu::clear();
$item = CRM_Core_Menu::get('civicrm/case');
\Civi::router()->clear();
$item = \Civi::router()->get('civicrm/case');
$this->assertFalse(isset($item['ids_arguments']['exceptions']));
$this->assertFalse(isset($item['whimsy']));

Expand All @@ -80,8 +80,8 @@ public function testModuleData(): void {
$items['civicrm/case']['whimsy'] = 'godliness';
});

CRM_Core_Menu::clear();
$item = CRM_Core_Menu::get('civicrm/case');
\Civi::router()->clear();
$item = \Civi::router()->get('civicrm/case');
$this->assertTrue(in_array('foobar', $item['ids_arguments']['exceptions']));
$this->assertEquals('godliness', $item['whimsy']);
}
Expand Down