mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-24 02:58:43 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -15,6 +15,7 @@ use Symfony\Component\Config\Definition\Exception\Exception;
|
||||
use Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
|
||||
use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
|
||||
|
||||
/**
|
||||
* The base node class.
|
||||
@@ -23,56 +24,96 @@ use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
|
||||
*/
|
||||
abstract class BaseNode implements NodeInterface
|
||||
{
|
||||
public const DEFAULT_PATH_SEPARATOR = '.';
|
||||
|
||||
private static $placeholderUniquePrefixes = [];
|
||||
private static $placeholders = [];
|
||||
|
||||
protected $name;
|
||||
protected $parent;
|
||||
protected $normalizationClosures = [];
|
||||
protected $finalValidationClosures = [];
|
||||
protected $allowOverwrite = true;
|
||||
protected $required = false;
|
||||
protected $deprecationMessage = null;
|
||||
protected $deprecation = [];
|
||||
protected $equivalentValues = [];
|
||||
protected $attributes = [];
|
||||
protected $pathSeparator;
|
||||
|
||||
private $handlingPlaceholder;
|
||||
|
||||
/**
|
||||
* @param string|null $name The name of the node
|
||||
* @param NodeInterface|null $parent The parent of this node
|
||||
*
|
||||
* @throws \InvalidArgumentException if the name contains a period
|
||||
*/
|
||||
public function __construct($name, NodeInterface $parent = null)
|
||||
public function __construct(?string $name, NodeInterface $parent = null, string $pathSeparator = self::DEFAULT_PATH_SEPARATOR)
|
||||
{
|
||||
if (false !== strpos($name = (string) $name, '.')) {
|
||||
throw new \InvalidArgumentException('The name must not contain ".".');
|
||||
if (str_contains($name = (string) $name, $pathSeparator)) {
|
||||
throw new \InvalidArgumentException('The name must not contain ".'.$pathSeparator.'".');
|
||||
}
|
||||
|
||||
$this->name = $name;
|
||||
$this->parent = $parent;
|
||||
$this->pathSeparator = $pathSeparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* Register possible (dummy) values for a dynamic placeholder value.
|
||||
*
|
||||
* Matching configuration values will be processed with a provided value, one by one. After a provided value is
|
||||
* successfully processed the configuration value is returned as is, thus preserving the placeholder.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function setAttribute($key, $value)
|
||||
public static function setPlaceholder(string $placeholder, array $values): void
|
||||
{
|
||||
if (!$values) {
|
||||
throw new \InvalidArgumentException('At least one value must be provided.');
|
||||
}
|
||||
|
||||
self::$placeholders[$placeholder] = $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a common prefix for dynamic placeholder values.
|
||||
*
|
||||
* Matching configuration values will be skipped from being processed and are returned as is, thus preserving the
|
||||
* placeholder. An exact match provided by {@see setPlaceholder()} might take precedence.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function setPlaceholderUniquePrefix(string $prefix): void
|
||||
{
|
||||
self::$placeholderUniquePrefixes[] = $prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets all current placeholders available.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public static function resetPlaceholders(): void
|
||||
{
|
||||
self::$placeholderUniquePrefixes = [];
|
||||
self::$placeholders = [];
|
||||
}
|
||||
|
||||
public function setAttribute(string $key, $value)
|
||||
{
|
||||
$this->attributes[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAttribute($key, $default = null)
|
||||
public function getAttribute(string $key, $default = null)
|
||||
{
|
||||
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
|
||||
return $this->attributes[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAttribute($key)
|
||||
public function hasAttribute(string $key)
|
||||
{
|
||||
return isset($this->attributes[$key]);
|
||||
}
|
||||
@@ -90,20 +131,15 @@ abstract class BaseNode implements NodeInterface
|
||||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
*/
|
||||
public function removeAttribute($key)
|
||||
public function removeAttribute(string $key)
|
||||
{
|
||||
unset($this->attributes[$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an info message.
|
||||
*
|
||||
* @param string $info
|
||||
*/
|
||||
public function setInfo($info)
|
||||
public function setInfo(string $info)
|
||||
{
|
||||
$this->setAttribute('info', $info);
|
||||
}
|
||||
@@ -111,7 +147,7 @@ abstract class BaseNode implements NodeInterface
|
||||
/**
|
||||
* Returns info message.
|
||||
*
|
||||
* @return string|null The info text
|
||||
* @return string|null
|
||||
*/
|
||||
public function getInfo()
|
||||
{
|
||||
@@ -131,7 +167,7 @@ abstract class BaseNode implements NodeInterface
|
||||
/**
|
||||
* Retrieves the example configuration for this node.
|
||||
*
|
||||
* @return string|array|null The example
|
||||
* @return string|array|null
|
||||
*/
|
||||
public function getExample()
|
||||
{
|
||||
@@ -151,35 +187,58 @@ abstract class BaseNode implements NodeInterface
|
||||
|
||||
/**
|
||||
* Set this node as required.
|
||||
*
|
||||
* @param bool $boolean Required node
|
||||
*/
|
||||
public function setRequired($boolean)
|
||||
public function setRequired(bool $boolean)
|
||||
{
|
||||
$this->required = (bool) $boolean;
|
||||
$this->required = $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 $package The name of the composer package that is triggering the deprecation
|
||||
* @param string $version The version of the package that introduced the deprecation
|
||||
* @param string $message the deprecation message to use
|
||||
*
|
||||
* @param string|null $message Deprecated message
|
||||
* You can use %node% and %path% placeholders in your message to display,
|
||||
* respectively, the node name and its complete path
|
||||
*/
|
||||
public function setDeprecated($message)
|
||||
public function setDeprecated(?string $package/*, string $version, string $message = 'The child node "%node%" at path "%path%" is deprecated.' */)
|
||||
{
|
||||
$this->deprecationMessage = $message;
|
||||
$args = \func_get_args();
|
||||
|
||||
if (\func_num_args() < 2) {
|
||||
trigger_deprecation('symfony/config', '5.1', 'The signature of method "%s()" requires 3 arguments: "string $package, string $version, string $message", not defining them is deprecated.', __METHOD__);
|
||||
|
||||
if (!isset($args[0])) {
|
||||
trigger_deprecation('symfony/config', '5.1', 'Passing a null message to un-deprecate a node is deprecated.');
|
||||
|
||||
$this->deprecation = [];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$message = (string) $args[0];
|
||||
$package = $version = '';
|
||||
} else {
|
||||
$package = (string) $args[0];
|
||||
$version = (string) $args[1];
|
||||
$message = (string) ($args[2] ?? 'The child node "%node%" at path "%path%" is deprecated.');
|
||||
}
|
||||
|
||||
$this->deprecation = [
|
||||
'package' => $package,
|
||||
'version' => $version,
|
||||
'message' => $message,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if this node can be overridden.
|
||||
*
|
||||
* @param bool $allow
|
||||
*/
|
||||
public function setAllowOverwrite($allow)
|
||||
public function setAllowOverwrite(bool $allow)
|
||||
{
|
||||
$this->allowOverwrite = (bool) $allow;
|
||||
$this->allowOverwrite = $allow;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +276,7 @@ abstract class BaseNode implements NodeInterface
|
||||
*/
|
||||
public function isDeprecated()
|
||||
{
|
||||
return null !== $this->deprecationMessage;
|
||||
return (bool) $this->deprecation;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,10 +286,27 @@ abstract class BaseNode implements NodeInterface
|
||||
* @param string $path the path of the node
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @deprecated since Symfony 5.1, use "getDeprecation()" instead.
|
||||
*/
|
||||
public function getDeprecationMessage($node, $path)
|
||||
public function getDeprecationMessage(string $node, string $path)
|
||||
{
|
||||
return strtr($this->deprecationMessage, ['%node%' => $node, '%path%' => $path]);
|
||||
trigger_deprecation('symfony/config', '5.1', 'The "%s()" method is deprecated, use "getDeprecation()" instead.', __METHOD__);
|
||||
|
||||
return $this->getDeprecation($node, $path)['message'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $node The configuration node name
|
||||
* @param string $path The path of the node
|
||||
*/
|
||||
public function getDeprecation(string $node, string $path): array
|
||||
{
|
||||
return [
|
||||
'package' => $this->deprecation['package'] ?? '',
|
||||
'version' => $this->deprecation['version'] ?? '',
|
||||
'message' => strtr($this->deprecation['message'] ?? '', ['%node%' => $node, '%path%' => $path]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,13 +322,11 @@ abstract class BaseNode implements NodeInterface
|
||||
*/
|
||||
public function getPath()
|
||||
{
|
||||
$path = $this->name;
|
||||
|
||||
if (null !== $this->parent) {
|
||||
$path = $this->parent->getPath().'.'.$path;
|
||||
return $this->parent->getPath().$this->pathSeparator.$this->name;
|
||||
}
|
||||
|
||||
return $path;
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,8 +338,34 @@ abstract class BaseNode implements NodeInterface
|
||||
throw new ForbiddenOverwriteException(sprintf('Configuration path "%s" cannot be overwritten. You have to define all options for this path, and any of its sub-paths in one configuration section.', $this->getPath()));
|
||||
}
|
||||
|
||||
$this->validateType($leftSide);
|
||||
$this->validateType($rightSide);
|
||||
if ($leftSide !== $leftPlaceholders = self::resolvePlaceholderValue($leftSide)) {
|
||||
foreach ($leftPlaceholders as $leftPlaceholder) {
|
||||
$this->handlingPlaceholder = $leftSide;
|
||||
try {
|
||||
$this->merge($leftPlaceholder, $rightSide);
|
||||
} finally {
|
||||
$this->handlingPlaceholder = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $rightSide;
|
||||
}
|
||||
|
||||
if ($rightSide !== $rightPlaceholders = self::resolvePlaceholderValue($rightSide)) {
|
||||
foreach ($rightPlaceholders as $rightPlaceholder) {
|
||||
$this->handlingPlaceholder = $rightSide;
|
||||
try {
|
||||
$this->merge($leftSide, $rightPlaceholder);
|
||||
} finally {
|
||||
$this->handlingPlaceholder = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $rightSide;
|
||||
}
|
||||
|
||||
$this->doValidateType($leftSide);
|
||||
$this->doValidateType($rightSide);
|
||||
|
||||
return $this->mergeValues($leftSide, $rightSide);
|
||||
}
|
||||
@@ -282,6 +382,20 @@ abstract class BaseNode implements NodeInterface
|
||||
$value = $closure($value);
|
||||
}
|
||||
|
||||
// resolve placeholder value
|
||||
if ($value !== $placeholders = self::resolvePlaceholderValue($value)) {
|
||||
foreach ($placeholders as $placeholder) {
|
||||
$this->handlingPlaceholder = $value;
|
||||
try {
|
||||
$this->normalize($placeholder);
|
||||
} finally {
|
||||
$this->handlingPlaceholder = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
// replace value with their equivalent
|
||||
foreach ($this->equivalentValues as $data) {
|
||||
if ($data[0] === $value) {
|
||||
@@ -290,7 +404,7 @@ abstract class BaseNode implements NodeInterface
|
||||
}
|
||||
|
||||
// validate type
|
||||
$this->validateType($value);
|
||||
$this->doValidateType($value);
|
||||
|
||||
// normalize value
|
||||
return $this->normalizeValue($value);
|
||||
@@ -301,7 +415,7 @@ abstract class BaseNode implements NodeInterface
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return mixed The normalized array value
|
||||
* @return mixed
|
||||
*/
|
||||
protected function preNormalize($value)
|
||||
{
|
||||
@@ -323,7 +437,20 @@ abstract class BaseNode implements NodeInterface
|
||||
*/
|
||||
final public function finalize($value)
|
||||
{
|
||||
$this->validateType($value);
|
||||
if ($value !== $placeholders = self::resolvePlaceholderValue($value)) {
|
||||
foreach ($placeholders as $placeholder) {
|
||||
$this->handlingPlaceholder = $value;
|
||||
try {
|
||||
$this->finalize($placeholder);
|
||||
} finally {
|
||||
$this->handlingPlaceholder = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
$this->doValidateType($value);
|
||||
|
||||
$value = $this->finalizeValue($value);
|
||||
|
||||
@@ -333,6 +460,10 @@ abstract class BaseNode implements NodeInterface
|
||||
try {
|
||||
$value = $closure($value);
|
||||
} catch (Exception $e) {
|
||||
if ($e instanceof UnsetKeyException && null !== $this->handlingPlaceholder) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
} catch (\Exception $e) {
|
||||
throw new InvalidConfigurationException(sprintf('Invalid configuration for path "%s": ', $this->getPath()).$e->getMessage(), $e->getCode(), $e);
|
||||
@@ -356,7 +487,7 @@ abstract class BaseNode implements NodeInterface
|
||||
*
|
||||
* @param mixed $value The value to normalize
|
||||
*
|
||||
* @return mixed The normalized value
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function normalizeValue($value);
|
||||
|
||||
@@ -366,7 +497,7 @@ abstract class BaseNode implements NodeInterface
|
||||
* @param mixed $leftSide
|
||||
* @param mixed $rightSide
|
||||
*
|
||||
* @return mixed The merged value
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function mergeValues($leftSide, $rightSide);
|
||||
|
||||
@@ -375,7 +506,84 @@ abstract class BaseNode implements NodeInterface
|
||||
*
|
||||
* @param mixed $value The value to finalize
|
||||
*
|
||||
* @return mixed The finalized value
|
||||
* @return mixed
|
||||
*/
|
||||
abstract protected function finalizeValue($value);
|
||||
|
||||
/**
|
||||
* Tests if placeholder values are allowed for this node.
|
||||
*/
|
||||
protected function allowPlaceholders(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a placeholder is being handled currently.
|
||||
*/
|
||||
protected function isHandlingPlaceholder(): bool
|
||||
{
|
||||
return null !== $this->handlingPlaceholder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets allowed dynamic types for this node.
|
||||
*/
|
||||
protected function getValidPlaceholderTypes(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private static function resolvePlaceholderValue($value)
|
||||
{
|
||||
if (\is_string($value)) {
|
||||
if (isset(self::$placeholders[$value])) {
|
||||
return self::$placeholders[$value];
|
||||
}
|
||||
|
||||
foreach (self::$placeholderUniquePrefixes as $placeholderUniquePrefix) {
|
||||
if (str_starts_with($value, $placeholderUniquePrefix)) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function doValidateType($value): void
|
||||
{
|
||||
if (null !== $this->handlingPlaceholder && !$this->allowPlaceholders()) {
|
||||
$e = new InvalidTypeException(sprintf('A dynamic value is not compatible with a "%s" node type at path "%s".', static::class, $this->getPath()));
|
||||
$e->setPath($this->getPath());
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if (null === $this->handlingPlaceholder || null === $value) {
|
||||
$this->validateType($value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$knownTypes = array_keys(self::$placeholders[$this->handlingPlaceholder]);
|
||||
$validTypes = $this->getValidPlaceholderTypes();
|
||||
|
||||
if ($validTypes && array_diff($knownTypes, $validTypes)) {
|
||||
$e = new InvalidTypeException(sprintf(
|
||||
'Invalid type for path "%s". Expected %s, but got %s.',
|
||||
$this->getPath(),
|
||||
1 === \count($validTypes) ? '"'.reset($validTypes).'"' : 'one of "'.implode('", "', $validTypes).'"',
|
||||
1 === \count($knownTypes) ? '"'.reset($knownTypes).'"' : 'one of "'.implode('", "', $knownTypes).'"'
|
||||
));
|
||||
if ($hint = $this->getInfo()) {
|
||||
$e->addHint($hint);
|
||||
}
|
||||
$e->setPath($this->getPath());
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->validateType($value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user