VariableNode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Definition;
  11. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  12. /**
  13. * This node represents a value of variable type in the config tree.
  14. *
  15. * This node is intended for values of arbitrary type.
  16. * Any PHP type is accepted as a value.
  17. *
  18. * @author Jeremy Mikola <jmikola@gmail.com>
  19. */
  20. class VariableNode extends BaseNode implements PrototypeNodeInterface
  21. {
  22. protected $defaultValueSet = false;
  23. protected $defaultValue;
  24. protected $allowEmptyValue = true;
  25. public function setDefaultValue($value)
  26. {
  27. $this->defaultValueSet = true;
  28. $this->defaultValue = $value;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function hasDefaultValue()
  34. {
  35. return $this->defaultValueSet;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getDefaultValue()
  41. {
  42. $v = $this->defaultValue;
  43. return $v instanceof \Closure ? $v() : $v;
  44. }
  45. /**
  46. * Sets if this node is allowed to have an empty value.
  47. *
  48. * @param bool $boolean True if this entity will accept empty values
  49. */
  50. public function setAllowEmptyValue($boolean)
  51. {
  52. $this->allowEmptyValue = (bool) $boolean;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function setName($name)
  58. {
  59. $this->name = $name;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function validateType($value)
  65. {
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function finalizeValue($value)
  71. {
  72. // deny environment variables only when using custom validators
  73. // this avoids ever passing an empty value to final validation closures
  74. if (!$this->allowEmptyValue && $this->isHandlingPlaceholder() && $this->finalValidationClosures) {
  75. @trigger_error(sprintf('Setting path "%s" to an environment variable is deprecated since Symfony 4.3. Remove "cannotBeEmpty()", "validate()" or include a prefix/suffix value instead.', $this->getPath()), \E_USER_DEPRECATED);
  76. // $e = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an environment variable when empty values are not allowed by definition and are validated.', $this->getPath()));
  77. // if ($hint = $this->getInfo()) {
  78. // $e->addHint($hint);
  79. // }
  80. // $e->setPath($this->getPath());
  81. //
  82. // throw $e;
  83. }
  84. if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
  85. $ex = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an empty value, but got %s.', $this->getPath(), json_encode($value)));
  86. if ($hint = $this->getInfo()) {
  87. $ex->addHint($hint);
  88. }
  89. $ex->setPath($this->getPath());
  90. throw $ex;
  91. }
  92. return $value;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. protected function normalizeValue($value)
  98. {
  99. return $value;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. protected function mergeValues($leftSide, $rightSide)
  105. {
  106. return $rightSide;
  107. }
  108. /**
  109. * Evaluates if the given value is to be treated as empty.
  110. *
  111. * By default, PHP's empty() function is used to test for emptiness. This
  112. * method may be overridden by subtypes to better match their understanding
  113. * of empty data.
  114. *
  115. * @param mixed $value
  116. *
  117. * @return bool
  118. *
  119. * @see finalizeValue()
  120. */
  121. protected function isValueEmpty($value)
  122. {
  123. return empty($value);
  124. }
  125. }