Skip to content

Commit 0ac1f29

Browse files
committed
Added option to rewrite the base path for urls (i.e. for proxies)
1 parent ae61409 commit 0ac1f29

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

library/rampage/core/url/BaseUrl.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ class BaseUrl implements UrlModelInterface
5050
*/
5151
protected $type = null;
5252

53+
/**
54+
* @var string
55+
*/
56+
protected $rewriteBasePath = null;
57+
5358
/**
5459
* Base Url
5560
*
56-
* @var array[\Zend\Http\Uri]
61+
* @var \Zend\Http\Uri[]
5762
*/
5863
private $baseUrl = array();
5964

@@ -65,6 +70,42 @@ public function __construct($type = null)
6570
$this->type = $type;
6671
}
6772

73+
/**
74+
* @param string $path
75+
* @return string
76+
*/
77+
protected function rewriteBasePath($path)
78+
{
79+
if (!$this->rewriteBasePath) {
80+
return $path;
81+
}
82+
83+
if (substr($this->rewriteBasePath, 0, 6) == 'regex:') {
84+
$pattern = substr($this->rewriteBasePath, 6);
85+
return @preg_replace($pattern, '', $path);
86+
}
87+
88+
if (strpos($path, $this->rewriteBasePath) === 0) {
89+
$path = substr($path, strlen($this->rewriteBasePath));
90+
}
91+
92+
return $path;
93+
}
94+
95+
/**
96+
* @param string $path
97+
* @return self
98+
*/
99+
public function setRewriteBasePath($path)
100+
{
101+
if ($path && (substr($path, 0, 1) != '/') && (substr($path, 0, 6) != 'regex:')) {
102+
$path = '/' . $path;
103+
}
104+
105+
$this->rewriteBasePath = $path;
106+
return $this;
107+
}
108+
68109
/**
69110
* Returns the base url type
70111
*
@@ -211,7 +252,9 @@ public function getUrl($path = null, $params = null)
211252

212253
$base = $uri->getPath();
213254

214-
if ($extractBasePath && !in_array($base, array('', '/'))) {
255+
if ($extractBasePath && $this->rewriteBasePath) {
256+
$path = $this->rewriteBasePath($path);
257+
} else if ($extractBasePath && !in_array($base, array('', '/'))) {
215258
if (substr($path, 0, 1) != '/') {
216259
$path = '/' . $path;
217260
}

library/rampage/core/url/UrlConfig.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ class UrlConfig extends ArrayConfig implements UrlConfigInterface
3333
public function configureUrlModel(UrlModelInterface $url)
3434
{
3535
$type = $url->getType()? : 'base';
36-
$unsecureUrl = $this->getSection($type)->get('unsecure');
37-
$secureUrl = $this->getSection($type)->get('secure');
36+
$section = $this->getSection($type);
37+
$unsecureUrl = $section->get('unsecure');
38+
$secureUrl = $section->get('secure');
39+
$rewriteBasePath = $section->get('rewriteBasePath');
3840

3941
if ($unsecureUrl) {
4042
$url->setBaseUrl($unsecureUrl, false);
@@ -46,6 +48,8 @@ public function configureUrlModel(UrlModelInterface $url)
4648
$url->setBaseUrl($unsecureUrl, true);
4749
}
4850

51+
$url->setRewriteBasePath($rewriteBasePath);
52+
4953
return $this;
5054
}
5155
}

library/rampage/core/url/UrlModelInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ public function getUrl($path = null, $params = null);
5353
* @return self Must return $this for a fluent interface
5454
*/
5555
public function setBaseUrl($baseUrl, $secure = false);
56+
57+
/**
58+
* Set the base path that should be rewritten (i.e. for proxies)
59+
*
60+
* @param string $path
61+
* @return self
62+
*/
63+
public function setRewriteBasePath($path);
5664
}

0 commit comments

Comments
 (0)