Skip to content

[Config] Create XML Reference Dumper #8635

Closed
wants to merge 6 commits into from

4 participants

@WouterJ
Symfony member
WouterJ commented Aug 1, 2013
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? no
Fixed tickets -
License MIT
Doc PR -

Only Yaml was supported. This PR adds support for XML. This makes it easier to test XML schema's (see symfony-cmf/menu-bundle#114 ), helps us at the docs with our configuration reference and helps others using XML with symfony.

Todo

  • Prototyped arrays don't work properly
  • Add comments (see Yaml dumper)
  • Add namespaces support

Side effects

I've moved the reference dumpers to their own namespace and renamed the original reference dumper to YamlReferenceDumper. The old one is kept for BC, but deprecated.

/cc @dantleech

@stloyd stloyd and 2 others commented on an outdated diff Aug 1, 2013
...onent/Config/Definition/Dumper/XmlReferenceDumper.php
+ }
+
+ if (false === $value) {
+ return 'false';
+ }
+
+ if (true === $value) {
+ return 'true';
+ }
+
+ if (null === $value || empty($value)) {
+ return '';
+ }
+
+ if (is_array($value)) {
+ if (0 === count($value)) {
@stloyd
stloyd added a note Aug 1, 2013

It's useless as empty($value) will return true already.

@staabm
staabm added a note Aug 4, 2013

Nit: will return empty string already :)

@WouterJ
Symfony member
WouterJ added a note Aug 4, 2013

fixedd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stloyd stloyd and 1 other commented on an outdated diff Aug 1, 2013
...onent/Config/Definition/Dumper/XmlReferenceDumper.php
+ if (!$child instanceof ArrayNode) {
+ $name = str_replace('_', '-', $child->getName());
+ $value = null;
+
+ if ($child->hasDefaultValue()) {
+ $value = $child->getDefaultValue();
+ }
+
+ $rootAttributes[$name] = $value;
+ } else {
+ $rootChildren[] = $child;
+ }
+ }
+ }
+
+ $rootIsEmptyTag = (0 === count($rootChildren));
@stloyd
stloyd added a note Aug 1, 2013

No need for the brackets.

@WouterJ
Symfony member
WouterJ added a note Aug 1, 2013

It makes it a lot more readable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@WouterJ WouterJ commented on an outdated diff Aug 1, 2013
phpunit.xml.dist
@@ -23,6 +23,12 @@
<directory>./src/Symfony/Component/*/Tests/</directory>
<directory>./src/Symfony/Bundle/*/Tests/</directory>
</testsuite>
+
+
+ <testsuite name="Config">
+ <directory>./src/Symfony/Component/Config/Tests/</directory>
+ <exclude>./src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php</exclude>
+ </testsuite>
@WouterJ
Symfony member
WouterJ added a note Aug 1, 2013

this will be removed when the pr is ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@staabm staabm commented on the diff Aug 4, 2013
...onent/Config/Definition/Dumper/XmlReferenceDumper.php
+ return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
+ }
+
+ public function dumpNode(NodeInterface $node)
+ {
+ $this->reference = '';
+ $this->writeNode($node, 0, true);
+ $ref = $this->reference;
+ $this->reference = null;
+
+ return $ref;
+ }
+
+ /**
+ * @param NodeInterface $node
+ * @param integer $depth
@staabm
staabm added a note Aug 4, 2013

Missing last param

@WouterJ
Symfony member
WouterJ added a note Aug 20, 2013

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Aug 4, 2013
...mfony/Component/Config/Definition/ReferenceDumper.php
/**
- * Dumps a reference configuration for the given configuration/node instance.
- *
- * Currently, only YML format is supported.
- *
- * @author Kevin Bond <kevinbond@gmail.com>
+ * @deprecated Deprecated since version 2.4, to be removed in 3.0. Use Symfony\Component\Config\Definition\DumperYamlReferenceDumper instead.
@stof
Symfony member
stof added a note Aug 4, 2013

missing \

@WouterJ
Symfony member
WouterJ added a note Aug 4, 2013

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof commented on an outdated diff Aug 4, 2013
...ig/Tests/Definition/Dumper/XmlReferenceDumperTest.php
+ public function testDumper()
+ {
+ $configuration = new ExampleConfiguration();
+
+ $dumper = new XmlReferenceDumper();
+ // $this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
+ echo("\n\n".$dumper->dump($configuration));
+ }
+
+ private function getConfigurationAsString()
+ {
+ return <<<EOL
+<config
+ boolean="true"
+ scalar-empty=""
+ scalar-null=""
@stof
Symfony member
stof added a note Aug 4, 2013

this is wrong. null should be dumped as "null", not as an empty string (otherwise it will be loaded as an empty string)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@WouterJ
Symfony member
WouterJ commented Aug 20, 2013

I fixed the comments and added support for comments in XML.

The only thing missing now is correct prototyped arrays, but I don't know the correct format for that. Can someone confirm my format is correct? Or can someone provide a correct format?

@WouterJ
Symfony member
WouterJ commented Sep 9, 2013

I changed the example to use a more common prototype example and fixed the dumpers.

@fabpot I think it's ready to merge now

@stof stof and 1 other commented on an outdated diff Sep 9, 2013
...onent/Config/Definition/Dumper/XmlReferenceDumper.php
+ if (1 === $attributesCount) {
+ $rootOpenTag .= sprintf(' %s="%s"', current(array_keys($rootAttributes)), $this->writeValue(current($rootAttributes)));
+ }
+
+ $rootOpenTag .= $rootIsEmptyTag ? ' />' : '>';
+
+ $this->writeLine($rootOpenTag, $depth);
+ } else {
+ $this->writeLine($rootOpenTag, $depth);
+
+ $i = 1;
+
+ foreach ($rootAttributes as $attrName => $attrValue) {
+ $attr = sprintf('%s="%s"', $attrName, $this->writeValue($attrValue));
+
+ if (0 === $attributesCount) {
@stof
Symfony member
stof added a note Sep 9, 2013

It is never 0 here as you are in the else part of if (1 >= ($attributesCount = count($rootAttributes))) {, which means 1 < $attributesCount

@WouterJ
Symfony member
WouterJ added a note Sep 9, 2013

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Sep 9, 2013
...onent/Config/Definition/Dumper/XmlReferenceDumper.php
+ // get elements
+ $rootChildren[] = $child;
+ }
+ }
+ }
+
+ // render comments
+
+ // special prototype comment
+ if (isset($prototype)) {
+ $this->writeLine('<!-- prototype', $depth);
+ }
+
+ // root node comment
+ if (isset($rootComment)) {
+ $this->writeLine('<!-- '.$rootComment.' -->', $depth);
@stof
Symfony member
stof added a note Sep 9, 2013

This is broken when you are in a prototype as it will close the comment opened for the prototype:

<!-- prototype
<!-- root comments -->
<more stuff="in the prototype comment" />
-->
@stof
Symfony member
stof added a note Sep 9, 2013

This will also break in case of nested array nodes btw.

Why putting the whole prototype in a comment ?

@WouterJ
Symfony member
WouterJ added a note Sep 9, 2013

I fixed it by adding 'prototype' to the root comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Sep 9, 2013
...ig/Tests/Definition/Dumper/XmlReferenceDumperTest.php
+ */
+
+namespace Symfony\Component\Config\Tests\Definition\Dumper;
+
+use Symfony\Component\Config\Definition\Dumper\XmlReferenceDumper;
+use Symfony\Component\Config\Tests\Fixtures\Configuration\ExampleConfiguration;
+
+class XmlReferenceDumperTest extends \PHPUnit_Framework_TestCase
+{
+ public function testDumper()
+ {
+ $configuration = new ExampleConfiguration();
+
+ $dumper = new XmlReferenceDumper();
+ $this->assertEquals($this->getConfigurationAsString(), $dumper->dump($configuration));
+// echo("\n\n".$dumper->dump($configuration));
@stof
Symfony member
stof added a note Sep 9, 2013

this should be removed

@WouterJ
Symfony member
WouterJ added a note Sep 9, 2013

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Sep 9, 2013
...ig/Tests/Definition/Dumper/XmlReferenceDumperTest.php
+ <!--
+ child3: this is a long
+ multi-line info text
+ which should be indented;
+ Example: example setting
+ -->
+ <array
+ child1=""
+ child2=""
+ child3=""
+ />
+
+ <array-prototype>
+
+ <!-- prototype
+ <parameter name="parameter name" />
@stof
Symfony member
stof added a note Sep 9, 2013

For scalar prototypes, it looks like <parameter name="parameter name">value</parameter>. Using a self closing tag works to configure an empty string but not for other values so it should be rendered with a closing tag IMO

@WouterJ
Symfony member
WouterJ added a note Sep 9, 2013

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Sep 9, 2013
...Tests/Fixtures/Configuration/ExampleConfiguration.php
@@ -48,14 +49,11 @@ public function getConfigTreeBuilder()
->end()
->end()
->arrayNode('array_prototype')
@stof
Symfony member
stof added a note Sep 9, 2013

I think it would be less confusing if this node was renamed. The prototyped array node is parameters, not array_prototype

@stof
Symfony member
stof added a note Sep 9, 2013

And you should also keep an example of a prototyped node with an array as prototype (as it happens regularly too) but giving it several children in this case (a single child value is a case where the prototype should be scalar)

@WouterJ
Symfony member
WouterJ added a note Sep 9, 2013

fixed name, I'll need to add another tests for array prototypes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@WouterJ
Symfony member
WouterJ commented Sep 9, 2013

Thank you @stof! I've fixed most of your comments. I've also added support for enum nodes in both dumpers.

@stof stof commented on an outdated diff Sep 9, 2013
...onent/Config/Definition/Dumper/XmlReferenceDumper.php
+ switch (get_class($prototype)) {
+ case 'Symfony\Component\Config\Definition\ScalarNode':
+ $prototypeValue = 'scalar value';
+ break;
+
+ case 'Symfony\Component\Config\Definition\FloatNode':
+ case 'Symfony\Component\Config\Definition\IntegerNode':
+ $prototypeValue = 'numeric value';
+ break;
+
+ case 'Symfony\Component\Config\Definition\BooleanNode':
+ $prototypeValue = 'true|false';
+ break;
+
+ case 'Symfony\Component\Config\Definition\EnumNode':
+ $prototypeValue = implode('|', $prototype->getValues());
@stof
Symfony member
stof added a note Sep 9, 2013

This is not be safe. It is possible to use an array as one of the possible values of the enum. EnumNode::finalizeValue uses implode(', ', array_map('json_encode', $this->values)) to display the permissible values in the exception message.

@stof
Symfony member
stof added a note Sep 9, 2013

note that writing XML config files while putting an array in it can get nasty (same than putting an array in a variableNode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof stof and 1 other commented on an outdated diff Sep 9, 2013
...g/Tests/Definition/Dumper/YamlReferenceDumperTest.php
- # Prototype
- name:
- value: ~ # Required
+ # Prototype
+ name: []
@stof
Symfony member
stof added a note Sep 9, 2013

this is wrong. The prototype is a scalar, not an array. The config will look like this:

parameters:
    name: value
    foobar: true
    name2: value2
@WouterJ
Symfony member
WouterJ added a note Sep 10, 2013

fixed (it was a bug in the yaml dumper)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@WouterJ
Symfony member
WouterJ commented Sep 10, 2013

I did some more stuff:

  • Added namespace support
  • Added (failing) test for array prototypes without a key
WouterJ added some commits Sep 9, 2013
@WouterJ WouterJ Created XmlReferenceDumper a2fd571
@WouterJ WouterJ Moved ReferenceDumper to Dumper\YamlReferenceDumper
This is done to make other formats, such as the XmlReferenceDumper
possible. This deprecates the old
Symfony\Component\Config\Definition\ReferenceDumper.
4aa360e
@WouterJ WouterJ Modified command to support XML dumper 7f5e7d4
@WouterJ WouterJ Added and modified tests 09ca40f
@WouterJ WouterJ Fixed Yaml Dumper
Fixed Yaml scalar prototypes
b20ebf9
@WouterJ WouterJ Marked test incomplete b0789c1
@WouterJ
Symfony member
WouterJ commented Sep 17, 2013

@fabpot I think I'm done now. The only problem is rendering the prototype values in Yaml. The Dumper needs an almost complete rewrite to fix that test and that's out of scope for this PR. I've marked the test for the YamlDumper as incomplete, so we can still safely merge this PR for 2.4.

@WouterJ
Symfony member
WouterJ commented Sep 17, 2013

btw, the test failure is just a random symfony failure which has nothing to do with this PR

@fabpot fabpot added a commit that referenced this pull request Sep 18, 2013
@fabpot fabpot merged branch WouterJ/dump_xml (PR #8635)
This PR was squashed before being merged into the master branch (closes #8635).

Discussion
----------

[Config] Create XML Reference Dumper

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | no
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Only Yaml was supported. This PR adds support for XML. This makes it easier to test XML schema's (see symfony-cmf/menu-bundle#114 ), helps us at the docs with our configuration reference and helps others using XML with symfony.

## Todo

 - [x] Prototyped arrays don't work properly
 - [x] Add comments (see Yaml dumper)
 - [x] Add namespaces support

## Side effects

I've moved the reference dumpers to their own namespace and renamed the original reference dumper to `YamlReferenceDumper`. The old one is kept for BC, but deprecated.

/cc @dantleech

Commits
-------

05e9ca7 [Config] Create XML Reference Dumper
7005cf5
@fabpot fabpot added a commit that closed this pull request Sep 18, 2013
@fabpot fabpot merged branch WouterJ/dump_xml (PR #8635)
This PR was squashed before being merged into the master branch (closes #8635).

Discussion
----------

[Config] Create XML Reference Dumper

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | no
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Only Yaml was supported. This PR adds support for XML. This makes it easier to test XML schema's (see symfony-cmf/menu-bundle#114 ), helps us at the docs with our configuration reference and helps others using XML with symfony.

## Todo

 - [x] Prototyped arrays don't work properly
 - [x] Add comments (see Yaml dumper)
 - [x] Add namespaces support

## Side effects

I've moved the reference dumpers to their own namespace and renamed the original reference dumper to `YamlReferenceDumper`. The old one is kept for BC, but deprecated.

/cc @dantleech

Commits
-------

05e9ca7 [Config] Create XML Reference Dumper
7005cf5
@fabpot fabpot closed this in 7005cf5 Sep 18, 2013
@fabpot fabpot added a commit that referenced this pull request Sep 18, 2013
@fabpot fabpot fixed previous merge (refs #8635) 4705e6f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.