/** * Load a config directory * * @param string $dir * @return object */ public function load($dir) { if (!is_dir($dir)) { if (empty($this->options['optional'])) { trigger_error("Config directory '{$dir}' doesn't exist", E_USER_WARNING); } return null; } $config = (object) array(); foreach (scandir($dir) as $file) { if ($file[0] == '.') { continue; } if (is_dir("{$dir}/{$file}")) { $data = $this->load("{$dir}/{$file}"); } else { $loader = Config::getLoader("{$dir}/{$file}", $this->options); if (!$loader) { continue; } $data = $loader->load("{$dir}/{$file}"); } if ($data) { $key = pathinfo($file, PATHINFO_FILENAME); Config::merge($config->{$key}, $data); } } return $config; }
/** * Get the DB connection. * * @return DB */ public static function conn() { // Auto connect using Jasny's Config class if (!isset(self::$connection)) { if (!class_exists('Jasny\\Config') || !isset(\Jasny\Config::i()->db)) { throw new DB_Exception("Unable to create DB connection: not configured"); } new static(\Jasny\Config::i()->db); } return self::$connection; }
/** * Get the DB connection. * * @return DB */ public static function conn() { // Auto connect using Jasny's Config class if (!isset(self::$connection)) { if (!class_exists('Jasny\\Config') || !isset(\Jasny\Config::i()->db)) { throw new DB_Exception("Unable to create DB connection: not configured"); } $cfg = \Jasny\Config::i()->db; new static(isset($cfg->host) ? $cfg->host : 'localhost', $cfg->username, $cfg->password, isset($cfg->dbname) ? $cfg->dbname : null, isset($cfg->port) ? $cfg->port : null); } return self::$connection; }
/** * Load all files in a directory * * @param string $dir * @return object */ protected function loadDir($dir) { $exts = array_keys(Config::$loaders, get_called_class()); if (empty($exts)) { return null; } $glob = '*.' . (count($exts) == 1 ? reset($exts) : '{' . join(',', $exts) . '}'); $config = (object) array(); foreach (glob("{$dir}/{$glob}", GLOB_BRACE) as $file) { if (basename($file)[0] == '.') { continue; } $key = pathinfo($file, PATHINFO_FILENAME); $data = $this->load($file); if ($data) { Config::merge($config->{$key}, $data); } } return $config; }
/** * Test through config->load */ public function testConfigLoad() { $options = ['table' => self::TABLE_NAME, 'key' => 'dev']; $config = new Config(); $config->load(self::$dynamodb, $options); $expected = self::getTestData(); $this->assertEquals($expected->qux, $config->qux); $this->assertEquals($expected->foo, $config->foo); }
/** * Parse ini string * * @param string $input * @return object */ public function parse($input) { $data = parse_ini_string($input, true); return Config::objectify($data); }
/** * Parse yaml string * * @param string $input * @return object */ public function parse($input) { switch ($this->options['use']) { case 'yaml': $data = yaml_parse($input); break; case 'syck': $data = syck_load($input); break; case 'symfony': $data = Symfony_Yaml::parse($input); break; case 'spyc': $data = \Spyc::YAMLLoadString($input); break; default: return null; } return Config::objectify($data); }