diff --git a/extras/ConfigSchema/StringHashAdapter.php b/extras/ConfigSchema/StringHashAdapter.php index ca0437e0..ccbe1ff3 100644 --- a/extras/ConfigSchema/StringHashAdapter.php +++ b/extras/ConfigSchema/StringHashAdapter.php @@ -33,32 +33,33 @@ class ConfigSchema_StringHashAdapter list($ns, $directive) = explode('.', $hash['ID'], 2); - if (isset($hash['DEFAULT'], $hash['DESCRIPTION'], $hash['TYPE'])) { + if (isset($hash['TYPE'], $hash['DEFAULT'], $hash['DESCRIPTION'])) { + $type = $hash['TYPE']; $raw_default = $hash['DEFAULT']; $default = eval("return $raw_default;"); $description = $hash['DESCRIPTION']; - $type = $hash['TYPE']; $schema->add($ns, $directive, $default, $type, $description); } - if (isset($hash['VALUE-ALIASES'])) { - $raw_value_aliases = $hash['VALUE-ALIASES']; - $value_aliases = eval("return array($raw_value_aliases);"); - $schema->addValueAliases($ns, $directive, $value_aliases); - } - if (isset($hash['ALLOWED'])) { $raw_allowed = $hash['ALLOWED']; $allowed = eval("return array($raw_allowed);"); $schema->addAllowedValues($ns, $directive, $allowed); } + // This must be after ALLOWED + if (isset($hash['VALUE-ALIASES'])) { + $raw_value_aliases = $hash['VALUE-ALIASES']; + $value_aliases = eval("return array($raw_value_aliases);"); + $schema->addValueAliases($ns, $directive, $value_aliases); + } + if (isset($hash['ALIASES'])) { $raw_aliases = $hash['ALIASES']; $aliases = preg_split('/\s*,\s*/', $raw_aliases); foreach ($aliases as $alias) { - list($new_ns, $new_directive) = explode('.', $alias, 2); - $schema->addAlias($ns, $directive, $new_ns, $new_directive); + list($alias_ns, $alias_directive) = explode('.', $alias, 2); + $schema->addAlias($alias_ns, $alias_directive, $ns, $directive); } } diff --git a/extras/ConfigSchema/StringHashReverseAdapter.php b/extras/ConfigSchema/StringHashReverseAdapter.php new file mode 100644 index 00000000..cb29688e --- /dev/null +++ b/extras/ConfigSchema/StringHashReverseAdapter.php @@ -0,0 +1,89 @@ +schema = $schema; + } + + /** + * Retrieves a string hash from a specific ID, could be a directive + * or a namespace. + * @param $ns string namespace + * @param $directive string directive name + */ + public function get($ns, $directive = null) { + $ret = array(); + if ($directive === null) { + if (!isset($this->schema->info_namespace[$ns])) { + trigger_error("Namespace '$ns' doesn't exist in schema"); + return; + } + $def = $this->schema->info_namespace[$ns]; + $ret['ID'] = $ns; + $ret['DESCRIPTION'] = $def->description; + return $ret; + } + if (!isset($this->schema->info[$ns][$directive])) { + trigger_error("Directive '$ns.$directive' doesn't exist in schema"); + return; + } + $def = $this->schema->info[$ns][$directive]; + $ret['ID'] = "$ns.$directive"; + $ret['TYPE'] = $def->type; + $ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]); + $ret['DESCRIPTION'] = $def->description; + if ($def->allowed !== null) { + $ret['ALLOWED'] = $this->exportLookup($def->allowed); + } + if (!empty($def->aliases)) { + $ret['VALUE-ALIASES'] = $this->exportHash($def->aliases); + } + if (!empty($def->directiveAliases)) { + $ret['ALIASES'] = implode(', ', $def->directiveAliases); + } + return $ret; + } + + /** + * Exports a variable into a PHP-readable format + */ + protected function export($var) { + return var_export($var, true); + } + + /** + * Exports a lookup array into the form 'key1', 'key2', ... + */ + protected function exportLookup($lookup) { + $keys = array_map(array($this, 'export'), array_keys($lookup)); + return implode(', ', $keys); + } + + /** + * Exports a hash into the form 'key' => 'val',\n ... + */ + protected function exportHash($hash) { + $code = $this->export($hash); + $lines = explode("\n", $code); + $ret = ''; + foreach ($lines as $line) { + if ($line == 'array (') continue; + if ($line == ')') continue; + $ret .= substr($line, 2) . "\n"; + } + return $ret; + } + +} diff --git a/tests/ConfigSchema/StringHashAdapterTest.php b/tests/ConfigSchema/StringHashAdapterTest.php index c1b41b9f..3a930897 100644 --- a/tests/ConfigSchema/StringHashAdapterTest.php +++ b/tests/ConfigSchema/StringHashAdapterTest.php @@ -82,8 +82,8 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase 'ALIASES' => "Ns.Dir2, Ns2.Dir", ), array( - array('addAlias', array('Ns', 'Dir', 'Ns', 'Dir2')), - array('addAlias', array('Ns', 'Dir', 'Ns2', 'Dir')), + array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')), + array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')), ) ); } @@ -92,25 +92,25 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase $this->assertAdapt( array( 'ID' => 'Ns.Dir', - 'DEFAULT' => "'default' . 'bar'", + 'DEFAULT' => "'val' . '1'", 'TYPE' => 'string', 'DESCRIPTION' => "Description of default.\n", 'VALUE-ALIASES' => " - 'milk' => 'dairy', - 'cheese' => 'dairy', + 'milk' => 'val1', + 'cheese' => 'val1', ", 'ALLOWED' => "'val1', 'val2'", 'ALIASES' => "Ns.Dir2, Ns2.Dir", ), array( array('add', array( - 'Ns', 'Dir', 'defaultbar', 'string', + 'Ns', 'Dir', 'val1', 'string', "Description of default.\n" )), - array('addValueAliases', array('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'))), array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))), - array('addAlias', array('Ns', 'Dir', 'Ns', 'Dir2')), - array('addAlias', array('Ns', 'Dir', 'Ns2', 'Dir')), + array('addValueAliases', array('Ns', 'Dir', array('milk' => 'val1', 'cheese' => 'val1'))), + array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')), + array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')), ) ); } diff --git a/tests/ConfigSchema/StringHashReverseAdapterTest.php b/tests/ConfigSchema/StringHashReverseAdapterTest.php new file mode 100644 index 00000000..38adf873 --- /dev/null +++ b/tests/ConfigSchema/StringHashReverseAdapterTest.php @@ -0,0 +1,60 @@ +addNamespace('Ns', 'Description of ns.'); + $schema->addNamespace('Ns2', 'Description of ns2.'); + $schema->add('Ns', 'Dir', 'dairy', 'string', + "Description of default.\n"); + $schema->addAllowedValues('Ns', 'Dir', array('dairy', 'meat')); + $schema->addValueAliases('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy')); + $schema->addAlias('Ns', 'Dir2', 'Ns', 'Dir'); + $schema->addAlias('Ns2', 'Dir', 'Ns', 'Dir'); + return $schema; + } + + function testNamespace() { + $adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema()); + $result = $adapter->get('Ns'); + $expect = array( + 'ID' => 'Ns', + 'DESCRIPTION' => "Description of ns.", + ); + $this->assertIdentical($result, $expect); + } + + function testBadNamespace() { + $adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema()); + $this->expectError("Namespace 'BadNs' doesn't exist in schema"); + $adapter->get('BadNs'); + } + + function testDirective() { + + $adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema()); + + $result = $adapter->get('Ns', 'Dir'); + $expect = array( + 'ID' => 'Ns.Dir', + 'TYPE' => 'string', + 'DEFAULT' => "'dairy'", + 'DESCRIPTION' => "Description of default.\n", + 'ALLOWED' => "'dairy', 'meat'", + 'VALUE-ALIASES' => "'milk' => 'dairy',\n'cheese' => 'dairy',\n", + 'ALIASES' => "Ns.Dir2, Ns2.Dir", + ); + + $this->assertIdentical($result, $expect); + + } + + function testBadDirective() { + $adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema()); + $this->expectError("Directive 'BadNs.BadDir' doesn't exist in schema"); + $adapter->get('BadNs', 'BadDir'); + } + +} diff --git a/tests/test_files.php b/tests/test_files.php index d4f115d5..f6c1b7a7 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -137,5 +137,6 @@ $test_files[] = 'FSTools/FileTest.php'; // ConfigSchema auxiliary library $test_files[] = 'ConfigSchema/StringHashAdapterTest.php'; +$test_files[] = 'ConfigSchema/StringHashReverseAdapterTest.php'; $test_files[] = 'ConfigSchema/StringHashParserTest.php'; $test_files[] = 'ConfigSchema/StringHashTest.php';