diff options
author | Fbenas <philbeansburton@gmail.com> | 2018-02-18 22:29:36 +0000 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2018-02-18 22:29:36 +0000 |
commit | 62146c4027e48cfbdb4f518de137de8430392e24 (patch) | |
tree | 791efd0bdccf4f23302428539a0fdc1047502646 /src/Client/Config/Loader.php | |
parent | 7d2128f364c0e81a15653db2508d3e09b262eca1 (diff) |
Split client and manager
Diffstat (limited to 'src/Client/Config/Loader.php')
-rw-r--r-- | src/Client/Config/Loader.php | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/Client/Config/Loader.php b/src/Client/Config/Loader.php new file mode 100644 index 0000000..97df62e --- /dev/null +++ b/src/Client/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" + ]; + } +} |