Thanks to visit codestin.com
Credit goes to github.com

Skip to content

The generated code for lazy non-shared services doesn't work #40642

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
vkurdin-aw opened this issue Mar 30, 2021 · 7 comments
Closed

The generated code for lazy non-shared services doesn't work #40642

vkurdin-aw opened this issue Mar 30, 2021 · 7 comments

Comments

@vkurdin-aw
Copy link

vkurdin-aw commented Mar 30, 2021

Symfony version(s) affected: 4.4.21

Description
When a service is declared as lazy and non-shared, generated code uses $this->factories['service_name'](false); inside proxy without initialization of $this->factories['service_name'] before. like #38327

How to reproduce
Considering the following directory structure:

├── cache
├── reproducer.php
└── src
    ├── MyClassParent.php
    └── MyClass.php

reproducer.php

<?php
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
include_once __DIR__. '/../vendor/autoload.php';
$container = new ContainerBuilder();
$container->setParameter('container.dumper.inline_class_loader', true);
$container
    ->register(\Acme\MyClass::class, \Acme\MyClass::class)
    ->setShared(false)
    ->setLazy(true)
    ->setPublic(true);
$container->compile();
$dumper = new PhpDumper($container);
$dumper->setProxyDumper(new ProxyDumper());
$dump = $dumper->dump([
    'as_files' => true,
    'file' => 'cache/appAppKernelDevDebugContainer.php',
]);
print_r($dump);

src/MyClassParent.php

<?php
namespace Acme;
class MyClassParent {}

src/MyClass.php

<?php
namespace Acme;
class MyClass extends MyClassParent {}

generated ContainerHNvJ5jM/getMyClassService.php is:

<?php

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

// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
// Returns the public 'Acme\MyClass' service.

if ($lazyLoad) {
    return $this->createProxy('MyClass_d0c7b87', function () {
        return \MyClass_d0c7b87::staticProxyConstructor(function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {
            $wrappedInstance = $this->factories['Acme\\MyClass'](false);

            $proxy->setProxyInitializer(null);

            return true;
        });
    });
}

include_once \dirname(__DIR__, 2).'/src/MyClassParent.php';
include_once \dirname(__DIR__, 2).'/src/MyClass.php';

$this->factories['Acme\\MyClass'] = function ($lazyLoad = true) {
    return new \Acme\MyClass();
};

return $this->factories['Acme\\MyClass']();

The key difference from #38327 example is to force compiler to insert include_once ... statements in the generated file, without inlined includes all works correctly.

Possible Solution
The same trick from "Possible Solution" of #38327 is working for me.

@nicolas-grekas
Copy link
Member

@vkurdin-aw would you like to work on a fix? Or maybe just on a failing test case first?

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 18, 2022

@vkurdin-aw would you like to submit a PR with a failing test case? Alternatively, putting your reproducer in a small app that I could clone locally would really help.

@nicolas-grekas
Copy link
Member

I just tried reproducing using the given instructions and I failed. I'm therefore closing. Please provide a git repo as a reproducer if you still experience this.

@vkurdin-aw
Copy link
Author

@nicolas-grekas Yep, it is still reproduced in 4.4.49 (Security fixes only). https://github.com/vkurdin-aw/symfony-reproducer-40642-4.4
Checked on 5.4.18 — all OK! https://github.com/vkurdin-aw/symfony-reproducer-40642-5.4

Run docker compose up in both repos to print generated file

@nicolas-grekas
Copy link
Member

Given 4.4 is in security fixes only and this doesn't qualify, this is a won't fix. Thanks for the feedback!

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