8000 [DI] Improve class named services with root namespce by ro0NL · Pull Request #24145 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Improve class named services with root namespce #24145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -53,7 +53,7 @@ public function process(ContainerBuilder $container)
'The definition for "%s" has no class attribute, and appears to reference a '
.'class or interface in the global namespace. Leaving out the "class" attribute '
.'is only allowed for namespaced classes. Please specify the class attribute '
.'explicitly to get rid of this error.',
.'explicitly or change its service identifier to \\%1$s to get rid of this error.',
$id
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function process(ContainerBuilder $container)
if ($definition->isSynthetic() || null !== $definition->getClass()) {
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 (preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[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 a 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,18 +1069,6 @@ public function testNoClassFromNonClassId()
$container->compile();
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "\foo" has no class.
*/
public function testNoClassFromNsSeparatorId()
{
$container = new ContainerBuilder();

$definition = $container->register('\\foo');
$container->compile();
}

public function testServiceLocator()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -1129,6 +1117,26 @@ public function testPrivateServiceTriggersDeprecation()

$container->get('bar');
}

public function testNamespacedClassServiceWithRootNs()
{
$container = new ContainerBuilder();
$definition = $container->register($expected = '\\My\\DateTime');

$container->compile();

$this->assertSame($expected, $definition->getClass());
}

public function testClassServiceWithRootNs()
{
$container = new ContainerBuilder();
$definition = $container->register($expected = '\\DateTime');

$container->compile();

$this->assertSame($expected, $definition->getClass());
}
}

class FooClass
Expand Down
0