summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Application.php25
-rw-r--r--src/Config/Config.php28
-rw-r--r--src/Container/Container.php116
-rw-r--r--src/Container/ContainerException.php11
-rw-r--r--src/Container/NotFoundException.php10
-rw-r--r--src/Exceptions/Handler.php2
-rw-r--r--src/Http/Request.php25
-rw-r--r--src/Logger/EngelsystemLogger.php74
-rw-r--r--src/Renderer/HtmlEngine.php2
-rw-r--r--src/Renderer/Renderer.php27
-rw-r--r--src/Routing/UrlGenerator.php6
-rw-r--r--src/helpers.php43
12 files changed, 280 insertions, 89 deletions
diff --git a/src/Application.php b/src/Application.php
new file mode 100644
index 00000000..674b3869
--- /dev/null
+++ b/src/Application.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Engelsystem;
+
+use Engelsystem\Container\Container;
+use Psr\Container\ContainerInterface;
+
+class Application extends Container
+{
+ public function __construct()
+ {
+ $this->registerBaseBindings();
+ }
+
+ protected function registerBaseBindings()
+ {
+ self::setInstance($this);
+ Container::setInstance($this);
+ $this->instance('app', $this);
+ $this->instance('container', $this);
+ $this->instance(Container::class, $this);
+ $this->instance(Application::class, $this);
+ $this->instance(ContainerInterface::class, $this);
+ }
+}
diff --git a/src/Config/Config.php b/src/Config/Config.php
index 02080de4..34c21a78 100644
--- a/src/Config/Config.php
+++ b/src/Config/Config.php
@@ -2,16 +2,9 @@
namespace Engelsystem\Config;
-use ErrorException;
-
class Config
{
/**
- * @var self
- */
- protected static $instance;
-
- /**
* The config values
*
* @var array
@@ -104,25 +97,4 @@ class Config
{
$this->remove($key);
}
-
- /**
- * @return Config
- * @throws ErrorException
- */
- public static function getInstance()
- {
- if (!self::$instance instanceof self) {
- throw new ErrorException('Config not initialized');
- }
-
- return self::$instance;
- }
-
- /**
- * @param self $instance
- */
- public static function setInstance($instance)
- {
- self::$instance = $instance;
- }
}
diff --git a/src/Container/Container.php b/src/Container/Container.php
new file mode 100644
index 00000000..59a17a04
--- /dev/null
+++ b/src/Container/Container.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Engelsystem\Container;
+
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\ContainerInterface;
+use Psr\Container\NotFoundExceptionInterface;
+
+class Container implements ContainerInterface
+{
+ /**
+ * The globally available container
+ *
+ * @var static
+ */
+ protected static $instance;
+
+ /**
+ * Contains the shared instances
+ *
+ * @var mixed[]
+ */
+ protected $instances = [];
+
+ /**
+ * Finds an entry of the container by its identifier and returns it
+ *
+ * @param string $id Identifier of the entry to look for
+ *
+ * @throws NotFoundExceptionInterface No entry was found for **this** identifier
+ * @throws ContainerExceptionInterface Error while retrieving the entry
+ *
+ * @return mixed Entry
+ */
+ public function get($id)
+ {
+ if ($this->has($id)) {
+ return $this->resolve($id);
+ }
+
+ throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id));
+ }
+
+ /**
+ * Register a shared entry in the container
+ *
+ * @param string $abstract Identifier of the entry to set
+ * @param mixed $instance Entry
+ */
+ public function instance($abstract, $instance)
+ {
+ $this->singleton($abstract, $instance);
+ }
+
+ /**
+ * Register a shared entry as singleton in the container
+ *
+ * @param string $abstract
+ * @param mixed $instance
+ */
+ public function singleton($abstract, $instance)
+ {
+ $this->instances[$abstract] = $instance;
+ }
+
+ /**
+ * Returns true if the container can return an entry for the given identifier
+ * Returns false otherwise
+ *
+ * `has($id)` returning true does not mean that `get($id)` will not throw an exception
+ * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`
+ *
+ * @param string $id Identifier of the entry to look for
+ *
+ * @return bool
+ */
+ public function has($id)
+ {
+ return isset($this->instances[$id]);
+ }
+
+ /**
+ * Resolve the requested object
+ *
+ * @param string $abstract
+ * @return mixed
+ */
+ protected function resolve($abstract)
+ {
+ return $this->instances[$abstract];
+ }
+
+ /**
+ * Get the globally available instance of the container
+ *
+ * @return self
+ */
+ public static function getInstance()
+ {
+ if (is_null(static::$instance)) {
+ static::$instance = new static;
+ }
+
+ return static::$instance;
+ }
+
+ /**
+ * Set the globally available instance of the container
+ *
+ * @param Container $container
+ */
+ public static function setInstance(Container $container)
+ {
+ static::$instance = $container;
+ }
+}
diff --git a/src/Container/ContainerException.php b/src/Container/ContainerException.php
new file mode 100644
index 00000000..3cdde506
--- /dev/null
+++ b/src/Container/ContainerException.php
@@ -0,0 +1,11 @@
+<?php
+
+namespace Engelsystem\Container;
+
+use Exception;
+use Psr\Container\ContainerExceptionInterface;
+
+class ContainerException extends Exception implements ContainerExceptionInterface
+{
+
+}
diff --git a/src/Container/NotFoundException.php b/src/Container/NotFoundException.php
new file mode 100644
index 00000000..a83be0b1
--- /dev/null
+++ b/src/Container/NotFoundException.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Engelsystem\Container;
+
+use Psr\Container\NotFoundExceptionInterface;
+
+class NotFoundException extends ContainerException implements NotFoundExceptionInterface
+{
+
+}
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
index c4fb639c..95bcd132 100644
--- a/src/Exceptions/Handler.php
+++ b/src/Exceptions/Handler.php
@@ -42,7 +42,7 @@ class Handler
/**
* @param Throwable $e
*/
- public function exceptionHandler(Throwable $e)
+ public function exceptionHandler($e)
{
$this->handle(
'exception',
diff --git a/src/Http/Request.php b/src/Http/Request.php
index f0235d45..e7850c8b 100644
--- a/src/Http/Request.php
+++ b/src/Http/Request.php
@@ -2,14 +2,10 @@
namespace Engelsystem\Http;
-use ErrorException;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
class Request extends SymfonyRequest
{
- /** @var self */
- protected static $instance;
-
/**
* Get POST input
*
@@ -68,25 +64,4 @@ class Request extends SymfonyRequest
{
return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
}
-
- /**
- * @return self
- * @throws ErrorException
- */
- public static function getInstance()
- {
- if (!self::$instance instanceof self) {
- throw new ErrorException('Request not initialized');
- }
-
- return self::$instance;
- }
-
- /**
- * @param self $instance
- */
- public static function setInstance($instance)
- {
- self::$instance = $instance;
- }
}
diff --git a/src/Logger/EngelsystemLogger.php b/src/Logger/EngelsystemLogger.php
new file mode 100644
index 00000000..1f255b69
--- /dev/null
+++ b/src/Logger/EngelsystemLogger.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Engelsystem\Logger;
+
+use Psr\Log\AbstractLogger;
+use Psr\Log\InvalidArgumentException;
+use Psr\Log\LogLevel;
+
+class EngelsystemLogger extends AbstractLogger
+{
+ protected $allowedLevels = [
+ LogLevel::ALERT,
+ LogLevel::CRITICAL,
+ LogLevel::DEBUG,
+ LogLevel::EMERGENCY,
+ LogLevel::ERROR,
+ LogLevel::INFO,
+ LogLevel::NOTICE,
+ LogLevel::WARNING,
+ ];
+
+ /**
+ * Logs with an arbitrary level.
+ *
+ * @TODO: Implement $context['exception']
+ *
+ * @param mixed $level
+ * @param string $message
+ * @param array $context
+ *
+ * @throws InvalidArgumentException
+ */
+ public function log($level, $message, array $context = [])
+ {
+ if (!$this->checkLevel($level)) {
+ throw new InvalidArgumentException();
+ }
+
+ $message = $this->interpolate($message, $context);
+
+ LogEntry_create($level, $message);
+ }
+
+ /**
+ * Interpolates context values into the message placeholders.
+ *
+ * @param string $message
+ * @param array $context
+ * @return string
+ */
+ protected function interpolate($message, array $context = [])
+ {
+ foreach ($context as $key => $val) {
+ // check that the value can be casted to string
+ if (is_array($val) || (is_object($val) && !method_exists($val, '__toString'))) {
+ continue;
+ }
+
+ // replace the values of the message
+ $message = str_replace('{' . $key . '}', $val, $message);
+ }
+
+ return $message;
+ }
+
+ /**
+ * @param string $level
+ * @return bool
+ */
+ protected function checkLevel($level)
+ {
+ return in_array($level, $this->allowedLevels);
+ }
+}
diff --git a/src/Renderer/HtmlEngine.php b/src/Renderer/HtmlEngine.php
index 4a48e1f0..75343bbd 100644
--- a/src/Renderer/HtmlEngine.php
+++ b/src/Renderer/HtmlEngine.php
@@ -29,6 +29,6 @@ class HtmlEngine implements EngineInterface
*/
public function canRender($path)
{
- return strpos($path, '.html') && file_exists($path);
+ return strpos($path, '.htm') && file_exists($path);
}
}
diff --git a/src/Renderer/Renderer.php b/src/Renderer/Renderer.php
index bf3d5609..de31ca74 100644
--- a/src/Renderer/Renderer.php
+++ b/src/Renderer/Renderer.php
@@ -2,12 +2,11 @@
namespace Engelsystem\Renderer;
-use ErrorException;
+use Psr\Log\LoggerAwareTrait;
class Renderer
{
- /** @var self */
- protected static $instance;
+ use LoggerAwareTrait;
/** @var EngineInterface[] */
protected $renderer = [];
@@ -29,7 +28,10 @@ class Renderer
return $renderer->get($template, $data);
}
- engelsystem_error('Unable to find a renderer for template file &laquo;' . $template . '&raquo;.');
+ if ($this->logger) {
+ $this->logger->error('Unable to find a renderer for template file "{file}"', ['file' => $template]);
+ }
+
return '';
}
@@ -42,21 +44,4 @@ class Renderer
{
$this->renderer[] = $renderer;
}
-
- /**
- * @return self
- * @throws ErrorException
- */
- public static function getInstance()
- {
- return self::$instance;
- }
-
- /**
- * @param self $instance
- */
- public static function setInstance($instance)
- {
- self::$instance = $instance;
- }
}
diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php
index 8dc464c6..6df52425 100644
--- a/src/Routing/UrlGenerator.php
+++ b/src/Routing/UrlGenerator.php
@@ -2,8 +2,6 @@
namespace Engelsystem\Routing;
-use Engelsystem\Http\Request;
-
class UrlGenerator
{
/**
@@ -11,10 +9,10 @@ class UrlGenerator
* @param array $parameters
* @return string
*/
- public static function to($path, $parameters = [])
+ public function to($path, $parameters = [])
{
$path = '/' . ltrim($path, '/');
- $request = Request::getInstance();
+ $request = app('request');
$uri = $request->getUriForPath($path);
if (!empty($parameters) && is_array($parameters)) {
diff --git a/src/helpers.php b/src/helpers.php
index 24f93f2c..de303963 100644
--- a/src/helpers.php
+++ b/src/helpers.php
@@ -1,6 +1,7 @@
<?php
// Some useful functions
+use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer;
@@ -8,6 +9,21 @@ use Engelsystem\Routing\UrlGenerator;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
+ * Get the global app instance
+ *
+ * @param string $id
+ * @return mixed
+ */
+function app($id = null)
+{
+ if (is_null($id)) {
+ return Application::getInstance();
+ }
+
+ return Application::getInstance()->get($id);
+}
+
+/**
* Get or set config values
*
* @param string|array $key
@@ -16,15 +32,18 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
*/
function config($key = null, $default = null)
{
+ $config = app('config');
+
if (empty($key)) {
- return Config::getInstance();
+ return $config;
}
if (is_array($key)) {
- Config::getInstance()->set($key);
+ $config->set($key);
+ return true;
}
- return Config::getInstance()->get($key, $default);
+ return $config->get($key, $default);
}
/**
@@ -34,7 +53,7 @@ function config($key = null, $default = null)
*/
function request($key = null, $default = null)
{
- $request = Request::getInstance();
+ $request = app('request');
if (is_null($key)) {
return $request;
@@ -50,7 +69,7 @@ function request($key = null, $default = null)
*/
function session($key = null, $default = null)
{
- $session = request()->getSession();
+ $session = app('session');
if (is_null($key)) {
return $session;
@@ -66,7 +85,7 @@ function session($key = null, $default = null)
*/
function view($template = null, $data = null)
{
- $renderer = Renderer::getInstance();
+ $renderer = app('renderer');
if (is_null($template)) {
return $renderer;
@@ -78,9 +97,15 @@ function view($template = null, $data = null)
/**
* @param string $path
* @param array $parameters
- * @return string
+ * @return UrlGenerator|string
*/
-function url($path, $parameters = [])
+function url($path = null, $parameters = [])
{
- return UrlGenerator::to($path, $parameters);
+ $urlGenerator = app('routing.urlGenerator');
+
+ if (is_null($path)) {
+ return $urlGenerator;
+ }
+
+ return $urlGenerator->to($path, $parameters);
}