summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Config/Config.php128
-rw-r--r--src/Database/Db.php170
-rw-r--r--src/Exceptions/Handler.php97
-rw-r--r--src/helpers.php24
4 files changed, 419 insertions, 0 deletions
diff --git a/src/Config/Config.php b/src/Config/Config.php
new file mode 100644
index 00000000..02080de4
--- /dev/null
+++ b/src/Config/Config.php
@@ -0,0 +1,128 @@
+<?php
+
+namespace Engelsystem\Config;
+
+use ErrorException;
+
+class Config
+{
+ /**
+ * @var self
+ */
+ protected static $instance;
+
+ /**
+ * The config values
+ *
+ * @var array
+ */
+ protected $data = [];
+
+ /**
+ * @param string|null $key
+ * @param mixed $default
+ * @return mixed
+ */
+ public function get($key, $default = null)
+ {
+ if (is_null($key)) {
+ return $this->data;
+ }
+
+ if ($this->has($key)) {
+ return $this->data[$key];
+ }
+
+ return $default;
+ }
+
+ /**
+ * @param string|array $key
+ * @param mixed $value
+ */
+ public function set($key, $value = null)
+ {
+ if (is_array($key)) {
+ foreach ($key as $configKey => $configValue) {
+ $this->set($configKey, $configValue);
+ }
+
+ return;
+ }
+
+ $this->data[$key] = $value;
+ }
+
+ /**
+ * @param string $key
+ * @return bool
+ */
+ public function has($key)
+ {
+ return isset($this->data[$key]);
+ }
+
+ /**
+ * @param string $key
+ */
+ public function remove($key)
+ {
+ unset($this->data[$key]);
+ }
+
+ /**
+ * @param string $key
+ * @return mixed
+ */
+ public function __get($key)
+ {
+ return $this->get($key);
+ }
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ */
+ public function __set($key, $value)
+ {
+ $this->set($key, $value);
+ }
+
+ /**
+ * @param string $key
+ * @return bool
+ */
+ public function __isset($key)
+ {
+ return $this->has($key);
+ }
+
+ /**
+ * @param string $key
+ */
+ public function __unset($key)
+ {
+ $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/Database/Db.php b/src/Database/Db.php
new file mode 100644
index 00000000..c1efa058
--- /dev/null
+++ b/src/Database/Db.php
@@ -0,0 +1,170 @@
+<?php
+
+namespace Engelsystem\Database;
+
+use PDO;
+use PDOException;
+use PDOStatement;
+
+class Db
+{
+ /** @var PDO */
+ protected static $db;
+
+ /** @var PDOStatement */
+ protected static $stm = null;
+
+ /** @var bool */
+ protected static $lastStatus = true;
+
+ /**
+ * Connect to database
+ *
+ * @param string $dsn
+ * @param string $username
+ * @param string $password
+ * @param array $options
+ * @return bool
+ */
+ public static function connect($dsn, $username = null, $password = null, $options = [])
+ {
+ try {
+ self::$db = new PDO($dsn, $username, $password, $options);
+ } catch (PDOException $e) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Run a prepared query
+ *
+ * @param string $query
+ * @param array $bindings
+ * @return PDOStatement
+ */
+ public static function query($query, array $bindings = [])
+ {
+ self::$stm = self::$db->prepare($query);
+ self::$lastStatus = self::$stm->execute($bindings);
+
+ return self::$stm;
+ }
+
+ /**
+ * Run a sql query
+ *
+ * @param string $query
+ * @return bool
+ */
+ public static function unprepared($query)
+ {
+ self::$stm = self::$db->query($query);
+ self::$lastStatus = (self::$stm instanceof PDOStatement);
+
+ return self::$lastStatus;
+ }
+
+ /**
+ * Run a select query
+ *
+ * @param string $query
+ * @param array $bindings
+ * @return array
+ */
+ public static function select($query, array $bindings = [])
+ {
+ self::query($query, $bindings);
+
+ return self::$stm->fetchAll(PDO::FETCH_ASSOC);
+ }
+
+ /**
+ * Run a insert query
+ *
+ * @param string $query
+ * @param array $bindings
+ * @return bool
+ */
+ public static function insert($query, array $bindings = [])
+ {
+ self::query($query, $bindings);
+
+ return self::$lastStatus;
+ }
+
+ /**
+ * Run a update query
+ *
+ * @param string $query
+ * @param array $bindings
+ * @return int|null
+ */
+ public static function update($query, array $bindings = [])
+ {
+ self::query($query, $bindings);
+
+ return (self::$lastStatus ? self::$stm->rowCount() : null);
+ }
+
+ /**
+ * Run a delete query
+ *
+ * @param string $query
+ * @param array $bindings
+ * @return int|null
+ */
+ public static function delete($query, array $bindings = [])
+ {
+ self::query($query, $bindings);
+
+ return (self::$lastStatus ? self::$stm->rowCount() : null);
+ }
+
+ /**
+ * Run a single statement
+ *
+ * @param string $query
+ * @param array $bindings
+ * @return bool
+ */
+ public static function statement($query, array $bindings = [])
+ {
+ self::query($query, $bindings);
+
+ return self::$lastStatus;
+ }
+
+ /**
+ * Returns the last error
+ *
+ * @return array
+ */
+ public static function getError()
+ {
+ if (!self::$stm instanceof PDOStatement) {
+ return [-1, null, null];
+ }
+
+ return self::$stm->errorInfo();
+ }
+
+ /**
+ * Get the PDO instance
+ *
+ * @return PDO
+ */
+ public static function getPdo()
+ {
+ return self::$db;
+ }
+
+ /**
+ * @return PDOStatement|false|null
+ */
+ public static function getStm()
+ {
+ return self::$stm;
+ }
+}
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
new file mode 100644
index 00000000..0532a7d8
--- /dev/null
+++ b/src/Exceptions/Handler.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Engelsystem\Exceptions;
+
+use Exception;
+
+class Handler
+{
+ /** @var string */
+ protected $environment;
+
+ const ENV_PRODUCTION = 'prod';
+ const ENV_DEVELOPMENT = 'dev';
+
+ /**
+ * Handler constructor.
+ *
+ * @param string $environment production|development
+ */
+ public function __construct($environment = self::ENV_PRODUCTION)
+ {
+ $this->environment = $environment;
+
+ set_error_handler([$this, 'errorHandler']);
+ set_exception_handler([$this, 'exceptionHandler']);
+ }
+
+ /**
+ * @param int $number
+ * @param string $string
+ * @param string $file
+ * @param int $line
+ * @param array $context
+ */
+ public function errorHandler($number, $string, $file, $line, $context)
+ {
+ $this->handle('error', $number, $string, $file, $line, $context);
+ }
+
+ /**
+ * @param Exception $e
+ */
+ public function exceptionHandler(Exception $e)
+ {
+ $this->handle(
+ 'exception',
+ $e->getCode(),
+ get_class($e) . ': ' . $e->getMessage(),
+ $e->getFile(),
+ $e->getLine(),
+ ['exception' => $e]
+ );
+ }
+
+ /**
+ * @param string $type
+ * @param int $number
+ * @param string $string
+ * @param string $file
+ * @param int $line
+ * @param array $context
+ */
+ protected function handle($type, $number, $string, $file, $line, $context = [])
+ {
+ error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s',
+ $type,
+ $number,
+ $string,
+ $file,
+ $line,
+ json_encode($context)
+ ));
+
+ if ($this->environment == self::ENV_DEVELOPMENT) {
+ echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;overflow-y:auto;">';
+ echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
+ var_export([
+ 'string' => $string,
+ 'file' => $file . ':' . $line,
+ 'context' => ($this->environment == self::ENV_DEVELOPMENT ? $context : null),
+ ]);
+ echo '</pre>';
+ die();
+ }
+
+ echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
+ die();
+ }
+
+ /**
+ * @param string $environment
+ */
+ public function setEnvironment($environment)
+ {
+ $this->environment = $environment;
+ }
+}
diff --git a/src/helpers.php b/src/helpers.php
new file mode 100644
index 00000000..aeb256e9
--- /dev/null
+++ b/src/helpers.php
@@ -0,0 +1,24 @@
+<?php
+// Some useful functions
+
+use Engelsystem\Config\Config;
+
+/**
+ * Get or set config values
+ *
+ * @param string|array $key
+ * @param mixed $default
+ * @return mixed|Config
+ */
+function config($key = null, $default = null)
+{
+ if (empty($key)) {
+ return Config::getInstance();
+ }
+
+ if (is_array($key)) {
+ Config::getInstance()->set($key);
+ }
+
+ return Config::getInstance()->get($key, $default);
+}