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\Twig; |
12
|
|
|
|
13
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
14
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
15
|
|
|
use Yarhon\RouteGuardBundle\Routing\AuthorizedUrlGeneratorInterface; |
16
|
|
|
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class RoutingRuntime implements RuntimeExtensionInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var AuthorizedUrlGeneratorInterface |
25
|
|
|
*/ |
26
|
|
|
protected $urlGenerator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param AuthorizedUrlGeneratorInterface $urlGenerator |
30
|
|
|
*/ |
31
|
30 |
|
public function __construct(AuthorizedUrlGeneratorInterface $urlGenerator) |
32
|
|
|
{ |
33
|
30 |
|
$this->urlGenerator = $urlGenerator; |
34
|
30 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $name |
38
|
|
|
* @param array $parameters |
39
|
|
|
* @param string $method |
40
|
|
|
* @param array $generateAs |
41
|
|
|
* |
42
|
|
|
* @return string|bool |
43
|
|
|
*/ |
44
|
26 |
|
public function route($name, array $parameters = [], $method = 'GET', array $generateAs = []) |
45
|
|
|
{ |
46
|
26 |
|
$generateAsDefault = ['path', false]; |
47
|
26 |
|
$generateAs += $generateAsDefault; |
48
|
|
|
|
49
|
26 |
|
$referenceType = null; |
50
|
|
|
|
51
|
26 |
|
if ('path' === $generateAs[0]) { |
52
|
4 |
|
$referenceType = $generateAs[1] ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH; |
53
|
22 |
|
} elseif ('url' === $generateAs[0]) { |
54
|
21 |
|
$referenceType = $generateAs[1] ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL; |
55
|
|
|
} else { |
56
|
1 |
|
throw new InvalidArgumentException(sprintf('Invalid reference type: "%s"', $generateAs[0])); |
57
|
|
|
} |
58
|
|
|
|
59
|
25 |
|
return $this->urlGenerator->generate($name, $parameters, $method, $referenceType); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $name |
64
|
|
|
* @param array $parameters |
65
|
|
|
* @param string $method |
66
|
|
|
* @param bool $relative |
67
|
|
|
* |
68
|
|
|
* @return string|bool |
69
|
|
|
*/ |
70
|
2 |
|
public function path($name, array $parameters = [], $method = 'GET', $relative = false) |
71
|
|
|
{ |
72
|
2 |
|
$referenceType = $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH; |
73
|
|
|
|
74
|
2 |
|
return $this->urlGenerator->generate($name, $parameters, $method, $referenceType); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param array $parameters |
80
|
|
|
* @param string $method |
81
|
|
|
* @param bool $relative |
82
|
|
|
* |
83
|
|
|
* @return string|bool |
84
|
|
|
*/ |
85
|
2 |
|
public function url($name, array $parameters = [], $method = 'GET', $relative = false) |
86
|
|
|
{ |
87
|
2 |
|
$referenceType = $relative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL; |
88
|
|
|
|
89
|
2 |
|
return $this->urlGenerator->generate($name, $parameters, $method, $referenceType); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|