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