summaryrefslogtreecommitdiff
path: root/src/Config/Loader.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Config/Loader.php')
-rw-r--r--src/Config/Loader.php150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/Config/Loader.php b/src/Config/Loader.php
new file mode 100644
index 0000000..97df62e
--- /dev/null
+++ b/src/Config/Loader.php
@@ -0,0 +1,150 @@
+<?php
+
+namespace App\Config;
+
+/**
+ * A class to load a config into an array
+ * Throws an exception if it fails to laod
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
+class Loader
+{
+ /**
+ * A loaded config array
+ *
+ * @var mixed[]
+ */
+ protected $config;
+
+ /**
+ * Try and load a config
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
+ public function __construct()
+ {
+ foreach ($this->getConfigPaths() as $path) {
+ if ($config = $this->loadConfig($path)) {
+ $this->validate();
+ $this->clean();
+ return;
+ }
+ }
+
+ // No config found
+ throw new \Exception('Could not find config file.');
+ }
+
+ /**
+ * Try and load a config file
+ * If we can't return false
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return mixed[]|false
+ */
+ public function loadConfig(string $path)
+ {
+ $filename = $path . '/' . $this->getConfigFilename();
+
+ if (!$this->config) {
+ if (!file_exists($filename)) {
+ return false;
+ }
+
+ if (!is_readable($filename)) {
+ return false;
+ }
+
+ if (!$config = include($filename)) {
+ return false;
+ }
+
+ $this->config = $config;
+ }
+
+ return $this->config;
+ }
+
+ /**
+ * Return the nsam of the config file to load
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string;
+ */
+ public function getConfigFilename()
+ {
+ return 'dipper.php';
+ }
+
+ /**
+ * Make sure the config is up to scratch
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
+ public function validate()
+ {
+ $config = $this->getConfig();
+
+ foreach ($this->getRequired() as $required) {
+ if (!isset($config[$required])) {
+ throw new \Exception("Could not find " . $required . " in config file.");
+ }
+ }
+ }
+
+ /**
+ * Clean up the config ready for use
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
+ public function clean()
+ {
+ $config = $this->getConfig();
+ foreach ($config as &$item) {
+ // Remove trailing slashes
+ $item = rtrim($item, "/");
+ }
+
+ $this->config = $config;
+ }
+
+ /**
+ * Return the array of config paths
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string[]
+ */
+ public function getConfigPaths()
+ {
+ return [
+ realpath(dirname(__FILE__) . "/../../"), // Root of dipper
+ realpath(dirname(__FILE__) . "/../../config") // Config directory in dipper
+ ];
+ }
+
+ /**
+ * Return the loaded config
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return mixed[]
+ */
+ public function getConfig()
+ {
+ return $this->config;
+ }
+
+ /**
+ * Return the required config keys
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string[]
+ */
+ public function getRequired()
+ {
+ return [
+ "SITES_ROOT",
+ "CONFIG_ROOT"
+ ];
+ }
+}