1 | <?php |
||
2 | |||
3 | /* |
||
4 | * |
||
5 | * (c) Yaroslav Honcharuk <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | |||
11 | namespace Yarhon\RouteGuardBundle\Routing; |
||
12 | |||
13 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
14 | use Yarhon\RouteGuardBundle\Security\RouteAuthorizationCheckerInterface; |
||
15 | use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException; |
||
16 | |||
17 | /** |
||
18 | * @author Yaroslav Honcharuk <[email protected]> |
||
19 | */ |
||
20 | class AuthorizedUrlGenerator implements AuthorizedUrlGeneratorInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var UrlGeneratorInterface |
||
24 | */ |
||
25 | protected $delegate; |
||
26 | |||
27 | /** |
||
28 | * @var RouteAuthorizationCheckerInterface |
||
29 | */ |
||
30 | protected $authorizationChecker; |
||
31 | |||
32 | /** |
||
33 | * @var LocalizedRouteDetector|null |
||
34 | */ |
||
35 | protected $localizedRouteDetector; |
||
36 | |||
37 | /** |
||
38 | * @param UrlGeneratorInterface $urlGenerator |
||
39 | * @param RouteAuthorizationCheckerInterface $authorizationChecker |
||
40 | * @param LocalizedRouteDetector|null $localizedRouteDetector |
||
41 | */ |
||
42 | 25 | public function __construct(UrlGeneratorInterface $urlGenerator, RouteAuthorizationCheckerInterface $authorizationChecker, LocalizedRouteDetector $localizedRouteDetector = null) |
|
43 | { |
||
44 | 25 | $this->delegate = $urlGenerator; |
|
45 | 25 | $this->authorizationChecker = $authorizationChecker; |
|
46 | 25 | $this->localizedRouteDetector = $localizedRouteDetector; |
|
47 | 25 | } |
|
48 | |||
49 | /** |
||
50 | * @param string $name |
||
51 | * @param array $parameters |
||
52 | * @param string $method |
||
53 | * @param int $referenceType One of UrlGeneratorInterface constants |
||
54 | * |
||
55 | * @return string|bool |
||
56 | */ |
||
57 | 25 | public function generate($name, $parameters = [], $method = 'GET', $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
|
58 | { |
||
59 | 25 | if (!is_string($name)) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
60 | 1 | throw new InvalidArgumentException(sprintf('Route name must be a string, %s given.', gettype($name))); |
|
61 | } |
||
62 | |||
63 | 24 | $originalName = $name; |
|
64 | 24 | $originalParameters = $parameters; |
|
65 | |||
66 | 24 | $localizedName = $this->localizedRouteDetector ? $this->localizedRouteDetector->getLocalizedName($name, $parameters) : null; |
|
67 | |||
68 | 24 | if ($localizedName) { |
|
69 | 1 | $name = $localizedName; |
|
70 | 1 | unset($parameters['_locale']); |
|
71 | } |
||
72 | |||
73 | 24 | $routeContext = new GeneratedUrlAwareRouteContext($name, $parameters, $method); |
|
74 | 24 | $routeContext->setReferenceType($referenceType); |
|
75 | |||
76 | 24 | $isGranted = $this->authorizationChecker->isGranted($routeContext); |
|
77 | |||
78 | 24 | if (!$isGranted) { |
|
79 | 10 | return false; |
|
80 | } |
||
81 | |||
82 | 14 | if ($generatedUrl = $routeContext->getGeneratedUrl()) { |
|
83 | 3 | return $generatedUrl; |
|
84 | } |
||
85 | |||
86 | 11 | return $this->delegate->generate($originalName, $originalParameters, $referenceType); |
|
87 | } |
||
88 | } |
||
89 |