summaryrefslogtreecommitdiff
path: root/src/Config
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2017-01-21 23:07:20 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2017-01-21 23:07:20 +0100
commit8506d6d27e3b926521007064abcdcc2f69c6aa06 (patch)
tree4c0207871b3e9a831f8a619ff095ad71adb66f05 /src/Config
parent740026a9de6cba73c4e77aba78950d0a791b6b62 (diff)
Refactoring: Config cleanup / moved to class
Diffstat (limited to 'src/Config')
-rw-r--r--src/Config/Config.php128
1 files changed, 128 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;
+ }
+}