Skip to content

Commit

Permalink
Merge pull request #320 from emulienfou/master
Browse files Browse the repository at this point in the history
Fixing issue #319
  • Loading branch information
lsmith77 committed Sep 29, 2018
2 parents cbe7ba6 + 2ccdbb5 commit fce90c6
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 25 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Expand Up @@ -2,9 +2,7 @@ language: php

matrix:
include:
- php: 5.3
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
dist: precise
- php: 5.6
- php: 7.1
- php: 7.2
- php: 7.1
Expand Down
1 change: 1 addition & 0 deletions Command/DumpCommand.php
Expand Up @@ -167,6 +167,7 @@ private function doDump(InputInterface $input, OutputInterface $output)
$extractor->getRoutes(),
$extractor->getPrefix($input->getOption('locale')),
$extractor->getHost(),
$extractor->getPort(),
$extractor->getScheme()
),
'json',
Expand Down
1 change: 1 addition & 0 deletions Controller/Controller.php
Expand Up @@ -96,6 +96,7 @@ public function indexAction(Request $request, $_format)
$exposedRoutes,
$this->exposedRoutesExtractor->getPrefix($request->getLocale()),
$this->exposedRoutesExtractor->getHost(),
$this->exposedRoutesExtractor->getPort(),
$this->exposedRoutesExtractor->getScheme(),
$request->getLocale()
);
Expand Down
18 changes: 15 additions & 3 deletions Extractor/ExposedRoutesExtractor.php
Expand Up @@ -104,14 +104,26 @@ public function getHost()
{
$requestContext = $this->router->getContext();

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

return $host;
}

/**
* {@inheritDoc}
*/
public function getPort()
{
$requestContext = $this->router->getContext();

$port="";
if ($this->usesNonStandardPort()) {
$method = sprintf('get%sPort', ucfirst($requestContext->getScheme()));
$host .= ':' . $requestContext->$method();
$port = $requestContext->$method();
}

return $host;
return $port;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions Extractor/ExposedRoutesExtractorInterface.php
Expand Up @@ -49,6 +49,13 @@ public function getPrefix($locale);
*/
public function getHost();

/**
* Get the port from RequestContext, only if non standard port (Eg: "8080")
*
* @return string
*/
public function getPort();

/**
* Get the scheme from RequestContext
*
Expand Down
26 changes: 22 additions & 4 deletions Resources/js/router.js
Expand Up @@ -19,7 +19,7 @@ class Router {
* @param {Object.<string, Router.Route>=} routes
*/
constructor(context, routes) {
this.context_ = context || {base_url: '', prefix: '', host: '', scheme: ''};
this.context_ = context || {base_url: '', prefix: '', host: '', port: '', scheme: ''};
this.setRoutes(routes || {});
}

Expand Down Expand Up @@ -52,6 +52,9 @@ class Router {
if ('prefix' in data) {
this.setPrefix(data['prefix']);
}
if ('port' in data) {
this.setPort(data['port']);
}

this.setHost(data['host']);
this.setScheme(data['scheme']);
Expand Down Expand Up @@ -120,6 +123,20 @@ class Router {
return this.context_.host;
}

/**
* @param {string} port
*/
setPort(port) {
this.context_.port = port;
}

/**
* @return {string}
*/
getPort() {
return this.context_.port;
};

/**
* Builds query string params added to a URL.
* Port of jQuery's $.param() function, so credit is due there.
Expand Down Expand Up @@ -184,7 +201,8 @@ class Router {
unusedParams = Object.assign({}, params),
url = '',
optional = true,
host = '';
host = '',
port = (typeof this.getPort() == "undefined" || this.getPort() === null) ? '' : this.getPort();

route.tokens.forEach((token) => {
if ('text' === token[0]) {
Expand Down Expand Up @@ -263,8 +281,8 @@ class Router {
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + url;
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
url = route.schemes[0] + "://" + (host || this.getHost()) + url;
} else if (host && this.getHost() !== host) {
url = this.getScheme() + "://" + host + url;
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
} else if (absolute === true) {
url = this.getScheme() + "://" + this.getHost() + url;
}
Expand Down
16 changes: 16 additions & 0 deletions Resources/js/router.test.js
Expand Up @@ -406,3 +406,19 @@ function testGenerateWithNullValue() {

assertEquals('/blog-post//10', router.generate('posts', { page: null, id: 10 }));
}

function testGenerateWithPort() {
var router = new fos.Router({base_url: '/foo', host: "localhost", scheme: "http", port: 443}, {
homepage: {
tokens: [['text', '/bar']],
defaults: {subdomain: 'api'},
requirements: {},
hosttokens: [
['text', '.localhost'],
['variable', '', '', 'subdomain']
]
}
});

assertEquals('http://api.localhost:443/foo/bar', router.generate('homepage'));
}
38 changes: 31 additions & 7 deletions Resources/public/js/router.js
Expand Up @@ -48,7 +48,7 @@ var Router = function () {
function Router(context, routes) {
_classCallCheck(this, Router);

this.context_ = context || { base_url: '', prefix: '', host: '', scheme: '' };
this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '' };
this.setRoutes(routes || {});
}

Expand All @@ -73,6 +73,9 @@ var Router = function () {
if ('prefix' in data) {
this.setPrefix(data['prefix']);
}
if ('port' in data) {
this.setPort(data['port']);
}

this.setHost(data['host']);
this.setScheme(data['scheme']);
Expand Down Expand Up @@ -168,6 +171,29 @@ var Router = function () {
return this.context_.host;
}

/**
* @param {string} port
*/

}, {
key: 'setPort',
value: function setPort(port) {
this.context_.port = port;
}

/**
* @return {string}
*/

}, {
key: 'getPort',
value: function getPort() {
return this.context_.port;
}
}, {
key: 'buildQueryParams',


/**
* Builds query string params added to a URL.
* Port of jQuery's $.param() function, so credit is due there.
Expand All @@ -176,9 +202,6 @@ var Router = function () {
* @param {Array|Object|string} params
* @param {Function} add
*/

}, {
key: 'buildQueryParams',
value: function buildQueryParams(prefix, params, add) {
var _this = this;

Expand Down Expand Up @@ -245,7 +268,8 @@ var Router = function () {
unusedParams = _extends({}, params),
url = '',
optional = true,
host = '';
host = '',
port = typeof this.getPort() == "undefined" || this.getPort() === null ? '' : this.getPort();

route.tokens.forEach(function (token) {
if ('text' === token[0]) {
Expand Down Expand Up @@ -324,8 +348,8 @@ var Router = function () {
url = route.requirements["_scheme"] + "://" + (host || this.getHost()) + url;
} else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) {
url = route.schemes[0] + "://" + (host || this.getHost()) + url;
} else if (host && this.getHost() !== host) {
url = this.getScheme() + "://" + host + url;
} else if (host && this.getHost() !== host + ('' === port ? '' : ':' + port)) {
url = this.getScheme() + "://" + host + ('' === port ? '' : ':' + port) + url;
} else if (absolute === true) {
url = this.getScheme() + "://" + this.getHost() + url;
}
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/js/router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Response/RoutesResponse.php
Expand Up @@ -19,15 +19,17 @@ class RoutesResponse
private $routes;
private $prefix;
private $host;
private $port;
private $scheme;
private $locale;

public function __construct($baseUrl, RouteCollection $routes = null, $prefix = null, $host = null, $scheme = null, $locale = null)
public function __construct($baseUrl, RouteCollection $routes = null, $prefix = null, $host = null, $port = null, $scheme = null, $locale = null)
{
$this->baseUrl = $baseUrl;
$this->routes = $routes ?: new RouteCollection();
$this->prefix = $prefix;
$this->host = $host;
$this->port = $port;
$this->scheme = $scheme;
$this->locale = $locale;
}
Expand Down Expand Up @@ -74,6 +76,11 @@ public function getHost()
return $this->host;
}

public function getPort()
{
return $this->port;
}

public function getScheme()
{
return $this->scheme;
Expand Down
1 change: 1 addition & 0 deletions Serializer/Normalizer/RoutesResponseNormalizer.php
Expand Up @@ -29,6 +29,7 @@ public function normalize($data, $format = null, array $context = array())
'routes' => $data->getRoutes(),
'prefix' => $data->getPrefix(),
'host' => $data->getHost(),
'port' => $data->getPort(),
'scheme' => $data->getScheme(),
);
}
Expand Down

0 comments on commit fce90c6

Please sign in to comment.