Skip to content

Commit fce90c6

Browse files
authored
Merge pull request #320 from emulienfou/master
Fixing issue #319
2 parents cbe7ba6 + 2ccdbb5 commit fce90c6

File tree

13 files changed

+111
-25
lines changed

13 files changed

+111
-25
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ language: php
22

33
matrix:
44
include:
5-
- php: 5.3
6-
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
7-
dist: precise
5+
- php: 5.6
86
- php: 7.1
97
- php: 7.2
108
- php: 7.1

Command/DumpCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ private function doDump(InputInterface $input, OutputInterface $output)
167167
$extractor->getRoutes(),
168168
$extractor->getPrefix($input->getOption('locale')),
169169
$extractor->getHost(),
170+
$extractor->getPort(),
170171
$extractor->getScheme()
171172
),
172173
'json',

Controller/Controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function indexAction(Request $request, $_format)
9696
$exposedRoutes,
9797
$this->exposedRoutesExtractor->getPrefix($request->getLocale()),
9898
$this->exposedRoutesExtractor->getHost(),
99+
$this->exposedRoutesExtractor->getPort(),
99100
$this->exposedRoutesExtractor->getScheme(),
100101
$request->getLocale()
101102
);

Extractor/ExposedRoutesExtractor.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,26 @@ public function getHost()
104104
{
105105
$requestContext = $this->router->getContext();
106106

107-
$host = $requestContext->getHost();
107+
$host = $requestContext->getHost() .
108+
('' === $this->getPort() ? $this->getPort() : ':' . $this->getPort());
108109

110+
return $host;
111+
}
112+
113+
/**
114+
* {@inheritDoc}
115+
*/
116+
public function getPort()
117+
{
118+
$requestContext = $this->router->getContext();
119+
120+
$port="";
109121
if ($this->usesNonStandardPort()) {
110122
$method = sprintf('get%sPort', ucfirst($requestContext->getScheme()));
111-
$host .= ':' . $requestContext->$method();
123+
$port = $requestContext->$method();
112124
}
113125

114-
return $host;
126+
return $port;
115127
}
116128

117129
/**

Extractor/ExposedRoutesExtractorInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public function getPrefix($locale);
4949
*/
5050
public function getHost();
5151

52+
/**
53+
* Get the port from RequestContext, only if non standard port (Eg: "8080")
54+
*
55+
* @return string
56+
*/
57+
public function getPort();
58+
5259
/**
5360
* Get the scheme from RequestContext
5461
*

Resources/js/router.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Router {
1919
* @param {Object.<string, Router.Route>=} routes
2020
*/
2121
constructor(context, routes) {
22-
this.context_ = context || {base_url: '', prefix: '', host: '', scheme: ''};
22+
this.context_ = context || {base_url: '', prefix: '', host: '', port: '', scheme: ''};
2323
this.setRoutes(routes || {});
2424
}
2525

@@ -52,6 +52,9 @@ class Router {
5252
if ('prefix' in data) {
5353
this.setPrefix(data['prefix']);
5454
}
55+
if ('port' in data) {
56+
this.setPort(data['port']);
57+
}
5558

5659
this.setHost(data['host']);
5760
this.setScheme(data['scheme']);
@@ -120,6 +123,20 @@ class Router {
120123
return this.context_.host;
121124
}
122125

126+
/**
127+
* @param {string} port
128+
*/
129+
setPort(port) {
130+
this.context_.port = port;
131+
}
132+
133+
/**
134+
* @return {string}
135+
*/
136+
getPort() {
137+
return this.context_.port;
138+
};
139+
123140
/**
124141
* Builds query string params added to a URL.
125142
* Port of jQuery's $.param() function, so credit is due there.
@@ -184,7 +201,8 @@ class Router {
184201
unusedParams = Object.assign({}, params),
185202
url = '',
186203
optional = true,
187-
host = '';
204+
host = '',
205+
port = (typeof this.getPort() == "undefined" || this.getPort() === null) ? '' : this.getPort();
188206

189207
route.tokens.forEach((token) => {
190208
if ('text' === token[0]) {
@@ -263,8 +281,8 @@ class Router {
263281
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + url;
264282
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
265283
url = route.schemes[0] + "://" + (host || this.getHost()) + url;
266-
} else if (host && this.getHost() !== host) {
267-
url = this.getScheme() + "://" + host + url;
284+
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
285+
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
268286
} else if (absolute === true) {
269287
url = this.getScheme() + "://" + this.getHost() + url;
270288
}

Resources/js/router.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,19 @@ function testGenerateWithNullValue() {
406406

407407
assertEquals('/blog-post//10', router.generate('posts', { page: null, id: 10 }));
408408
}
409+
410+
function testGenerateWithPort() {
411+
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http", port: 443}, {
412+
homepage: {
413+
tokens: [['text', '/bar']],
414+
defaults: {subdomain: 'api'},
415+
requirements: {},
416+
hosttokens: [
417+
['text', '.localhost'],
418+
['variable', '', '', 'subdomain']
419+
]
420+
}
421+
});
422+
423+
assertEquals('http://api.localhost:443/foo/bar', router.generate('homepage'));
424+
}

Resources/public/js/router.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var Router = function () {
4848
function Router(context, routes) {
4949
_classCallCheck(this, Router);
5050

51-
this.context_ = context || { base_url: '', prefix: '', host: '', scheme: '' };
51+
this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '' };
5252
this.setRoutes(routes || {});
5353
}
5454

@@ -73,6 +73,9 @@ var Router = function () {
7373
if ('prefix' in data) {
7474
this.setPrefix(data['prefix']);
7575
}
76+
if ('port' in data) {
77+
this.setPort(data['port']);
78+
}
7679

7780
this.setHost(data['host']);
7881
this.setScheme(data['scheme']);
@@ -168,6 +171,29 @@ var Router = function () {
168171
return this.context_.host;
169172
}
170173

174+
/**
175+
* @param {string} port
176+
*/
177+
178+
}, {
179+
key: 'setPort',
180+
value: function setPort(port) {
181+
this.context_.port = port;
182+
}
183+
184+
/**
185+
* @return {string}
186+
*/
187+
188+
}, {
189+
key: 'getPort',
190+
value: function getPort() {
191+
return this.context_.port;
192+
}
193+
}, {
194+
key: 'buildQueryParams',
195+
196+
171197
/**
172198
* Builds query string params added to a URL.
173199
* Port of jQuery's $.param() function, so credit is due there.
@@ -176,9 +202,6 @@ var Router = function () {
176202
* @param {Array|Object|string} params
177203
* @param {Function} add
178204
*/
179-
180-
}, {
181-
key: 'buildQueryParams',
182205
value: function buildQueryParams(prefix, params, add) {
183206
var _this = this;
184207

@@ -245,7 +268,8 @@ var Router = function () {
245268
unusedParams = _extends({}, params),
246269
url = '',
247270
optional = true,
248-
host = '';
271+
host = '',
272+
port = typeof this.getPort() == "undefined" || this.getPort() === null ? '' : this.getPort();
249273

250274
route.tokens.forEach(function (token) {
251275
if ('text' === token[0]) {
@@ -324,8 +348,8 @@ var Router = function () {
324348
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + url;
325349
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
326350
url = route.schemes[0] + "://" + (host || this.getHost()) + url;
327-
} else if (host && this.getHost() !== host) {
328-
url = this.getScheme() + "://" + host + url;
351+
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
352+
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
329353
} else if (absolute === true) {
330354
url = this.getScheme() + "://" + this.getHost() + url;
331355
}

Resources/public/js/router.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Response/RoutesResponse.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ class RoutesResponse
1919
private $routes;
2020
private $prefix;
2121
private $host;
22+
private $port;
2223
private $scheme;
2324
private $locale;
2425

25-
public function __construct($baseUrl, RouteCollection $routes = null, $prefix = null, $host = null, $scheme = null, $locale = null)
26+
public function __construct($baseUrl, RouteCollection $routes = null, $prefix = null, $host = null, $port = null, $scheme = null, $locale = null)
2627
{
2728
$this->baseUrl = $baseUrl;
2829
$this->routes = $routes ?: new RouteCollection();
2930
$this->prefix = $prefix;
3031
$this->host = $host;
32+
$this->port = $port;
3133
$this->scheme = $scheme;
3234
$this->locale = $locale;
3335
}
@@ -74,6 +76,11 @@ public function getHost()
7476
return $this->host;
7577
}
7678

79+
public function getPort()
80+
{
81+
return $this->port;
82+
}
83+
7784
public function getScheme()
7885
{
7986
return $this->scheme;

0 commit comments

Comments
 (0)