8000 [config] Add abbitily to deprecate a node by sanpii · Pull Request #22382 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[config] Add abbitily to deprecate a node #22382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.4.0
-----

* added `setDeprecated()` method to indicate a deprecated node

3.3.0
-----

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ protected function finalizeValue($value)
}

foreach ($this->children as $name => $child) {
if ($child->isDeprecated()) {
@trigger_error($child->getDeprecationMessage($name, $this->getPath()), E_USER_DEPRECATED);
}

if (!array_key_exists($name, $value)) {
if ($child->isRequired()) {
$msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath());
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class BaseNode implements NodeInterface
protected $finalValidationClosures = array();
protected $allowOverwrite = true;
protected $required = false;
protected $deprecationMessage = null;
protected $equivalentValues = array();
protected $attributes = array();

Expand Down Expand Up @@ -141,6 +142,19 @@ public function setRequired($boolean)
$this->required = (bool) $boolean;
}

/**
* Sets this node as deprecated.
*
* You can use %node% and %path% placeholders in your message to display,
* respectively, the node name and its complete path.
*
* @param string|null $message Deprecated message
*/
public function setDeprecated($message)
{
$this->deprecationMessage = $message;
}

/**
* Sets if this node can be overridden.
*
Expand Down Expand Up @@ -181,6 +195,29 @@ public function isRequired()
return $this->required;
}

/**
* Checks if this node is deprecated.
*
* @return bool
*/
public function isDeprecated()
{
return null !== $this->deprecationMessage;
}

/**
* Returns the deprecated message.
*
* @param string $node the configuration node name
* @param string $path the path of the node
*
* @return string
*/
public function getDeprecationMessage($node, $path)
{
return strtr($this->deprecationMessage, array('%node%' => $node, '%path%' => $path));
}

/**
* Returns the name of this node.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class NodeDefinition implements NodeParentInterface
protected $defaultValue;
protected $default = false;
protected $required = false;
protected $deprecationMessage = null;
protected $merge;
protected $allowEmptyValue = true;
protected $nullEquivalent;
Expand Down Expand Up @@ -168,6 +169,23 @@ public function isRequired()
return $this;
}

/**
* Sets the node as deprecated.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here for the special %node% and %path% vars.

* You can use %node% and %path% placeholders in your message to display,
* respectively, the node name and its complete path.
*
* @param string $message Deprecation message
*
* @return $this
*/
public function setDeprecated($message = 'The child node "%node%" at path "%path%" is deprecated.')
{
$this->deprecationMessage = $message;

return $this;
}

/**
* Sets the equivalent value used when the node contains null.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected function createNode()
$node->addEquivalentValue(true, $this->trueEquivalent);
$node->addEquivalentValue(false, $this->falseEquivalent);
$node->setRequired($this->required);
$node->setDeprecated($this->deprecationMessage);

if (null !== $this->validation) {
$node->setFinalValidationClosures($this->validation->rules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ private function writeNode(NodeInterface $node, $depth = 0, $root = false, $name
$comments[] = 'Required';
}

if ($child->isDeprecated()) {
$comments[] = sprintf('Deprecated (%s)', $child->getDeprecationMessage($child->getName(), $child->getPath()));
}

if ($child instanceof EnumNode) {
$comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = f
$comments[] = 'Required';
}

// deprecated?
if ($node->isDeprecated()) {
$comments[] = sprintf('Deprecated (%s)', $node->getDeprecationMessage($node->getName(), $node->getPath()));
}

// example
if ($example && !is_array($example)) {
$comments[] = 'Example: '.$example;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ private function getConfigurationAsString()
return str_replace("\n", PHP_EOL, <<<'EOL'
<!-- Namespace: http://example.org/schema/dic/acme_root -->
<!-- scalar-required: Required -->
<!-- scalar-deprecated: Deprecated (The child node "scalar_deprecated" at path "acme_root.scalar_deprecated" is deprecated.) -->
<!-- scalar-deprecated-with-message: Deprecated (Deprecation custom message for "scalar_deprecated_with_message" at "acme_root.scalar_deprecated_with_message") -->
<!-- enum-with-default: One of "this"; "that" -->
<!-- enum: One of "this"; "that" -->
<config
Expand All @@ -50,6 +52,8 @@ private function getConfigurationAsString()
scalar-array-empty=""
scalar-array-defaults="elem1,elem2"
scalar-required=""
scalar-deprecated=""
scalar-deprecated-with-message=""
node-with-a-looong-name=""
enum-with-default="this"
enum=""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ private function getConfigurationAsString()
- elem1
- elem2
scalar_required: ~ # Required
scalar_deprecated: ~ # Deprecated (The child node "scalar_deprecated" at path "acme_root.scalar_deprecated" is deprecated.)
scalar_deprecated_with_message: ~ # Deprecated (Deprecation custom message for "scalar_deprecated_with_message" at "acme_root.scalar_deprecated_with_message")
node_with_a_looong_name: ~
enum_with_default: this # One of & 10000 quot;this"; "that"
enum: ~ # One of "this"; "that"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function getConfigTreeBuilder()
->scalarNode('scalar_array_empty')->defaultValue(array())->end()
->scalarNode('scalar_array_defaults')->defaultValue(array('elem1', 'elem2'))->end()
->scalarNode('scalar_required')->isRequired()->end()
->scalarNode('scalar_deprecated')->setDeprecated()->end()
->scalarNode('scalar_deprecated_with_message')->setDeprecated('Deprecation custom message for "%node%" at "%path%"')->end()
->scalarNode('node_with_a_looong_name')->end()
->enumNode('enum_with_default')->values(array('this', 'that'))->defaultValue('this')->end()
->enumNode('enum')->values(array('this', 'that'))->end()
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Added `TranslationExtractorPass`
* Added `TranslatorPass`
* Added <notes> section to the Xliff 2.0 dumper.
* Improved Xliff 2.0 loader to load <notes> section.
* Added `TranslationWriterInterface`
* Deprecated `TranslationWriter::writeTranslations` in favor of `TranslationWriter::write`

Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $

$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');

foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
foreach ($xml->xpath('//xliff:unit') as $unit) {
$segment = $unit->segment;
$source = $segment->source;

// If the xlf file has another encoding specified, try to convert it because
Expand All @@ -144,6 +145,18 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $
}
}

if (isset($unit->notes)) {
$metadata['notes'] = array();
foreach ($unit->notes->note as $noteNode) {
$note = array();
foreach ($noteNode->attributes() as $key => $value) {
$note[$key] = (string) $value;
}
$note['content'] = (string) $noteNode;
$metadata['notes'][] = $note;
}
}

$catalogue->setMetadata((string) $source, $metadata, $domain);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ public function testFormatCatalogueWithNotesMetadata()
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
'baz' => 'biz',
));
$catalogue->setMetadata('foo', array('notes' => array(
array('category' => 'state', 'content' => 'new'),
array('category' => 'approved', 'content' => 'true'),
array('category' => 'section', 'content' => 'user login', 'priority' => '1'),
)));
$catalogue->setMetadata('baz', array('notes' => array(
array('id' => 'x', 'content' => 'x_content'),
array('appliesTo' => 'target', 'category' => 'quality', 'content' => 'Fuzzy'),
)));

$dumper = new XliffFileDumper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,44 @@ public function testLoadVersion2()
// target attributes
$this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));
}

public function testLoadVersion2WithNoteMeta()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
$this->assertSame(array(), libxml_get_errors());

// test for "foo" metadata
$this->assertTrue($catalogue->defines('foo', 'domain1'));
$metadata = $catalogue->getMetadata('foo', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(3, $metadata['notes']);

$this->assertEquals('state', $metadata['notes'][0]['category']);
$this->assertEquals('new', $metadata['notes'][0]['content']);

$this->assertEquals('approved', $metadata['notes'][1]['category']);
$this->assertEquals('true', $metadata['notes'][1]['content']);

$this->assertEquals('section', $metadata['notes'][2]['category']);
$this->assertEquals('1', $metadata['notes'][2]['priority']);
$this->assertEquals('user login', $metadata['notes'][2]['content']);

// test for "baz" metadata
$this->assertTrue($catalogue->defines('baz', 'domain1'));
$metadata = $catalogue->getMetadata('baz', 'domain1');
$this->assertNotEmpty($metadata);
$this->assertCount(2, $metadata['notes']);

$this->assertEquals('x', $metadata['notes'][0]['id']);
$this->assertEquals('x_content', $metadata['notes'][0]['content']);

$this->assertEquals('target', $metadata['notes'][1]['appliesTo']);
$this->assertEquals('quality', $metadata['notes'][1]['category']);
$this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@
<target>bar</target>
</segment>
</unit>
<unit id="uqWglk0">
<notes>
<note id="x">x_content</note>
<note appliesTo="target" category="quality">Fuzzy</note>
</notes>
<segment>
<source>baz</source>
<target>biz</target>
</segment>
</unit>
</file>
</xliff>
0