2011-01-07 14:44:29 +00:00
< ? php
2011-01-15 13:29:43 +00:00
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-01-15 13:29:43 +00:00
*
* 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
{
2011-03-11 13:50:46 +00:00
private $id ;
private $public ;
2017-09-17 10:49:41 +01:00
private $private ;
2017-10-27 14:58:53 +01:00
private $deprecated ;
private $deprecationTemplate ;
2019-01-26 21:26:02 +00:00
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 ;
2018-07-26 09:45:46 +01:00
$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 .
*
2017-09-05 12:45:10 +01:00
* @ return $this
2011-02-13 18:06:41 +00:00
*/
2019-06-28 17:25:25 +01:00
public function setPublic ( bool $boolean )
2011-01-07 14:44:29 +00:00
{
2019-06-28 17:25:25 +01:00
$this -> public = $boolean ;
2017-09-17 10:49:41 +01:00
$this -> private = false ;
2017-09-05 12:45:10 +01:00
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
*/
2019-06-28 17:25:25 +01:00
public function setPrivate ( bool $boolean )
2017-09-05 12:45:10 +01:00
{
2019-06-28 17:25:25 +01:00
$this -> private = $boolean ;
2017-09-05 12:45:10 +01:00
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
/**
2019-01-23 16:13:13 +00:00
* Whether this alias is deprecated , that means it should not be referenced
2017-10-27 14:58:53 +01:00
* anymore .
*
2019-01-23 16:13:13 +00:00
* @ 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
*/
2019-06-28 17:25:25 +01:00
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.' );
}
2019-01-26 21:26:02 +00:00
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 ;
}
2019-06-28 17:25:25 +01:00
$this -> deprecated = $status ;
2017-10-27 14:58:53 +01:00
return $this ;
}
2019-01-23 16:13:13 +00:00
public function isDeprecated () : bool
2017-10-27 14:58:53 +01:00
{
return $this -> deprecated ;
}
2019-01-23 16:13:13 +00:00
public function getDeprecationMessage ( string $id ) : string
2017-10-27 14:58:53 +01:00
{
2019-01-26 21:26:02 +00: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 ;
}
2011-03-11 13:50:46 +00:00
}