Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate URLs faster - another attempt #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/PathProcessor/CivicrmPathProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@
class CivicrmPathProcessor implements InboundPathProcessorInterface {

/**
* {@inheritdoc}
* If it's not a civi path, just return it as-is.
* If we've looked up the path already, return that.
* If there's a longest matching path in the list of defined urls, then fudge any parts of the url after that so that we don't get page not found. This is for weirdo urls like civicrm/report/instance/14, where for some reason the 14 is not a standard url parameter like ?id=14.
* If we could eliminate all those weird ones, then this function shouldn't be needed.
* We also separately cache the list of defined urls, since it's expensive to build.
* If there is no matching path in the list, just return as-is.
*/
public function processInbound($path, Request $request) {
// If the path is a civicrm path.
if (strpos($path, '/civicrm/') === 0) {
if (class_exists('Civi', FALSE) && isset(\Civi::$statics[__CLASS__]['knownPaths'][$path])) {
return \Civi::$statics[__CLASS__]['knownPaths'][$path];
}
// Initialize civicrm.
$civicrm = new Civicrm();
$civicrm->initialize();
// Fetch civicrm menu items.
$items = \CRM_Core_Menu::items();
$items = \Civi::cache('long')->get('CivicrmPathProcessor-Core_Menu_items');
if (empty($items)) {
$items = \CRM_Core_Menu::items();
\Civi::cache('long')->set('CivicrmPathProcessor-Core_Menu_items', $items);
}
\Civi::$statics[__CLASS__]['knownPaths'][$path] = $path;
$longest = '';
foreach (array_keys($items) as $item) {
$item = '/' . $item;
Expand All @@ -43,12 +56,13 @@ public function processInbound($path, Request $request) {
if (substr($params, 0, 1) == ':') {
$params = substr($params, 1);
}
return "$longest/$params";
\Civi::$statics[__CLASS__]['knownPaths'][$path] = "$longest/$params";
}
else {
return $longest;
\Civi::$statics[__CLASS__]['knownPaths'][$path] = $longest;
}
}
return \Civi::$statics[__CLASS__]['knownPaths'][$path];
}
return $path;
}
Expand Down