diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Config/Config.php | 128 | ||||
-rw-r--r-- | src/Exceptions/Handler.php | 8 | ||||
-rw-r--r-- | src/helpers.php | 24 |
3 files changed, 156 insertions, 4 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/Exceptions/Handler.php b/src/Exceptions/Handler.php index a81900b6..0532a7d8 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -11,7 +11,6 @@ class Handler const ENV_PRODUCTION = 'prod'; const ENV_DEVELOPMENT = 'dev'; - const ENV_DEBUGGING = 'debug'; /** * Handler constructor. @@ -48,7 +47,8 @@ class Handler $e->getCode(), get_class($e) . ': ' . $e->getMessage(), $e->getFile(), - $e->getLine() + $e->getLine(), + ['exception' => $e] ); } @@ -71,13 +71,13 @@ class Handler json_encode($context) )); - if ($this->environment == self::ENV_DEVELOPMENT || $this->environment == self::ENV_DEBUGGING) { + 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_DEBUGGING ? $context : null), + 'context' => ($this->environment == self::ENV_DEVELOPMENT ? $context : null), ]); echo '</pre>'; die(); 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); +} |