2010-01-04 14:26:20 +00:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2010-01-04 14:26:20 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-01-04 14:26:20 +00:00
*
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 .
2010-01-04 14:26:20 +00:00
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\DependencyInjection\Loader ;
2012-08-07 08:39:30 +01:00
use Symfony\Component\Config\FileLocatorInterface ;
2018-07-26 10:03:18 +01:00
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader ;
2018-07-26 10:06:28 +01:00
use Symfony\Component\Config\Resource\GlobResource ;
2017-02-03 19:41:28 +00:00
use Symfony\Component\DependencyInjection\ChildDefinition ;
2011-01-15 13:29:43 +00:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2017-01-14 11:46:46 +00:00
use Symfony\Component\DependencyInjection\Definition ;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ;
2011-01-15 13:29:43 +00:00
2010-01-04 14:26:20 +00:00
/**
* FileLoader is the abstract class used by all built - in loaders that are file based .
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2010-01-04 14:26:20 +00:00
*/
2011-02-10 15:15:51 +00:00
abstract class FileLoader extends BaseFileLoader
2010-01-04 14:26:20 +00:00
{
2011-02-10 15:15:51 +00:00
protected $container ;
2017-02-03 19:41:28 +00:00
protected $isLoadingInstanceof = false ;
2019-01-16 09:39:14 +00:00
protected $instanceof = [];
2010-01-04 14:26:20 +00:00
2012-08-07 08:39:30 +01:00
public function __construct ( ContainerBuilder $container , FileLocatorInterface $locator )
2010-01-04 14:26:20 +00:00
{
2011-02-10 15:15:51 +00:00
$this -> container = $container ;
2010-07-16 08:12:58 +01:00
2011-02-10 15:15:51 +00:00
parent :: __construct ( $locator );
2010-07-18 10:26:47 +01:00
}
2017-01-14 11:46:46 +00:00
/**
* Registers a set of classes as services using PSR - 4 for discovery .
*
* @ param Definition $prototype A definition to use as template
* @ param string $namespace The namespace prefix of classes in the scanned directory
* @ param string $resource The directory to look for classes , glob - patterns allowed
2017-05-09 15:16:01 +01:00
* @ param string $exclude A globed path of files to exclude
2017-01-14 11:46:46 +00:00
*/
2017-05-09 15:16:01 +01:00
public function registerClasses ( Definition $prototype , $namespace , $resource , $exclude = null )
2017-01-14 11:46:46 +00:00
{
if ( '\\' !== substr ( $namespace , - 1 )) {
throw new InvalidArgumentException ( sprintf ( 'Namespace prefix must end with a "\\": %s.' , $namespace ));
}
if ( ! preg_match ( '/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/' , $namespace )) {
throw new InvalidArgumentException ( sprintf ( 'Namespace is not a valid PSR-4 prefix: %s.' , $namespace ));
}
2017-05-09 15:16:01 +01:00
$classes = $this -> findClasses ( $namespace , $resource , $exclude );
2017-01-14 11:46:46 +00:00
// prepare for deep cloning
2017-12-03 09:59:27 +00:00
$serializedPrototype = serialize ( $prototype );
2019-01-16 09:39:14 +00:00
$interfaces = [];
$singlyImplemented = [];
2017-01-14 11:46:46 +00:00
2018-01-25 19:21:40 +00:00
foreach ( $classes as $class => $errorMessage ) {
2017-12-03 09:59:27 +00:00
if ( interface_exists ( $class , false )) {
$interfaces [] = $class ;
} else {
2018-01-25 19:21:40 +00:00
$this -> setDefinition ( $class , $definition = unserialize ( $serializedPrototype ));
if ( null !== $errorMessage ) {
$definition -> addError ( $errorMessage );
continue ;
}
2017-12-03 09:59:27 +00:00
foreach ( class_implements ( $class , false ) as $interface ) {
$singlyImplemented [ $interface ] = isset ( $singlyImplemented [ $interface ]) ? false : $class ;
}
}
}
foreach ( $interfaces as $interface ) {
if ( ! empty ( $singlyImplemented [ $interface ])) {
$this -> container -> setAlias ( $interface , $singlyImplemented [ $interface ])
-> setPublic ( false );
}
2017-02-03 19:41:28 +00:00
}
}
/**
2017-04-05 08:26:56 +01:00
* Registers a definition in the container with its instanceof - conditionals .
*
* @ param string $id
* @ param Definition $definition
2017-02-03 19:41:28 +00:00
*/
protected function setDefinition ( $id , Definition $definition )
{
if ( $this -> isLoadingInstanceof ) {
if ( ! $definition instanceof ChildDefinition ) {
2018-07-26 09:45:46 +01:00
throw new InvalidArgumentException ( sprintf ( 'Invalid type definition "%s": ChildDefinition expected, "%s" given.' , $id , \get_class ( $definition )));
2017-02-03 19:41:28 +00:00
}
$this -> instanceof [ $id ] = $definition ;
} else {
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition
Also, not allowing arguments or method calls for autoconfigure. This is a safety
mechanism, since we don't have merging logic. It will allow us to add this in the
future if we want to.
The reason is that parent-child definitions are a different mechanism for "inheritance"
than instanceofConditionas and defaults... creating some edge cases when trying to
figure out which settings "win". For example:
Suppose a child and parent definitions are defined in different YAML files. The
child receives public: false from its _defaults, and the parent receives public: true
from its _defaults. Should the final child definition be public: true (so the parent
overrides the child, even though it only came from _defaults) or public: false (where
the child wins... even though it was only set from its _defaults). Or, if the parent
is explicitly set to public: true, should that override the public: false of the
child (which it got from its _defaults)? On one hand, the parent is being explicitly
set. On the other hand, the child is explicitly in a file settings _defaults public
to false. There's no correct answer.
There are also problems with instanceof. The importance goes:
defaults < instanceof < service definition
But how does parent-child relationships fit into that? If a child has public: false
from an _instanceof, but the parent explicitly sets public: true, which wins? Should
we assume the parent definition wins because it's explicitly set? Or would the
_instanceof win, because that's being explicitly applied to the child definition's
class by an _instanceof that lives in the same file as that class (whereas the parent
definition may live in a different file).
Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that
the complexity was growing too much. The solution is to not allow any of these
new feature to be used by ChildDefinition objects. In other words, when you want some
sort of "inheritance" for your service, you should *either* giving your service a
parent *or* using defaults and instanceof. And instead of silently not applying
defaults and instanceof to child definitions, I think it's better to scream that it's
not supported.
2017-04-27 16:48:07 +01:00
$this -> container -> setDefinition ( $id , $definition instanceof ChildDefinition ? $definition : $definition -> setInstanceofConditionals ( $this -> instanceof ));
2017-01-14 11:46:46 +00:00
}
}
2017-05-09 15:16:01 +01:00
private function findClasses ( $namespace , $pattern , $excludePattern )
2017-01-14 11:46:46 +00:00
{
2017-04-11 11:58:51 +01:00
$parameterBag = $this -> container -> getParameterBag ();
2017-05-09 15:16:01 +01:00
2019-01-16 09:39:14 +00:00
$excludePaths = [];
2017-05-09 15:16:01 +01:00
$excludePrefix = null ;
if ( $excludePattern ) {
$excludePattern = $parameterBag -> unescapeValue ( $parameterBag -> resolveValue ( $excludePattern ));
foreach ( $this -> glob ( $excludePattern , true , $resource ) as $path => $info ) {
if ( null === $excludePrefix ) {
$excludePrefix = $resource -> getPrefix ();
}
// normalize Windows slashes
$excludePaths [ str_replace ( '\\' , '/' , $path )] = true ;
}
}
2017-05-03 14:01:12 +01:00
$pattern = $parameterBag -> unescapeValue ( $parameterBag -> resolveValue ( $pattern ));
2019-01-16 09:39:14 +00:00
$classes = [];
2018-07-26 09:45:46 +01:00
$extRegexp = \defined ( 'HHVM_VERSION' ) ? '/\\.(?:php|hh)$/' : '/\\.php$/' ;
2017-02-15 13:40:32 +00:00
$prefixLen = null ;
2017-05-03 14:01:12 +01:00
foreach ( $this -> glob ( $pattern , true , $resource ) as $path => $info ) {
2017-02-15 13:40:32 +00:00
if ( null === $prefixLen ) {
2018-07-26 09:45:46 +01:00
$prefixLen = \strlen ( $resource -> getPrefix ());
2017-05-09 15:16:01 +01:00
2017-09-15 11:08:59 +01:00
if ( $excludePrefix && 0 !== strpos ( $excludePrefix , $resource -> getPrefix ())) {
2017-05-09 15:16:01 +01:00
throw new InvalidArgumentException ( sprintf ( 'Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s)' , $namespace , $excludePattern , $pattern ));
}
}
if ( isset ( $excludePaths [ str_replace ( '\\' , '/' , $path )])) {
continue ;
2017-02-15 13:40:32 +00:00
}
2017-01-14 11:46:46 +00:00
2017-01-17 13:46:23 +00:00
if ( ! preg_match ( $extRegexp , $path , $m ) || ! $info -> isReadable ()) {
2017-01-14 11:46:46 +00:00
continue ;
}
2018-07-26 09:45:46 +01:00
$class = $namespace . ltrim ( str_replace ( '/' , '\\' , substr ( $path , $prefixLen , - \strlen ( $m [ 0 ]))), '\\' );
2017-01-14 11:46:46 +00:00
if ( ! preg_match ( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/' , $class )) {
continue ;
}
2018-01-25 19:21:40 +00:00
try {
$r = $this -> container -> getReflectionClass ( $class );
} catch ( \ReflectionException $e ) {
$classes [ $class ] = sprintf (
'While discovering services from namespace "%s", an error was thrown when processing the class "%s": "%s".' ,
$namespace ,
$class ,
$e -> getMessage ()
);
continue ;
}
2017-04-26 04:23:29 +01:00
// check to make sure the expected class exists
2018-01-25 19:21:40 +00:00
if ( ! $r ) {
2017-05-03 14:01:12 +01:00
throw new InvalidArgumentException ( sprintf ( 'Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.' , $class , $path , $pattern ));
2017-01-14 11:46:46 +00:00
}
2017-05-09 16:03:31 +01:00
2017-12-03 09:59:27 +00:00
if ( $r -> isInstantiable () || $r -> isInterface ()) {
2018-01-25 19:21:40 +00:00
$classes [ $class ] = null ;
2017-01-14 11:46:46 +00:00
}
}
2017-05-03 14:01:12 +01:00
// track only for new & removed files
if ( $resource instanceof GlobResource ) {
$this -> container -> addResource ( $resource );
} else {
foreach ( $resource as $path ) {
$this -> container -> fileExists ( $path , false );
}
2017-01-14 11:46:46 +00:00
}
2017-02-15 13:40:32 +00:00
return $classes ;
2017-01-14 11:46:46 +00:00
}
2010-01-04 14:26:20 +00:00
}