|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\UrlBundle\Twig; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
10
|
|
|
use Zicht\Bundle\UrlBundle\Exception\UnsupportedException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides some twig utilities. |
|
14
|
|
|
*/ |
|
15
|
|
|
class UrlExtension extends \Twig_Extension |
|
16
|
|
|
{ |
|
17
|
|
|
protected $provider; |
|
18
|
|
|
protected $aliasing; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Construct the extension with the passed object as provider. The provider is typically a DelegatingProvider |
|
22
|
|
|
* that delegates to all registered url providers. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \Zicht\Bundle\UrlBundle\Url\Provider $provider |
|
25
|
|
|
* @param \Zicht\Bundle\UrlBundle\Aliasing\Aliasing $aliasing |
|
26
|
9 |
|
*/ |
|
27
|
|
|
public function __construct($provider, $aliasing = null) |
|
28
|
9 |
|
{ |
|
29
|
9 |
|
$this->provider = $provider; |
|
30
|
9 |
|
$this->aliasing = $aliasing; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @{inheritDoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getFilters() |
|
37
|
|
|
{ |
|
38
|
|
|
return array( |
|
39
|
|
|
new \Twig_SimpleFilter('internal_to_public_aliasing', array($this, 'internalToPublicAliasing')), |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Takes a HTML sting and replaces all internal urls with aliased public urls, i.e. /nl/page/42 -> /nl/bring-your-towel |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $html |
|
47
|
|
|
* @return string |
|
48
|
|
|
* |
|
49
|
|
|
* @deprecated Should no longer be used, the aliasing is now handled by a response listener. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function internalToPublicAliasing($html) |
|
52
|
|
|
{ |
|
53
|
|
|
return $html; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @{inheritDoc} |
|
58
|
1 |
|
*/ |
|
59
|
|
|
public function getFunctions() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return array( |
|
62
|
1 |
|
'object_url' => new \Twig_SimpleFunction('object_url', [$this, 'objectUrl']), |
|
63
|
1 |
|
'static_ref' => new \Twig_SimpleFunction('static_ref', [$this, 'staticRef']), |
|
64
|
|
|
'static_reference' => new \Twig_SimpleFunction('static_reference', [$this, 'staticRef']) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns an url based on the passed object. |
|
71
|
|
|
* |
|
72
|
|
|
* @param object $object |
|
73
|
|
|
* @param mixed $defaultIfNotFound |
|
74
|
|
|
* @param array $routerParams |
|
75
|
4 |
|
* @param bool $referenceType |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public function objectUrl($object, $defaultIfNotFound = null, array $routerParams = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
|
79
|
3 |
|
{ |
|
80
|
3 |
|
try { |
|
81
|
1 |
|
$ret = $this->provider->url($object, $routerParams, $referenceType); |
|
|
|
|
|
|
82
|
|
|
} catch (UnsupportedException $e) { |
|
83
|
2 |
|
if (null === $defaultIfNotFound) { |
|
84
|
1 |
|
throw $e; |
|
85
|
|
|
} else { |
|
86
|
1 |
|
if (true === $defaultIfNotFound) { |
|
87
|
|
|
$ret = (string)$object; |
|
88
|
|
|
} else { |
|
89
|
|
|
$ret = $defaultIfNotFound; |
|
90
|
3 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $ret; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns a static reference, i.e. an url that is provided based on a simple string. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* @param array $params |
|
101
|
3 |
|
* |
|
102
|
|
|
* @return string |
|
103
|
3 |
|
*/ |
|
104
|
3 |
|
public function staticRef($name, $params = null) |
|
105
|
|
|
{ |
|
106
|
3 |
|
$name = (string)$name; |
|
107
|
1 |
|
if (!isset($this->static_refs[$name])) { |
|
108
|
1 |
|
try { |
|
109
|
|
|
$this->static_refs[$name] = $this->provider->url($name); |
|
110
|
|
|
} catch (UnsupportedException $e) { |
|
111
|
|
|
$this->static_refs[$name] = '/[static_reference: '. $name . ']'; |
|
112
|
3 |
|
} |
|
113
|
3 |
|
} |
|
114
|
1 |
|
|
|
115
|
|
|
$ret = $this->static_refs[$name]; |
|
116
|
|
|
if ($params) { |
|
117
|
3 |
|
$ret .= '?' . http_build_query($params, 0, '&'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $ret; |
|
121
|
|
|
} |
|
122
|
|
|
private $static_refs = array(); |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the name of the extension. |
|
127
|
1 |
|
* |
|
128
|
|
|
* @return string The extension name |
|
129
|
1 |
|
*/ |
|
130
|
|
|
public function getName() |
|
131
|
|
|
{ |
|
132
|
|
|
return 'zicht_url'; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|