diff --git a/src/Cascade.php b/src/Cascade.php index b91851f..8bbae7c 100644 --- a/src/Cascade.php +++ b/src/Cascade.php @@ -98,9 +98,9 @@ public static function getConfig() } /** - * Load configuration options from a file or a string + * Load configuration options from a file, a JSON or Yaml string or an array. * - * @param string $resource Path to config file or string or array + * @param string|array $resource Path to config file or configuration as string or array */ public static function fileConfig($resource) { @@ -108,4 +108,26 @@ public static function fileConfig($resource) self::$config->load(); self::$config->configure(); } + + /** + * Load configuration options from a JSON or Yaml string. Alias of fileConfig. + * @see fileConfig + * + * @param string $configString Configuration in string form + */ + public static function loadConfigFromString($configString) + { + self::fileConfig($configString); + } + + /** + * Load configuration options from an array. Alias of fileConfig. + * @see fileConfig + * + * @param array $configArray Configuration in array form + */ + public static function loadConfigFromArray($configArray) + { + self::fileConfig($configArray); + } } diff --git a/tests/CascadeTest.php b/tests/CascadeTest.php index 3ff5efc..e1c6089 100644 --- a/tests/CascadeTest.php +++ b/tests/CascadeTest.php @@ -56,9 +56,30 @@ public function testRegistryWithInvalidName() } public function testFileConfig() + { + $filePath = Fixtures::getPhpArrayConfigFile(); + Cascade::fileConfig($filePath); + $this->assertInstanceOf('Cascade\Config', Cascade::getConfig()); + } + + public function testLoadConfigFromArray() { $options = Fixtures::getPhpArrayConfig(); - Cascade::fileConfig($options); + Cascade::loadConfigFromArray($options); + $this->assertInstanceOf('Cascade\Config', Cascade::getConfig()); + } + + public function testLoadConfigFromStringWithJson() + { + $jsonConfig = Fixtures::getJsonConfig(); + Cascade::loadConfigFromString($jsonConfig); + $this->assertInstanceOf('Cascade\Config', Cascade::getConfig()); + } + + public function testLoadConfigFromStringWithYaml() + { + $yamlConfig = Fixtures::getYamlConfig(); + Cascade::loadConfigFromString($yamlConfig); $this->assertInstanceOf('Cascade\Config', Cascade::getConfig()); } } diff --git a/tests/Fixtures.php b/tests/Fixtures.php index e57fd28..e358e18 100644 --- a/tests/Fixtures.php +++ b/tests/Fixtures.php @@ -42,6 +42,16 @@ public static function getYamlConfigFile() return self::fixtureDir().'/fixture_config.yml'; } + /** + * Return a config as Yaml + * + * @return string Yaml config + */ + public static function getYamlConfig() + { + return file_get_contents(self::fixtureDir().'/fixture_config.yml'); + } + /** * Return the fixture sample Yaml file * @@ -76,6 +86,16 @@ public static function getJsonConfigFile() return self::fixtureDir().'/fixture_config.json'; } + /** + * Return a config as JSON + * + * @return string JSON config + */ + public static function getJsonConfig() + { + return file_get_contents(self::fixtureDir().'/fixture_config.json'); + } + /** * Return the fixture sample JSON file * @@ -111,6 +131,16 @@ public static function getSampleString() return " some string with new \n\n lines and white spaces \n\n"; } + /** + * Return the fixture PHP array config file + * + * @return string Path to PHP array config file + */ + public static function getPhpArrayConfigFile() + { + return self::fixtureDir().'/fixture_config.php'; + } + /** * Return a config array *