Skip to content
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

[DependencyInjection] ContainerInterface cause ClassNotFoundException #52596

Closed
connorhu opened this issue Nov 15, 2023 · 4 comments
Closed

[DependencyInjection] ContainerInterface cause ClassNotFoundException #52596

connorhu opened this issue Nov 15, 2023 · 4 comments

Comments

@connorhu
Copy link
Contributor

Symfony version(s) affected

6.0

Description

This code is generated by DI and cause ClassNotFoundException for me:

<?php

namespace ContainerSF7xQsW;

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;

/**
 * @internal This class has been auto-generated by the Symfony Dependency Injection Component.
 */
class getMailer_TransportFactory_NativeService extends App_KernelDevDebugContainer
{
    /**
     * Gets the private 'mailer.transport_factory.native' shared service.
     *
     * @return \Symfony\Component\Mailer\Transport\NativeTransportFactory
     */
    public static function do($container, $lazyLoad = true)
    {
        include_once \dirname(__DIR__, 4).'/vendor/symfony/mailer/Transport/TransportFactoryInterface.php';
        include_once \dirname(__DIR__, 4).'/vendor/symfony/mailer/Transport/AbstractTransportFactory.php';
        include_once \dirname(__DIR__, 4).'/vendor/symfony/mailer/Transport/NativeTransportFactory.php';

        $a = ($container->services['event_dispatcher'] ?? self::getEventDispatcherService($container));

        if (isset($container->privates['mailer.transport_factory.native'])) {
            return $container->privates['mailer.transport_factory.native'];
        }

        return $container->privates['mailer.transport_factory.native'] = new \Symfony\Component\Mailer\Transport\NativeTransportFactory($a, ($container->privates['.debug.http_client'] ?? self::get_Debug_HttpClientService($container)), ($container->services['monolog.logger.mailer'] ?? $container->get('monolog.logger.mailer', ContainerInterface::NULL_ON_INVALID_REFERENCE)));
    }
}

And the exception is:
Attempted to load class "ContainerInterface" from namespace "ContainerSF7xQsW". Did you forget a "use" statement for e.g. "Symfony\Component\DependencyInjection\ContainerInterface" or "Psr\Container\ContainerInterface"?

The PhpDumper does generates wrong code:

$code = sprintf('$container->get(%s, ContainerInterface::NULL_ON_INVALID_REFERENCE)', $this->doExport($id));

This patch cause the regression: 16fcdda

How to reproduce

To be honest, I don't know how to reproduce this. I also don't fully understand why it only happened to me and not to others.

Possible Solution

Either of the two example codes will generate working code.

$code = sprintf('$container->get(%s, \\%s::NULL_ON_INVALID_REFERENCE)', $this->doExport($id), ContainerInterface::class);

or

$code = sprintf('$container->get(%s, \Symfony\Component\DependencyInjection\ContainerInterface::NULL_ON_INVALID_REFERENCE)', $this->doExport($id));

Additional Context

No response

@stof
Copy link
Member

stof commented Nov 15, 2023

Looks like the PHP dumper has been updated to reference the constant but without adding the corresponding use statement in the generated code.

@nicolas-grekas
Copy link
Member

Can you please confirm that #52597 fixes the issue?

@connorhu
Copy link
Contributor Author

@nicolas-grekas Yes it does fix the issue.

nicolas-grekas added a commit that referenced this issue Nov 15, 2023
…erenced services (nicolas-grekas)

This PR was merged into the 7.0 branch.

Discussion
----------

[DependencyInjection] Fix dumping containers with null-referenced services

| Q             | A
| ------------- | ---
| Branch?       | 7.0
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52596
| License       | MIT

Commits
-------

89f71ab [DependencyInjection] Fix dumping containers with null-referenced services
@connorhu
Copy link
Contributor Author

@nicolas-grekas I guess you missed that this bug exists since 6.0, so it's not enough to fix it only in the 7.0 branch.

nicolas-grekas added a commit that referenced this issue Nov 29, 2023
…erenced services (nicolas-grekas)

This PR was merged into the 6.3 branch.

Discussion
----------

[DependencyInjection] Fix dumping containers with null-referenced services

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52596
| License       | MIT

Backporting #52597 to 6.3 /cc `@connorhu`

Commits
-------

45a5994 [DependencyInjection] Fix dumping containers with null-referenced services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants