diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Config/ConfigServiceProvider.php | 21 | ||||
-rw-r--r-- | src/Exceptions/Handler.php | 6 | ||||
-rw-r--r-- | src/Http/RequestServiceProvider.php | 27 | ||||
-rw-r--r-- | src/Routing/LegacyUrlGenerator.php | 31 | ||||
-rw-r--r-- | src/Routing/RoutingServiceProvider.php | 14 | ||||
-rw-r--r-- | src/Routing/UrlGenerator.php | 11 | ||||
-rw-r--r-- | src/Routing/UrlGeneratorInterface.php | 16 | ||||
-rw-r--r-- | src/helpers.php | 12 |
8 files changed, 118 insertions, 20 deletions
diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php index 01b648df..9fbccd68 100644 --- a/src/Config/ConfigServiceProvider.php +++ b/src/Config/ConfigServiceProvider.php @@ -3,24 +3,33 @@ namespace Engelsystem\Config; use Engelsystem\Container\ServiceProvider; +use Exception; class ConfigServiceProvider extends ServiceProvider { + /** @var array */ + protected $configFiles = ['config.default.php', 'config.php']; + public function register() { - $defaultConfigFile = config_path('config.default.php'); - $configFile = config_path('config.php'); - $config = $this->app->make(Config::class); $this->app->instance('config', $config); - $config->set(require $defaultConfigFile); + foreach ($this->configFiles as $file) { + $file = config_path($file); + + if (!file_exists($file)) { + continue; + } - if (file_exists($configFile)) { $config->set(array_replace_recursive( $config->get(null), - require $configFile + require $file )); } + + if (empty($config->get(null))) { + throw new Exception('Configuration not found'); + } } } diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index ee15717a..6b3300e6 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -64,7 +64,7 @@ class Handler $handler = $this->handler[$this->environment]; $handler->report($e); $handler->render($this->request, $e); - $this->die(); + $this->terminateApplicationImmediately(); } /** @@ -73,10 +73,10 @@ class Handler * @codeCoverageIgnore * @param string $message */ - protected function die($message = '') + protected function terminateApplicationImmediately($message = '') { echo $message; - die(); + die(1); } /** 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/Routing/LegacyUrlGenerator.php b/src/Routing/LegacyUrlGenerator.php new file mode 100644 index 00000000..fdac4f96 --- /dev/null +++ b/src/Routing/LegacyUrlGenerator.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Routing; + +/** + * 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 linkTo($path, $parameters = []) + { + $page = ltrim($path, '/'); + if (!empty($page)) { + $page = str_replace('-', '_', $page); + $parameters = array_merge(['p' => $page], $parameters); + } + + $uri = parent::linkTo('index.php', $parameters); + $uri = preg_replace('~(/index\.php)+~', '/index.php', $uri); + $uri = preg_replace('~(/index\.php)$~', '/', $uri); + + return $uri; + } +} diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php index b7db1383..beaa6a94 100644 --- a/src/Routing/RoutingServiceProvider.php +++ b/src/Routing/RoutingServiceProvider.php @@ -1,14 +1,24 @@ <?php - namespace Engelsystem\Routing; use Engelsystem\Container\ServiceProvider; +/** + * Registers the url generator depending on config. + */ class RoutingServiceProvider extends ServiceProvider { + public function register() { - $urlGenerator = $this->app->make(UrlGenerator::class); + $config = $this->app->get('config'); + $class = UrlGenerator::class; + if (! $config->get('rewrite_urls', true)) { + $class = LegacyUrlGenerator::class; + } + + $urlGenerator = $this->app->make($class); $this->app->instance('routing.urlGenerator', $urlGenerator); + $this->app->bind(UrlGeneratorInterface::class, 'routing.urlGenerator'); } } diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php index 6df52425..188bac3b 100644 --- a/src/Routing/UrlGenerator.php +++ b/src/Routing/UrlGenerator.php @@ -2,14 +2,19 @@ namespace Engelsystem\Routing; -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 = []) + public function linkTo($path, $parameters = []) { $path = '/' . ltrim($path, '/'); $request = app('request'); diff --git a/src/Routing/UrlGeneratorInterface.php b/src/Routing/UrlGeneratorInterface.php new file mode 100644 index 00000000..f1a8ffed --- /dev/null +++ b/src/Routing/UrlGeneratorInterface.php @@ -0,0 +1,16 @@ +<?php + +namespace Engelsystem\Routing; + +/** + * To switch between different URL schemes. + */ +interface UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public function linkTo($path, $parameters = []); +} diff --git a/src/helpers.php b/src/helpers.php index c3c727ec..339936e3 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,7 +5,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; -use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -14,13 +14,13 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface; * @param string $id * @return mixed */ -function app($id = null) +function app($instance_id = null) { - if (is_null($id)) { + if (is_null($instance_id)) { return Application::getInstance(); } - return Application::getInstance()->get($id); + return Application::getInstance()->get($instance_id); } /** @@ -99,7 +99,7 @@ function session($key = null, $default = null) /** * @param string $path * @param array $parameters - * @return UrlGenerator|string + * @return UrlGeneratorInterface|string */ function url($path = null, $parameters = []) { @@ -109,7 +109,7 @@ function url($path = null, $parameters = []) return $urlGenerator; } - return $urlGenerator->to($path, $parameters); + return $urlGenerator->linkTo($path, $parameters); } /** |