vendor/symfony/dependency-injection/Alias.php line 99

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