Elgg  Version 6.2
RouteRegistrationService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Router;
4 
10 
17 
18  use Loggable;
19 
27  public function __construct(
28  protected EventsService $events,
29  protected RouteCollection $routes,
30  protected UrlGenerator $generator
31  ) {
32  }
33 
57  public function register(string $name, array $params = []): Route {
58 
59  $params = $this->events->triggerResults('route:config', $name, $params, $params);
60 
61  $path = elgg_extract('path', $params);
62  $controller = elgg_extract('controller', $params);
63  $file = elgg_extract('file', $params);
64  $resource = elgg_extract('resource', $params);
65  $handler = elgg_extract('handler', $params);
66  $middleware = elgg_extract('middleware', $params, []);
67  $walled = elgg_extract('walled', $params, true);
68  $deprecated = elgg_extract('deprecated', $params, '');
69  $required_plugins = elgg_extract('required_plugins', $params, []);
70  $detect_page_owner = (bool) elgg_extract('detect_page_owner', $params, false);
71  $priority = (int) elgg_extract('priority', $params);
72 
73  if (!$path || (!$controller && !$resource && !$handler && !$file)) {
74  throw new InvalidArgumentException(
75  __METHOD__ . ' requires "path" and one of controller parameters ("resource", "controller", "file" or "handler") to be set'
76  );
77  }
78 
79  $defaults = elgg_extract('defaults', $params, []);
80  $requirements = elgg_extract('requirements', $params, []);
81  $methods = elgg_extract('methods', $params, []);
82 
83  $patterns = [
84  'guid' => '\d+',
85  'group_guid' => '\d+',
86  'container_guid' => '\d+',
87  'owner_guid' => '\d+',
88  'username' => '[\p{L}\p{M}\p{Nd}._-]+',
89  ];
90 
91  $path = trim($path, '/');
92  $segments = explode('/', $path);
93  foreach ($segments as &$segment) {
94  // look for segments that are defined as optional with added ?
95  // e.g. /blog/owner/{username?}
96 
97  $matches = [];
98  if (!preg_match('/\{(\w*)(\?)?\}/i', $segment, $matches)) {
99  continue;
100  }
101 
102  $wildcard = $matches[1];
103  if (!isset($defaults[$wildcard]) && isset($matches[2])) {
104  $defaults[$wildcard] = ''; // make it optional
105  }
106 
107  if (!isset($requirements[$wildcard])) {
108  if (array_key_exists($wildcard, $patterns)) {
109  $requirements[$wildcard] = $patterns[$wildcard];
110  } else {
111  $requirements[$wildcard] = '.+?';
112  }
113  }
114 
115  $segment = '{' . $wildcard . '}';
116  }
117 
118  $path = '/' . implode('/', $segments);
119 
120  if ($walled !== false) {
121  $middleware[] = WalledGarden::class;
122  }
123 
124  $middleware[] = MaintenanceGatekeeper::class;
125 
126  $defaults['_controller'] = $controller;
127  $defaults['_file'] = $file;
128  $defaults['_resource'] = $resource;
129  $defaults['_handler'] = $handler;
130  $defaults['_deprecated'] = $deprecated;
131  $defaults['_middleware'] = $middleware;
132  $defaults['_required_plugins'] = $required_plugins;
133  $defaults['_detect_page_owner'] = $detect_page_owner;
134 
135  $options = array_merge((array) elgg_extract('options', $params, []), ['utf8' => true]);
136 
137  $route = new Route($path, $defaults, $requirements, $options, '', [], $methods);
138 
139  $this->routes->add($name, $route, $priority);
140 
141  return $route;
142  }
143 
151  public function unregister(string $name): void {
152  $this->routes->remove($name);
153  }
154 
162  public function get(string $name): ?Route {
163  return $this->routes->get($name);
164  }
165 
170  public function all(): array {
171  return $this->routes->all();
172  }
173 
182  public function generateUrl(string $name, array $parameters = []): ?string {
183  try {
184  $route = $this->get($name);
185  if ($route instanceof Route) {
186  $deprecated = $route->getDefault('_deprecated');
187  if (!empty($deprecated)) {
188  elgg_deprecated_notice("The route \"{$name}\" has been deprecated.", $deprecated);
189  }
190  }
191 
192  $url = $this->generator->generate($name, $parameters, UrlGenerator::ABSOLUTE_URL);
193 
194  // make sure the url is always normalized so it is also usable in CLI
195  return elgg_normalize_url($url);
196  } catch (\Exception $exception) {
197  $this->getLogger()->notice($exception->getMessage());
198  }
199 
200  return null;
201  }
202 
212  public function resolveRouteParameters(string $name, ?\ElggEntity $entity = null, array $parameters = []) {
213  $route = $this->routes->get($name);
214  if (!$route) {
215  return false;
216  }
217 
218  $requirements = $route->getRequirements();
219  $defaults = $route->getDefaults();
220  $props = array_merge(array_keys($requirements), array_keys($defaults));
221 
222  foreach ($props as $prop) {
223  if (str_starts_with($prop, '_')) {
224  continue;
225  }
226 
227  if (isset($parameters[$prop])) {
228  continue;
229  }
230 
231  if (!$entity) {
232  $parameters[$prop] = '';
233  continue;
234  }
235 
236  switch ($prop) {
237  case 'title':
238  case 'name':
239  $parameters[$prop] = elgg_get_friendly_title($entity->getDisplayName());
240  break;
241 
242  default:
243  $parameters[$prop] = $entity->$prop;
244  break;
245  }
246  }
247 
248  return $parameters;
249  }
250 }
resolveRouteParameters(string $name,?\ElggEntity $entity=null, array $parameters=[])
Populates route parameters from entity properties.
Exception thrown if an argument is not of the expected type.
$params
Saves global plugin settings.
Definition: save.php:13
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:117
$defaults
Generic entity header upload helper.
Definition: header.php:6
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Events service.
$resource
generateUrl(string $name, array $parameters=[])
Generate a absolute URL for a named route.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
$path
Definition: details.php:70
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
unregister(string $name)
Unregister a route by its name.
Route Wrapper.
Definition: Route.php:8
$entity
Definition: reset.php:8
elgg_get_friendly_title(string $title)
When given a title, returns a version suitable for inclusion in a URL.
Definition: output.php:192
getLogger()
Returns logger.
Definition: Loggable.php:37
RouteCollection Wrapper.
__construct(protected EventsService $events, protected RouteCollection $routes, protected UrlGenerator $generator)
Constructor.
$exception
Definition: error.php:15
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
$segments
Definition: admin.php:13
$handler
Definition: add.php:7
elgg_normalize_url(string $url)
Definition: output.php:163
$methods
Definition: subscribe.php:8
$priority
UrlGenerator Wrapper.