diff options
Diffstat (limited to 'src/Http')
-rw-r--r-- | src/Http/LegacyUrlGenerator.php | 31 | ||||
-rw-r--r-- | src/Http/RequestServiceProvider.php | 27 | ||||
-rw-r--r-- | src/Http/UrlGenerator.php | 9 | ||||
-rw-r--r-- | src/Http/UrlGeneratorInterface.php | 16 |
4 files changed, 81 insertions, 2 deletions
diff --git a/src/Http/LegacyUrlGenerator.php b/src/Http/LegacyUrlGenerator.php new file mode 100644 index 00000000..b9f8b7f1 --- /dev/null +++ b/src/Http/LegacyUrlGenerator.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Http; + +/** + * Provides urls when webserver rewriting is disabled. + * + * The urls have the form <app url>/index.php?p=<path>&<parameters> + */ +class LegacyUrlGenerator extends UrlGenerator +{ + /** + * @param string $path + * @param array $parameters + * @return string urls in the form <app url>/index.php?p=<path>&<parameters> + */ + public function to($path, $parameters = []) + { + $page = ltrim($path, '/'); + if (!empty($page)) { + $page = str_replace('-', '_', $page); + $parameters = array_merge(['p' => $page], $parameters); + } + + $uri = parent::to('index.php', $parameters); + $uri = preg_replace('~(/index\.php)+~', '/index.php', $uri); + $uri = preg_replace('~(/index\.php)$~', '/', $uri); + + return $uri; + } +} diff --git a/src/Http/RequestServiceProvider.php b/src/Http/RequestServiceProvider.php index 077e9ecc..bbf2579c 100644 --- a/src/Http/RequestServiceProvider.php +++ b/src/Http/RequestServiceProvider.php @@ -8,7 +8,34 @@ class RequestServiceProvider extends ServiceProvider { public function register() { + $config = $this->app->get('config'); + $trustedProxies = $config->get('trusted_proxies', []); + + if (!is_array($trustedProxies)) { + $trustedProxies = empty($trustedProxies) ? [] : explode(',', preg_replace('~\s+~', '', $trustedProxies)); + } + + /** @var Request $request */ $request = $this->app->call([Request::class, 'createFromGlobals']); + $this->setTrustedProxies($request, $trustedProxies); $this->app->instance('request', $request); } + + /** + * Set the trusted Proxies + * + * Required for unit tests (static methods can't be mocked) + * + * @param Request $request + * @param array $proxies + * @param int $trustedHeadersSet + * @codeCoverageIgnore + */ + protected function setTrustedProxies( + $request, + $proxies, + $trustedHeadersSet = Request::HEADER_FORWARDED | Request::HEADER_X_FORWARDED_ALL + ) { + $request->setTrustedProxies($proxies, $trustedHeadersSet); + } } diff --git a/src/Http/UrlGenerator.php b/src/Http/UrlGenerator.php index 132fefc7..7ced769e 100644 --- a/src/Http/UrlGenerator.php +++ b/src/Http/UrlGenerator.php @@ -2,12 +2,17 @@ namespace Engelsystem\Http; -class UrlGenerator +/** + * Provides urls when rewriting on the webserver is enabled. (default) + * + * The urls have the form <app url>/<path>?<parameters> + */ +class UrlGenerator implements UrlGeneratorInterface { /** * @param string $path * @param array $parameters - * @return string + * @return string url in the form [app url]/[path]?[parameters] */ public function to($path, $parameters = []) { diff --git a/src/Http/UrlGeneratorInterface.php b/src/Http/UrlGeneratorInterface.php new file mode 100644 index 00000000..b3b6b12d --- /dev/null +++ b/src/Http/UrlGeneratorInterface.php @@ -0,0 +1,16 @@ +<?php + +namespace Engelsystem\Http; + +/** + * To switch between different URL schemes. + */ +interface UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public function to($path, $parameters = []); +} |