This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Alias.php

134 lines
3.3 KiB
PHP
Raw Normal View History

2011-01-07 14:44:29 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2011-01-07 14:44:29 +00:00
namespace Symfony\Component\DependencyInjection;
2017-10-27 14:58:53 +01:00
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2011-01-07 14:44:29 +00:00
class Alias
{
private $id;
private $public;
private $private;
2017-10-27 14:58:53 +01:00
private $deprecated;
private $deprecationTemplate;
private static $defaultDeprecationTemplate = 'The "%alias_id%" service alias is deprecated. You should stop using it, as it will be removed in the future.';
2011-01-07 14:44:29 +00:00
2017-10-28 19:15:32 +01:00
public function __construct(string $id, bool $public = true)
2011-01-07 14:44:29 +00:00
{
2017-10-28 19:15:32 +01:00
$this->id = $id;
2011-01-07 14:44:29 +00:00
$this->public = $public;
$this->private = 2 > \func_num_args();
2017-10-27 14:58:53 +01:00
$this->deprecated = false;
2011-01-07 14:44:29 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Checks if this DI Alias should be public or not.
*
2014-04-16 11:30:19 +01:00
* @return bool
2011-02-13 18:06:41 +00:00
*/
2011-01-07 14:44:29 +00:00
public function isPublic()
{
return $this->public;
}
2011-02-13 18:06:41 +00:00
/**
* Sets if this Alias is public.
*
* @return $this
2011-02-13 18:06:41 +00:00
*/
public function setPublic(bool $boolean)
2011-01-07 14:44:29 +00:00
{
$this->public = $boolean;
$this->private = false;
return $this;
}
/**
* Sets if this Alias is private.
*
* When set, the "private" state has a higher precedence than "public".
* In version 3.4, a "private" alias always remains publicly accessible,
* but triggers a deprecation notice when accessed from the container,
* so that the alias can be made really private in 4.0.
*
* @return $this
*/
public function setPrivate(bool $boolean)
{
$this->private = $boolean;
return $this;
}
/**
* Whether this alias is private.
*
* @return bool
*/
public function isPrivate()
{
return $this->private;
2011-01-07 14:44:29 +00:00
}
2017-10-27 14:58:53 +01:00
/**
* Whether this alias is deprecated, that means it should not be referenced
2017-10-27 14:58:53 +01:00
* anymore.
*
* @param bool $status Whether this alias is deprecated, defaults to true
2017-10-27 14:58:53 +01:00
* @param string $template Optional template message to use if the alias is deprecated
*
* @return $this
*
* @throws InvalidArgumentException when the message template is invalid
*/
public function setDeprecated(bool $status = true, string $template = null)
2017-10-27 14:58:53 +01:00
{
if (null !== $template) {
if (preg_match('#[\r\n]|\*/#', $template)) {
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
}
if (false === strpos($template, '%alias_id%')) {
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
2017-10-27 14:58:53 +01:00
}
$this->deprecationTemplate = $template;
}
$this->deprecated = $status;
2017-10-27 14:58:53 +01:00
return $this;
}
public function isDeprecated(): bool
2017-10-27 14:58:53 +01:00
{
return $this->deprecated;
}
public function getDeprecationMessage(string $id): string
2017-10-27 14:58:53 +01:00
{
return str_replace('%alias_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
2017-10-27 14:58:53 +01:00
}
2011-02-13 18:06:41 +00:00
/**
* Returns the Id of this alias.
*
* @return string The alias id
*/
2011-01-07 14:44:29 +00:00
public function __toString()
{
return $this->id;
}
}