Skip to content

Commit

Permalink
Options for router
Browse files Browse the repository at this point in the history
Option: "X-Forwarded-Prefix" (boolean, or string)
  • Loading branch information
ElGigi committed Nov 4, 2024
1 parent 2e289e4 commit bc055f4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.3.0] - 2024-11-04

### Added

- Options for router
- Option: "X-Forwarded-Prefix" (boolean, or string)

## [2.2.0] - 2024-10-18

### Added
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ $router->addRoute(
$route = $router->handle($serverRequest);
```

#### Options

| Options | Type | Description |
|--------------------|----------------|-------------------------------------------------------------------------------------------|
| X-Forwarded-Prefix | boolean/string | Default to **false**, **true** to use "X-Forwarded-Prefix" value or custom name of header |

#### Generate path

You can generate a path with some parameters directly with `Router` object.
Expand Down
22 changes: 20 additions & 2 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ class Router implements RouterInterface
use LoggerAwareTrait;
use RouteSetTrait;

private array $options = [
'X-Forwarded-Prefix' => false,
];

/**
* Router constructor.
*
* @param array $options
* @param LoggerInterface|null $logger
*/
public function __construct(?LoggerInterface $logger = null)
public function __construct(array $options = [], ?LoggerInterface $logger = null)
{
if (null !== $logger) {
$this->setLogger($logger);
}

$this->options = array_replace($this->options, $options);
}

/**
Expand Down Expand Up @@ -97,7 +104,18 @@ public function generate(string $name, array|RouteAttributes $parameters = []):
throw new NotFoundException(sprintf('Route "%s" does not exists', $name));
}

return $route->generate($parameters);
$str = $route->generate($parameters);

// X-Forwarded-Prefix
if (false !== $this->options['X-Forwarded-Prefix']) {
$xForwardedPrefix = $this->options['X-Forwarded-Prefix'] === true ? 'X-Forwarded-Prefix' : (string)$this->options['X-Forwarded-Prefix'];
$xForwardedPrefix = 'HTTP_' . strtoupper(str_replace('-', '_', $xForwardedPrefix));
if (!empty($prefix = $_SERVER[$xForwardedPrefix] ?? null)) {
$str = '/' . trim($prefix, '/') . $str;
}
}

return $str;
}

private function generateParameters(array|RouteAttributes $parameters = []): array
Expand Down
53 changes: 53 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

class RouterTest extends AbstractTestCase
{
protected function setUp(): void
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = null;
}

public function testSerialization()
{
$router = new Router;
Expand Down Expand Up @@ -101,6 +106,54 @@ public function testGenerate()
);
}

public function testGenerate_withForwardedPrefix_enable()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/super-prefix/';

$router = new Router(['X-Forwarded-Prefix' => true]);
$router->addRoute(new Route('/path/{attr1}/sub-path', name: 'route1'));

$this->assertEquals(
'/super-prefix/path/test/sub-path',
$router->generate(
'route1',
['attr1' => 'test']
)
);
}

public function testGenerate_withForwardedPrefix_disable()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/super-prefix/';

$router = new Router(['X-Forwarded-Prefix' => false]);
$router->addRoute(new Route('/path/{attr1}/sub-path', name: 'route1'));

$this->assertEquals(
'/path/test/sub-path',
$router->generate(
'route1',
['attr1' => 'test']
)
);
}

public function testGenerate_withForwardedPrefix_custom()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX_CUSTOM'] = '/super-prefix/';

$router = new Router(['X-Forwarded-Prefix' => 'X-Forwarded-Prefix-Custom']);
$router->addRoute(new Route('/path/{attr1}/sub-path', name: 'route1'));

$this->assertEquals(
'/super-prefix/path/test/sub-path',
$router->generate(
'route1',
['attr1' => 'test']
)
);
}

public function testGenerateWithMissingAttributes()
{
$router = new Router;
Expand Down

0 comments on commit bc055f4

Please sign in to comment.