Skip to content

[Config] Introduce concept of deprecating nodes #21051

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

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 42 additions & 7 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
protected $ignoreExtraKeys = false;
protected $removeExtraKeys = true;
protected $normalizeKeys = true;
private $deprecations = array();

public function setNormalizeKeys($normalizeKeys)
{
Expand All @@ -52,17 +53,41 @@ public function setNormalizeKeys($normalizeKeys)
*/
protected function preNormalize($value)
{
if (!$this->normalizeKeys || !is_array($value)) {
if (!is_array($value)) {
return $value;
}

$normalized = array();
$normalized = $value;
if ($this->normalizeKeys) {
$normalized = array();

foreach ($value as $k => $v) {
if (false !== strpos($k, '-') && false === strpos($k, '_') && !array_key_exists($normalizedKey = str_replace('-', '_', $k), $value)) {
$normalized[$normalizedKey] = $v;
} else {
$normalized[$k] = $v;
}
}
}

foreach ($this->deprecations as $deprecation) {
list($names, $handler, $message) = $deprecation;

$given = array_intersect(array_keys($normalized), $names);
if (!$given) {
continue;
}

foreach ($value as $k => $v) {
if (false !== strpos($k, '-') && false === strpos($k, '_') && !array_key_exists($normalizedKey = str_replace('-', '_', $k), $value)) {
$normalized[$normalizedKey] = $v;
} else {
$normalized[$k] = $v;
if (null === $message) {
$message = sprintf(1 === count($given) ? 'The node "%s" is deprecated at path "%s".' : 'The nodes "%s" are deprecated at path "%s".', implode('", "', $given), $this->getPath());
}
@trigger_error($message, E_USER_DEPRECATED);

if (null !== $handler) {
$normalized = $handler($this->remapXml($normalized));
Copy link
Contributor Author

@ro0NL ro0NL Dec 25, 2016

Choose a reason for hiding this comment

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

Oh, and on a side note; it was a really weird experience noticing that xml remapping is applied after any normalization.

I totally expected this to be done before normalization. Like normalizing keys...

Maybe we shouldnt do it even here.. for consistency. But it's really convenient :)

foreach ($given as $name) {
unset($normalized[$name]);
}
}
}

Expand Down Expand Up @@ -216,6 +241,16 @@ public function addChild(NodeInterface $node)
$this->children[$name] = $node;
}

/**
* Set child node deprecations
*
* @param array $deprecations
*/
public function setDeprecations(array $deprecations)
{
$this->deprecations = $deprecations;
}

/**
* Finalizes the value of this node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition
protected $addDefaultChildren = false;
protected $nodeBuilder;
protected $normalizeKeys = true;
private $deprecations;

/**
* {@inheritdoc}
Expand All @@ -45,6 +46,7 @@ public function __construct($name, NodeParentInterface $parent = null)

$this->nullEquivalent = array();
$this->trueEquivalent = array();
$this->deprecations = array();
}

/**
Expand Down Expand Up @@ -389,6 +391,22 @@ public function append(NodeDefinition $node)
return $this;
}

/**
* Deprecate child definition(s)
*
* @param string[] $names
* @param callable|null $handler
* @param string|null $message
*
* @return $this
*/
public function deprecate(array $names, callable $handler = null, $message = null)
{
$this->deprecations[] = array(array_unique($names), $handler, $message);

return $this;
}

/**
* Returns a node builder to be used to add children and prototype.
*
Expand All @@ -413,6 +431,24 @@ protected function createNode()

$this->validateConcreteNode($node);

$deprecated = array();
foreach ($this->deprecations as $deprecation) {
$deprecated = array_merge($deprecated, $deprecation[0]);
}
$existing = array_intersect($deprecated, array_keys($this->children));
if ($existing) {
throw new InvalidDefinitionException(
sprintf('->deprecate() cannot reference existing child nodes ("%s") at path "%s"', implode('", "', $existing), $node->getPath())
);
}
$duplicates = array_diff_assoc($deprecated, array_unique($deprecated));
if ($duplicates) {
throw new InvalidDefinitionException(
sprintf('->deprecate() cannot reference child nodes ("%s") multiple times at path "%s"', implode('", "', $duplicates), $node->getPath())
);
}
$node->setDeprecations($this->deprecations);

$node->setAddIfNotSet($this->addDefaults);

foreach ($this->children as $child) {
Expand Down Expand Up @@ -520,6 +556,12 @@ protected function validatePrototypeNode(PrototypedArrayNode $node)
{
$path = $node->getPath();

if ($this->deprecations) {
throw new InvalidDefinitionException(
sprintf('->deprecate() is not applicable to prototype nodes at path "%s"', $path)
);
}

if ($this->addDefaults) {
throw new InvalidDefinitionException(
sprintf('->addDefaultsIfNotSet() is not applicable to prototype nodes at path "%s"', $path)
Expand Down