Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class CheckDefinitionValidityPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->hasErrors()) {
continue;
}

// synthetic service is public
if ($definition->isSynthetic() && !$definition->isPublic()) {
throw new RuntimeException(\sprintf('A synthetic service ("%s") must be public.', $id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ class ResolveClassPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
if ($definition->isSynthetic() || null !== $definition->getClass()) {
if ($definition->isSynthetic()
|| $definition->hasErrors()
|| null !== $definition->getClass()
|| !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)
) {
continue;
}
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]*+)++$/', $id)) {
if ($definition instanceof ChildDefinition && !class_exists($id)) {
throw new InvalidArgumentException(\sprintf('Service definition "%s" has a parent but no class, and its name looks like an FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.', $id));
}
$definition->setClass($id);
if ($definition instanceof ChildDefinition && !class_exists($id)) {
throw new InvalidArgumentException(\sprintf('Service definition "%s" has a parent but no class, and its name looks like an FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.', $id));
}
$definition->setClass($id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ public function registerClasses(Definition $prototype, string $namespace, string
$this->interfaces[] = $class;
} else {
$this->setDefinition($class, $definition = $getPrototype());
$definition->setClass($class);
if (null !== $errorMessage) {
$definition->addError($errorMessage);

continue;
}
$definition->setClass($class);

$interfaces = [];
foreach (class_implements($class, false) as $interface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ public function testDynamicPrivateName()
$this->addToAssertionCount(1);
}

public function testProcessSkipsDefinitionsWithErrors()
{
$container = new ContainerBuilder();
$definition = $container->register('a')->setSynthetic(true)->setPublic(false);
$definition->addError('Some error message');

// This should not throw an exception because the definition has errors
$this->process($container);

$this->addToAssertionCount(1);
}

protected function process(ContainerBuilder $container)
{
$pass = new CheckDefinitionValidityPass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ public function testAmbiguousChildDefinition()

(new ResolveClassPass())->process($container);
}

public function testSkipsDefinitionsWithErrors()
{
$container = new ContainerBuilder();
$def = $container->register('App\SomeClass');
$def->addError('Some error message');

(new ResolveClassPass())->process($container);

// The class should not be set because the definition has errors
$this->assertNull($def->getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ public function testMissingParentClass()
);
}

public function testClassIsSetEvenWhenDefinitionHasErrors()
{
$container = new ContainerBuilder();
$container->setParameter('bad_classes_dir', 'BadClasses');
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), 'test');

$loader->registerClasses(
(new Definition())->setAutoconfigured(true),
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\',
'Prototype/%bad_classes_dir%/*'
);

$definition = $container->getDefinition(MissingParent::class);

// The class should be set even when the definition has errors
$this->assertSame(MissingParent::class, $definition->getClass());
$this->assertNotEmpty($definition->getErrors());
}

public function testRegisterClassesWithBadPrefix()
{
$this->expectException(InvalidArgumentException::class);
Expand Down