-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrganizationalRouting.php
More file actions
86 lines (70 loc) · 2.86 KB
/
OrganizationalRouting.php
File metadata and controls
86 lines (70 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Routing;
use Klipper\Component\Security\Organizational\OrganizationalContextInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
/**
* Organizational routing.
*
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class OrganizationalRouting extends TranslatableRouting implements OrganizationalRoutingInterface
{
private RouterInterface $router;
private OrganizationalContextInterface $context;
/**
* @param RouterInterface $router The router
* @param RequestStack $requestStack The request stack
* @param OrganizationalContextInterface $context The organizational context
*/
public function __construct(
RouterInterface $router,
RequestStack $requestStack,
OrganizationalContextInterface $context
) {
parent::__construct($router, $requestStack);
$this->router = $router;
$this->context = $context;
}
public function getOrgPath(string $name, array $parameters = [], bool $relative = false): string
{
$parameters = $this->getOrgParameters($name, $parameters);
return $this->getPath($name, $parameters, $relative);
}
public function getLangOrgPath(string $name, array $parameters = [], bool $relative = false): string
{
return $this->getOrgPath($name, $this->getLangParameters($parameters), $relative);
}
public function getOrgUrl(string $name, array $parameters = [], bool $schemeRelative = false): string
{
$parameters = $this->getOrgParameters($name, $parameters);
return $this->getUrl($name, $parameters, $schemeRelative);
}
public function getLangOrgUrl(string $name, array $parameters = [], bool $schemeRelative = false): string
{
return $this->getOrgUrl($name, $this->getLangParameters($parameters), $schemeRelative);
}
public function getOrgParameters(string $name, array $parameters = []): array
{
$route = $this->router->getRouteCollection()->get($name);
if (null !== $route && $route->hasDefault('_organizational')
&& !isset($parameters[$route->getDefault('_organizational')])
) {
$orgKey = $route->getDefault('_organizational');
$parameters = array_merge($parameters, [
$orgKey => $this->context->isOrganization() && null !== $this->context->getCurrentOrganization()
? $this->context->getCurrentOrganization()->getName()
: 'user',
]);
}
return $parameters;
}
}