Alias.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 $private;
  17. private $deprecated;
  18. private $deprecationTemplate;
  19. private static $defaultDeprecationTemplate = 'The "%alias_id%" service alias is deprecated. You should stop using it, as it will be removed in the future.';
  20. public function __construct(string $id, bool $public = true)
  21. {
  22. $this->id = $id;
  23. $this->public = $public;
  24. $this->private = 2 > \func_num_args();
  25. $this->deprecated = false;
  26. }
  27. /**
  28. * Checks if this DI Alias should be public or not.
  29. *
  30. * @return bool
  31. */
  32. public function isPublic()
  33. {
  34. return $this->public;
  35. }
  36. /**
  37. * Sets if this Alias is public.
  38. *
  39. * @param bool $boolean If this Alias should be public
  40. *
  41. * @return $this
  42. */
  43. public function setPublic($boolean)
  44. {
  45. $this->public = (bool) $boolean;
  46. $this->private = false;
  47. return $this;
  48. }
  49. /**
  50. * Sets if this Alias is private.
  51. *
  52. * When set, the "private" state has a higher precedence than "public".
  53. * In version 3.4, a "private" alias always remains publicly accessible,
  54. * but triggers a deprecation notice when accessed from the container,
  55. * so that the alias can be made really private in 4.0.
  56. *
  57. * @param bool $boolean
  58. *
  59. * @return $this
  60. */
  61. public function setPrivate($boolean)
  62. {
  63. $this->private = (bool) $boolean;
  64. return $this;
  65. }
  66. /**
  67. * Whether this alias is private.
  68. *
  69. * @return bool
  70. */
  71. public function isPrivate()
  72. {
  73. return $this->private;
  74. }
  75. /**
  76. * Whether this alias is deprecated, that means it should not be referenced
  77. * anymore.
  78. *
  79. * @param bool $status Whether this alias is deprecated, defaults to true
  80. * @param string $template Optional template message to use if the alias is deprecated
  81. *
  82. * @return $this
  83. *
  84. * @throws InvalidArgumentException when the message template is invalid
  85. */
  86. public function setDeprecated($status = true, $template = null)
  87. {
  88. if (null !== $template) {
  89. if (preg_match('#[\r\n]|\*/#', $template)) {
  90. throw new InvalidArgumentException('Invalid characters found in deprecation template.');
  91. }
  92. if (false === strpos($template, '%alias_id%')) {
  93. throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
  94. }
  95. $this->deprecationTemplate = $template;
  96. }
  97. $this->deprecated = (bool) $status;
  98. return $this;
  99. }
  100. public function isDeprecated(): bool
  101. {
  102. return $this->deprecated;
  103. }
  104. public function getDeprecationMessage(string $id): string
  105. {
  106. return str_replace('%alias_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
  107. }
  108. /**
  109. * Returns the Id of this alias.
  110. *
  111. * @return string The alias id
  112. */
  113. public function __toString()
  114. {
  115. return $this->id;
  116. }
  117. }