*/ 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" ]; } }