Skip to content

Commit ee350f2

Browse files
committed
feature #9 A proxy URL with the port number (@iteman)
This PR was merged into the 1.2-dev branch. Discussion ---------- A proxy URL with the port number | Q | A | ------------- | --- | Branch? | `master` | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | BSD-2-Clause This PR provides a feature that allows you to specify the proxy URL with the port number as the following: ```yaml ... phpmentors_proxy_url_rewrite: proxy_urls: foo_bar: path: "!^.*!" proxy_url: "http://www.example.com:8080/foo/bar" ``` Commits ------- 54dd0b4 add a feature that allows you to specify the proxy URL with the port number as the following: bce0bd2 fix CS
2 parents 136604d + bce0bd2 commit ee350f2

13 files changed

+84
-45
lines changed

src/Asset/ProxyPackages.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function setProxyUrlMatcher(ProxyUrlMatcher $proxyUrlMatcher)
4343
}
4444

4545
/**
46-
* {@inheritDoc}
46+
* {@inheritdoc}
4747
*/
4848
public function getUrl($path, $packageName = null)
4949
{
@@ -71,8 +71,14 @@ public function getUrl($path, $packageName = null)
7171
if ($matchedProxyUrl->getHost() !== null) {
7272
$requestContext->setHost($matchedProxyUrl->getHost());
7373
}
74-
if ($matchedProxyUrl->getScheme() !== null) {
75-
$requestContext->setScheme($matchedProxyUrl->getScheme());
74+
$requestContext->setScheme($matchedProxyUrl->getScheme());
75+
76+
if ($matchedProxyUrl->getPort() !== null) {
77+
if ($matchedProxyUrl->getScheme() == 'http') {
78+
$requestContext->setHttpPort($matchedProxyUrl->getPort());
79+
} elseif ($matchedProxyUrl->getScheme() == 'https') {
80+
$requestContext->setHttpsPort($matchedProxyUrl->getPort());
81+
}
7682
}
7783

7884
$urlGenerator = new UrlGenerator($routeCollection, $requestContext);

src/DependencyInjection/Compiler/ReplaceAssetExtensionAndPakcagesDefinitionPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class ReplaceAssetExtensionAndPakcagesDefinitionPass implements CompilerPassInterface
1919
{
2020
/**
21-
* {@inheritDoc}
21+
* {@inheritdoc}
2222
*/
2323
public function process(ContainerBuilder $container)
2424
{

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Configuration implements ConfigurationInterface
2020
{
2121
/**
22-
* {@inheritDoc}
22+
* {@inheritdoc}
2323
*/
2424
public function getConfigTreeBuilder()
2525
{

src/DependencyInjection/PHPMentorsProxyURLRewriteExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class PHPMentorsProxyURLRewriteExtension extends Extension
2323
{
2424
/**
25-
* {@inheritDoc}
25+
* {@inheritdoc}
2626
*/
2727
public function load(array $configs, ContainerBuilder $container)
2828
{
@@ -35,7 +35,7 @@ public function load(array $configs, ContainerBuilder $container)
3535
}
3636

3737
/**
38-
* {@inheritDoc}
38+
* {@inheritdoc}
3939
*/
4040
public function getAlias()
4141
{

src/EventListener/ProxyUrlRewriteListener.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,18 @@ public function onKernelRequest(GetResponseEvent $event)
5858
$matchedProxyUrl = $this->proxyUrlMatcher->match($this->router->getContext()->getPathInfo());
5959
if ($matchedProxyUrl !== null) {
6060
$this->router->getContext()->setBaseUrl($matchedProxyUrl->getPath().$this->router->getContext()->getBaseUrl());
61-
62-
if ($matchedProxyUrl->getScheme() !== null) {
63-
$this->router->getContext()->setScheme($matchedProxyUrl->getScheme());
64-
}
61+
$this->router->getContext()->setScheme($matchedProxyUrl->getScheme());
6562

6663
if ($matchedProxyUrl->getHost() !== null) {
6764
$this->router->getContext()->setHost($matchedProxyUrl->getHost());
6865
}
6966

70-
if ($this->router->getContext()->getScheme() == 'http') {
71-
$this->router->getContext()->setHttpPort('80');
72-
} elseif ($this->router->getContext()->getScheme() == 'https') {
73-
$this->router->getContext()->setHttpsPort('443');
67+
if ($matchedProxyUrl->getPort() !== null) {
68+
if ($this->router->getContext()->getScheme() == 'http') {
69+
$this->router->getContext()->setHttpPort($matchedProxyUrl->getPort());
70+
} elseif ($this->router->getContext()->getScheme() == 'https') {
71+
$this->router->getContext()->setHttpsPort($matchedProxyUrl->getPort());
72+
}
7473
}
7574
}
7675
}

src/PHPMentorsProxyURLRewriteBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
class PHPMentorsProxyURLRewriteBundle extends Bundle
2121
{
2222
/**
23-
* {@inheritDoc}
23+
* {@inheritdoc}
2424
*/
2525
public function build(ContainerBuilder $container)
2626
{
2727
$container->addCompilerPass(new ReplaceAssetExtensionAndPakcagesDefinitionPass());
2828
}
2929

3030
/**
31-
* {@inheritDoc}
31+
* {@inheritdoc}
3232
*/
3333
public function getContainerExtension()
3434
{

src/ProxyUrl/ProxyUrl.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,43 @@ class ProxyUrl implements EntityInterface, IdentifiableInterface
4444
*/
4545
private $target;
4646

47+
/**
48+
* @var int
49+
*
50+
* @since Property available since Release 1.2.0
51+
*/
52+
private $port;
53+
4754
/**
4855
* @param int|string $id
4956
* @param string $target
5057
* @param string $path
5158
* @param string $host
5259
* @param string $scheme
60+
* @param int $port
5361
*/
54-
public function __construct($id, $target, $path, $host, $scheme)
62+
public function __construct($id, $target, $path, $host, $scheme, $port)
5563
{
5664
$this->id = $id;
5765
$this->target = $target;
5866
$this->path = rtrim($path, '/');
5967
$this->host = $host;
60-
$this->scheme = $scheme;
68+
69+
if ($scheme === null) {
70+
$this->scheme = 'http';
71+
} else {
72+
$this->scheme = $scheme;
73+
}
74+
75+
if ($port === null) {
76+
if ($this->scheme == 'http') {
77+
$this->port = 80;
78+
} elseif ($this->scheme == 'https') {
79+
$this->port = 443;
80+
}
81+
} else {
82+
$this->port = $port;
83+
}
6184
}
6285

6386
/**
@@ -69,7 +92,7 @@ public function getHost()
6992
}
7093

7194
/**
72-
* {@inheritDoc}
95+
* {@inheritdoc}
7396
*
7497
* @return int|string
7598
*
@@ -103,4 +126,12 @@ public function getTarget()
103126
{
104127
return $this->target;
105128
}
129+
130+
/**
131+
* @return int
132+
*/
133+
public function getPort()
134+
{
135+
return $this->port;
136+
}
106137
}

src/ProxyUrl/ProxyUrlCollection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ProxyUrlCollection implements EntityCollectionInterface
2323
private $proxyUrls = array();
2424

2525
/**
26-
* {@inheritDoc}
26+
* {@inheritdoc}
2727
*/
2828
public function add(EntityInterface $entity)
2929
{
@@ -33,23 +33,23 @@ public function add(EntityInterface $entity)
3333
}
3434

3535
/**
36-
* {@inheritDoc}
36+
* {@inheritdoc}
3737
*/
3838
public function count()
3939
{
4040
return count($this->proxyUrls);
4141
}
4242

4343
/**
44-
* {@inheritDoc}
44+
* {@inheritdoc}
4545
*/
4646
public function getIterator()
4747
{
4848
return new \ArrayIterator($this->proxyUrls);
4949
}
5050

5151
/**
52-
* {@inheritDoc}
52+
* {@inheritdoc}
5353
*/
5454
public function get($key)
5555
{
@@ -61,15 +61,15 @@ public function get($key)
6161
}
6262

6363
/**
64-
* {@inheritDoc}
64+
* {@inheritdoc}
6565
*/
6666
public function remove(EntityInterface $entity)
6767
{
6868
assert($entity instanceof ProxyUrl);
6969
}
7070

7171
/**
72-
* {@inheritDoc}
72+
* {@inheritdoc}
7373
*/
7474
public function toArray()
7575
{

src/ProxyUrl/ProxyUrlFactory.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class ProxyUrlFactory implements ServiceInterface
2525
*/
2626
public function create($proxyUrlId, $path, $proxyUrl)
2727
{
28-
list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme) = static::parseUrl($proxyUrl);
28+
list($proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort) = static::parseUrl($proxyUrl);
2929

30-
return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme);
30+
return new ProxyUrl($proxyUrlId, $path, $proxyUrlPath, $proxyUrlHost, $proxyUrlScheme, $proxyUrlPort);
3131
}
3232

3333
/**
@@ -46,13 +46,10 @@ public static function parseUrl($url)
4646
throw new \UnexpectedValueException(sprintf('The proxy URL "%s" is malformed.', $url));
4747
}
4848

49-
if (array_key_exists('port', $components)) {
50-
throw new \UnexpectedValueException(sprintf('The proxy URL "%s" cannot contain port number.', $url));
51-
}
52-
5349
$path = array_key_exists('path', $components) ? $components['path'] : null;
5450
$host = array_key_exists('host', $components) ? $components['host'] : null;
5551
$scheme = array_key_exists('scheme', $components) ? $components['scheme'] : null;
52+
$port = array_key_exists('port', $components) ? $components['port'] : null;
5653

5754
if (strpos($path, '//') === 0) {
5855
$endOfHostPosition = strpos($path, '/', 2);
@@ -67,6 +64,6 @@ public static function parseUrl($url)
6764
}
6865
}
6966

70-
return array($path, $host, $scheme);
67+
return array($path, $host, $scheme, $port);
7168
}
7269
}

src/Templating/ProxyAssetExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class ProxyAssetExtension extends AssetExtension
2121
{
2222
/**
23-
* {@inheritDoc}
23+
* {@inheritdoc}
2424
*/
2525
public function getAssetUrl($path, $packageName = null, $absolute = false, $version = null)
2626
{

0 commit comments

Comments
 (0)