Alias.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  12. class Alias
  13. {
  14. private $id;
  15. private $public;
  16. private $deprecation = [];
  17. private static $defaultDeprecationTemplate = 'The "%alias_id%" service alias is deprecated. You should stop using it, as it will be removed in the future.';
  18. public function __construct(string $id, bool $public = false)
  19. {
  20. $this->id = $id;
  21. $this->public = $public;
  22. }
  23. /**
  24. * Checks if this DI Alias should be public or not.
  25. *
  26. * @return bool
  27. */
  28. public function isPublic()
  29. {
  30. return $this->public;
  31. }
  32. /**
  33. * Sets if this Alias is public.
  34. *
  35. * @return $this
  36. */
  37. public function setPublic(bool $boolean)
  38. {
  39. $this->public = $boolean;
  40. return $this;
  41. }
  42. /**
  43. * Sets if this Alias is private.
  44. *
  45. * @return $this
  46. *
  47. * @deprecated since Symfony 5.2, use setPublic() instead
  48. */
  49. public function setPrivate(bool $boolean)
  50. {
  51. trigger_deprecation('symfony/dependency-injection', '5.2', 'The "%s()" method is deprecated, use "setPublic()" instead.', __METHOD__);
  52. return $this->setPublic(!$boolean);
  53. }
  54. /**
  55. * Whether this alias is private.
  56. *
  57. * @return bool
  58. */
  59. public function isPrivate()
  60. {
  61. return !$this->public;
  62. }
  63. /**
  64. * Whether this alias is deprecated, that means it should not be referenced
  65. * anymore.
  66. *
  67. * @param string $package The name of the composer package that is triggering the deprecation
  68. * @param string $version The version of the package that introduced the deprecation
  69. * @param string $message The deprecation message to use
  70. *
  71. * @return $this
  72. *
  73. * @throws InvalidArgumentException when the message template is invalid
  74. */
  75. public function setDeprecated(/* string $package, string $version, string $message */)
  76. {
  77. $args = \func_get_args();
  78. if (\func_num_args() < 3) {
  79. trigger_deprecation('symfony/dependency-injection', '5.1', 'The signature of method "%s()" requires 3 arguments: "string $package, string $version, string $message", not defining them is deprecated.', __METHOD__);
  80. $status = $args[0] ?? true;
  81. if (!$status) {
  82. trigger_deprecation('symfony/dependency-injection', '5.1', 'Passing a null message to un-deprecate a node is deprecated.');
  83. }
  84. $message = (string) ($args[1] ?? null);
  85. $package = $version = '';
  86. } else {
  87. $status = true;
  88. $package = (string) $args[0];
  89. $version = (string) $args[1];
  90. $message = (string) $args[2];
  91. }
  92. if ('' !== $message) {
  93. if (preg_match('#[\r\n]|\*/#', $message)) {
  94. throw new InvalidArgumentException('Invalid characters found in deprecation template.');
  95. }
  96. if (false === strpos($message, '%alias_id%')) {
  97. throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
  98. }
  99. }
  100. $this->deprecation = $status ? ['package' => $package, 'version' => $version, 'message' => $message ?: self::$defaultDeprecationTemplate] : [];
  101. return $this;
  102. }
  103. public function isDeprecated(): bool
  104. {
  105. return (bool) $this->deprecation;
  106. }
  107. /**
  108. * @deprecated since Symfony 5.1, use "getDeprecation()" instead.
  109. */
  110. public function getDeprecationMessage(string $id): string
  111. {
  112. trigger_deprecation('symfony/dependency-injection', '5.1', 'The "%s()" method is deprecated, use "getDeprecation()" instead.', __METHOD__);
  113. return $this->getDeprecation($id)['message'];
  114. }
  115. /**
  116. * @param string $id Service id relying on this definition
  117. */
  118. public function getDeprecation(string $id): array
  119. {
  120. return [
  121. 'package' => $this->deprecation['package'],
  122. 'version' => $this->deprecation['version'],
  123. 'message' => str_replace('%alias_id%', $id, $this->deprecation['message']),
  124. ];
  125. }
  126. /**
  127. * Returns the Id of this alias.
  128. *
  129. * @return string The alias id
  130. */
  131. public function __toString()
  132. {
  133. return $this->id;
  134. }
  135. }