summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Application.php6
-rw-r--r--src/Config/ConfigServiceProvider.php21
-rw-r--r--src/Database/Db.php9
-rw-r--r--src/Exceptions/Handler.php8
-rw-r--r--src/Http/LegacyUrlGenerator.php31
-rw-r--r--src/Http/RequestServiceProvider.php27
-rw-r--r--src/Http/Response.php2
-rw-r--r--src/Http/UrlGenerator.php9
-rw-r--r--src/Http/UrlGeneratorInterface.php16
-rw-r--r--src/Renderer/Twig/Extensions/Assets.php42
-rw-r--r--src/Renderer/TwigServiceProvider.php2
-rw-r--r--src/helpers.php8
12 files changed, 161 insertions, 20 deletions
diff --git a/src/Application.php b/src/Application.php
index 86397a2c..d12d3e31 100644
--- a/src/Application.php
+++ b/src/Application.php
@@ -106,8 +106,10 @@ class Application extends Container
$this->instance('path', $appPath);
$this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config');
- $this->instance('path.lang', $appPath . DIRECTORY_SEPARATOR . 'locale');
- $this->instance('path.views', $appPath . DIRECTORY_SEPARATOR . 'templates');
+ $this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources');
+ $this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets');
+ $this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views');
+ $this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang');
}
/**
diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php
index 21cbbeb6..63f43ced 100644
--- a/src/Config/ConfigServiceProvider.php
+++ b/src/Config/ConfigServiceProvider.php
@@ -3,25 +3,34 @@
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::class, $config);
$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/Database/Db.php b/src/Database/Db.php
index f34d1564..c0871e68 100644
--- a/src/Database/Db.php
+++ b/src/Database/Db.php
@@ -45,14 +45,19 @@ class Db
*
* @param string $query
* @param array $bindings
- * @return array
+ * @return array|null
*/
public static function selectOne($query, array $bindings = [])
{
$result = self::connection()->selectOne($query, $bindings);
// @TODO: remove typecast
- return (array)$result;
+ $result = (array)$result;
+ if (empty($result)) {
+ return null;
+ }
+
+ return $result;
}
/**
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
index 0503b5b7..b3d840c0 100644
--- a/src/Exceptions/Handler.php
+++ b/src/Exceptions/Handler.php
@@ -76,7 +76,9 @@ class Handler
http_response_code(500);
ob_end_flush();
- $this->die();
+
+ $this->terminateApplicationImmediately();
+
return '';
}
@@ -86,10 +88,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/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/Response.php b/src/Http/Response.php
index d79ab98b..4edf644a 100644
--- a/src/Http/Response.php
+++ b/src/Http/Response.php
@@ -22,7 +22,7 @@ class Response extends SymfonyResponse implements ResponseInterface
public function __construct(
$content = '',
int $status = 200,
- array $headers = array(),
+ array $headers = [],
Renderer $view = null
) {
$this->view = $view;
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 = []);
+}
diff --git a/src/Renderer/Twig/Extensions/Assets.php b/src/Renderer/Twig/Extensions/Assets.php
new file mode 100644
index 00000000..2cdfb0fd
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Assets.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Engelsystem\Http\UrlGenerator;
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Assets extends TwigExtension
+{
+ /** @var UrlGenerator */
+ protected $urlGenerator;
+
+ /**
+ * @param UrlGenerator $urlGenerator
+ */
+ public function __construct(UrlGenerator $urlGenerator)
+ {
+ $this->urlGenerator = $urlGenerator;
+ }
+
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ return [
+ new TwigFunction('asset', [$this, 'getAsset']),
+ ];
+ }
+
+ /**
+ * @param string $path
+ * @return UrlGenerator|string
+ */
+ public function getAsset($path)
+ {
+ $path = ltrim($path, '/');
+
+ return $this->urlGenerator->to('/' . $path);
+ }
+}
diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php
index 0f453989..c70fb303 100644
--- a/src/Renderer/TwigServiceProvider.php
+++ b/src/Renderer/TwigServiceProvider.php
@@ -3,6 +3,7 @@
namespace Engelsystem\Renderer;
use Engelsystem\Container\ServiceProvider;
+use Engelsystem\Renderer\Twig\Extensions\Assets;
use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Globals;
use Engelsystem\Renderer\Twig\Extensions\Session;
@@ -15,6 +16,7 @@ class TwigServiceProvider extends ServiceProvider
{
/** @var array */
protected $extensions = [
+ 'assets' => Assets::class,
'config' => Config::class,
'globals' => Globals::class,
'session' => Session::class,
diff --git a/src/helpers.php b/src/helpers.php
index 3a182bf7..84f26dfa 100644
--- a/src/helpers.php
+++ b/src/helpers.php
@@ -16,13 +16,13 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
* @param string $id
* @return mixed|Application
*/
-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);
}
/**
@@ -156,7 +156,7 @@ function __($key, $replace = [])
/**
* @param string $path
* @param array $parameters
- * @return UrlGenerator|string
+ * @return UrlGeneratorInterface|string
*/
function url($path = null, $parameters = [])
{