Elgg  Version master
Route.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Router;
4 
8 class Route extends \Symfony\Component\Routing\Route {
9 
13  protected $parameters = [];
14 
23  public function setMatchedParameters(array $parameters) {
24  $this->parameters = $parameters;
25 
26  return $this;
27  }
28 
34  public function getMatchedParameters() {
35  return $this->parameters;
36  }
37 
42  public function getName() {
43  return elgg_extract('_route', $this->parameters);
44  }
45 
52  public function resolvePageOwner() {
53 
54  $params = $this->getMatchedParameters();
55  $route_name = $this->getName();
56 
57  $route_parts = explode(':', $route_name);
58 
59  $from_guid = function ($guid) {
60  return elgg_call(ELGG_IGNORE_ACCESS, function() use ($guid) {
62  if ($entity instanceof \ElggObject) {
63  return $entity->getContainerEntity();
64  }
65 
66  return $entity;
67  });
68  };
69 
70  switch ($route_parts[0]) {
71  case 'view' :
72  case 'edit' :
73  $username = (string) elgg_extract('username', $params);
74  if (!empty($username)) {
76  }
77 
78  $guid = (int) elgg_extract('guid', $params);
79  if (!empty($guid)) {
80  return $from_guid($guid);
81  }
82  break;
83 
84  case 'add' :
85  case 'collection' :
86  $username = (string) elgg_extract('username', $params);
87  if (!empty($username)) {
89  }
90 
91  $guid = (int) elgg_extract('guid', $params);
92  if (!empty($guid)) {
93  return $from_guid($guid);
94  }
95 
96  $container_guid = (int) elgg_extract('container_guid', $params);
97  if (!empty($container_guid)) {
98  return $from_guid($container_guid);
99  }
100  break;
101 
102  default:
103  // route name doesn't support auto detection of page_owner
104  // but there is information in the route which could support it
105  // and the developer requests that detection is tried
106  if (!(bool) $this->getDefault('_detect_page_owner')) {
107  break;
108  }
109 
110  $username = (string) elgg_extract('username', $params);
111  if (!empty($username)) {
113  }
114 
115  $guid = (int) elgg_extract('guid', $params);
116  if (!empty($guid)) {
117  return $from_guid($guid);
118  }
119 
120  $container_guid = (int) elgg_extract('container_guid', $params);
121  if (!empty($container_guid)) {
122  return $from_guid($container_guid);
123  }
124  break;
125  }
126  }
127 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:301
$params
Saves global plugin settings.
Definition: save.php:13
elgg_get_user_by_username(string $username, bool $try_email=false)
Get a user by username.
Definition: users.php:39
$username
Definition: delete.php:23
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
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:135
resolvePageOwner()
Attemps to resolve page owner from route parameters.
Definition: Route.php:52
Route Wrapper.
Definition: Route.php:8
$entity
Definition: reset.php:8
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:67
getName()
Get matched route name.
Definition: Route.php:42
setMatchedParameters(array $parameters)
Set matched parameters.
Definition: Route.php:23
$container_guid
getMatchedParameters()
Get matched parameters.
Definition: Route.php:34
$guid
Reset an ElggUpgrade.
Definition: reset.php:6