2021-05-10 00:23:30 +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 .
*/
2021-05-16 08:19:51 +00:00
namespace RectorPrefix20210516\Symfony\Component\DependencyInjection ;
2021-05-10 00:23:30 +00:00
2021-05-16 08:19:51 +00:00
use RectorPrefix20210516\Psr\Container\ContainerExceptionInterface ;
use RectorPrefix20210516\Psr\Container\NotFoundExceptionInterface ;
use RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\RuntimeException ;
use RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException ;
use RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException ;
use RectorPrefix20210516\Symfony\Contracts\Service\ServiceLocatorTrait ;
use RectorPrefix20210516\Symfony\Contracts\Service\ServiceProviderInterface ;
use RectorPrefix20210516\Symfony\Contracts\Service\ServiceSubscriberInterface ;
2021-05-10 00:23:30 +00:00
/**
* @ author Robin Chalas < robin . chalas @ gmail . com >
* @ author Nicolas Grekas < p @ tchwork . com >
*/
2021-05-16 08:19:51 +00:00
class ServiceLocator implements \RectorPrefix20210516\Symfony\Contracts\Service\ServiceProviderInterface
2021-05-10 00:23:30 +00:00
{
use ServiceLocatorTrait {
get as private doGet ;
}
private $externalId ;
private $container ;
/**
* { @ inheritdoc }
*
* @ return mixed
*/
public function get ( $id )
{
if ( ! $this -> externalId ) {
return $this -> doGet ( $id );
}
try {
return $this -> doGet ( $id );
2021-05-16 08:19:51 +00:00
} catch ( \RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\RuntimeException $e ) {
2021-05-10 00:23:30 +00:00
$what = \sprintf ( 'service "%s" required by "%s"' , $id , $this -> externalId );
$message = \preg_replace ( '/service "\\.service_locator\\.[^"]++"/' , $what , $e -> getMessage ());
if ( $e -> getMessage () === $message ) {
$message = \sprintf ( 'Cannot resolve %s: %s' , $what , $message );
}
$r = new \ReflectionProperty ( $e , 'message' );
$r -> setAccessible ( \true );
$r -> setValue ( $e , $message );
throw $e ;
}
}
public function __invoke ( string $id )
{
return isset ( $this -> factories [ $id ]) ? $this -> get ( $id ) : null ;
}
/**
* @ internal
*
* @ return static
*/
2021-05-16 08:19:51 +00:00
public function withContext ( string $externalId , \RectorPrefix20210516\Symfony\Component\DependencyInjection\Container $container )
2021-05-10 00:23:30 +00:00
{
$locator = clone $this ;
$locator -> externalId = $externalId ;
$locator -> container = $container ;
return $locator ;
}
2021-05-16 08:19:51 +00:00
private function createNotFoundException ( string $id ) : \RectorPrefix20210516\Psr\Container\NotFoundExceptionInterface
2021-05-10 00:23:30 +00:00
{
if ( $this -> loading ) {
$msg = \sprintf ( 'The service "%s" has a dependency on a non-existent service "%s". This locator %s' , \end ( $this -> loading ), $id , $this -> formatAlternatives ());
2021-05-16 08:19:51 +00:00
return new \RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException ( $id , \end ( $this -> loading ) ? : null , null , [], $msg );
2021-05-10 00:23:30 +00:00
}
$class = \debug_backtrace ( \DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS , 4 );
$class = isset ( $class [ 3 ][ 'object' ]) ? \get_class ( $class [ 3 ][ 'object' ]) : null ;
$externalId = $this -> externalId ? : $class ;
$msg = [];
$msg [] = \sprintf ( 'Service "%s" not found:' , $id );
if ( ! $this -> container ) {
$class = null ;
} elseif ( $this -> container -> has ( $id ) || isset ( $this -> container -> getRemovedIds ()[ $id ])) {
$msg [] = 'even though it exists in the app\'s container,' ;
} else {
try {
$this -> container -> get ( $id );
$class = null ;
2021-05-16 08:19:51 +00:00
} catch ( \RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException $e ) {
2021-05-10 00:23:30 +00:00
if ( $e -> getAlternatives ()) {
$msg [] = \sprintf ( 'did you mean %s? Anyway,' , $this -> formatAlternatives ( $e -> getAlternatives (), 'or' ));
} else {
$class = null ;
}
}
}
if ( $externalId ) {
$msg [] = \sprintf ( 'the container inside "%s" is a smaller service locator that %s' , $externalId , $this -> formatAlternatives ());
} else {
$msg [] = \sprintf ( 'the current service locator %s' , $this -> formatAlternatives ());
}
if ( ! $class ) {
// no-op
2021-05-16 08:19:51 +00:00
} elseif ( \is_subclass_of ( $class , \RectorPrefix20210516\Symfony\Contracts\Service\ServiceSubscriberInterface :: class )) {
2021-05-10 00:23:30 +00:00
$msg [] = \sprintf ( 'Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".' , \preg_replace ( '/([^\\\\]++\\\\)++/' , '' , $class ));
} else {
$msg [] = 'Try using dependency injection instead.' ;
}
2021-05-16 08:19:51 +00:00
return new \RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException ( $id , \end ( $this -> loading ) ? : null , null , [], \implode ( ' ' , $msg ));
2021-05-10 00:23:30 +00:00
}
2021-05-16 08:19:51 +00:00
private function createCircularReferenceException ( string $id , array $path ) : \RectorPrefix20210516\Psr\Container\ContainerExceptionInterface
2021-05-10 00:23:30 +00:00
{
2021-05-16 08:19:51 +00:00
return new \RectorPrefix20210516\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException ( $id , $path );
2021-05-10 00:23:30 +00:00
}
private function formatAlternatives ( array $alternatives = null , string $separator = 'and' ) : string
{
$format = '"%s"%s' ;
if ( null === $alternatives ) {
if ( ! ( $alternatives = \array_keys ( $this -> factories ))) {
return 'is empty...' ;
}
$format = \sprintf ( 'only knows about the %s service%s.' , $format , 1 < \count ( $alternatives ) ? 's' : '' );
}
$last = \array_pop ( $alternatives );
return \sprintf ( $format , $alternatives ? \implode ( '", "' , $alternatives ) : $last , $alternatives ? \sprintf ( ' %s "%s"' , $separator , $last ) : '' );
}
}