From 62146c4027e48cfbdb4f518de137de8430392e24 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 18 Feb 2018 22:29:36 +0000 Subject: Split client and manager --- src/Manager/Config/Loader.php | 150 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/Manager/Config/Loader.php (limited to 'src/Manager/Config/Loader.php') diff --git a/src/Manager/Config/Loader.php b/src/Manager/Config/Loader.php new file mode 100644 index 0000000..97df62e --- /dev/null +++ b/src/Manager/Config/Loader.php @@ -0,0 +1,150 @@ + + */ +class Loader +{ + /** + * A loaded config array + * + * @var mixed[] + */ + protected $config; + + /** + * Try and load a config + * + * @author Phil Burton + */ + 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 + * @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 + * @return string; + */ + public function getConfigFilename() + { + return 'dipper.php'; + } + + /** + * Make sure the config is up to scratch + * + * @author Phil Burton + */ + 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 + */ + 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 + * @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 + * @return mixed[] + */ + public function getConfig() + { + return $this->config; + } + + /** + * Return the required config keys + * + * @author Phil Burton + * @return string[] + */ + public function getRequired() + { + return [ + "SITES_ROOT", + "CONFIG_ROOT" + ]; + } +} -- cgit v1.2.3