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

Factories definitions using arrow functions ClosureAnalysisException #706

Closed
ManuelMolina97 opened this issue Feb 26, 2020 · 6 comments · Fixed by #716 or #717
Closed

Factories definitions using arrow functions ClosureAnalysisException #706

ManuelMolina97 opened this issue Feb 26, 2020 · 6 comments · Fixed by #716 or #717
Labels

Comments

@ManuelMolina97
Copy link

Hello,

We have been using PHP-DI for around a month in development environment, and everything has been good so far. We tried to save in cache our definitions:

$container->addDefinitions("{$rootDir}/config/{$_SERVER['PHP_ENV']}.php");
$container->useAutowiring(true);
$container->useAnnotations(false);

$container->addDefinitions([
    \Config\DatabaseConfig::class => fn(ContainerInterface $container) => new \Config\DatabaseConfig($container->get('databaseConfig')),
...
]);

    $container->enableCompilation(__DIR__ . '/cache');
    $container->writeProxiesToFile(true, __DIR__ . '/cache/proxies');

And when writeProxiesToFile was being executed, we got the following error:

2020/02/26 10:02:31 [error] 1100#1100: *3 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught SuperClosure\Exception\ClosureAnalysisException: The closure was not found within the abstract syntax tree. in /var/www/wallet/vendor/jeremeamia/superclosure/src/Analyzer/AstAnalyzer.php:72
Stack trace:
#0 /var/www/pracmanduca/vendor/jeremeamia/superclosure/src/Analyzer/AstAnalyzer.php(30): SuperClosure\Analyzer\AstAnalyzer->locateClosure()
#1 /var/www/pracmanduca/vendor/jeremeamia/superclosure/src/Analyzer/ClosureAnalyzer.php(29): SuperClosure\Analyzer\AstAnalyzer->determineCode()
#2 /var/www/pracmanduca/vendor/php-di/php-di/src/Compiler/Compiler.php(373): SuperClosure\Analyzer\ClosureAnalyzer->analyze()
#3 /var/www/pracmanduca/vendor/php-di/php-di/src/Compiler/Compiler.php(322): DI\Compiler\Compiler->compileClosure()
#4 /var/www/pracmanduca/vendor/php-di/php-di/src/Compiler/Compiler.php(276): DI\Compiler\Compiler->compileValue()
#5 /var/www/pracmanduca/vendor/php-di/php-di/src/Compiler/Compiler.php(155): DI\Compiler\Compiler->compileDefinition()
#6 /var/www/pracmanduca/vendor/php-di/php-di/

We have tried the same example but without arrow functions, and it's working. So arrow functions are causing the issue, but why? We know context works different using arrow functions, but we're not using $this or anything similar in any of our factories.

PHP Version: 7.4.3
PHP-DI version: 6.0

@SvenRtbg
Copy link
Contributor

I think arrow functions are not yet supported by SuperClosure. See jeremeamia/super_closure#112 for the relevant issue.

@mnapoli mnapoli added the bug label Feb 28, 2020
@mnapoli
Copy link
Member

mnapoli commented Feb 28, 2020

Oh yeah good catch!

I was using short functions, but did not compile the container so I never saw the exception before.

@b1rdex
Copy link
Contributor

b1rdex commented Mar 26, 2020

Is https://github.com/opis/closure missing this too?

@b1rdex
Copy link
Contributor

b1rdex commented Apr 2, 2020

Ok, SuperClosure just became abandoned (jeremeamia/super_closure@3d2c7f8).

@mnapoli opis/closure supports short functions (opis/closure@86fc51f) so PHP-DI should migrate.

@nixoncode
Copy link

nixoncode commented Apr 25, 2020

using php-di/slim-bridge V3.0.1 in production. got the same error too

Package jeremeamia/superclosure is abandoned, you should avoid using it. Use opis/closure instead.

@b1rdex
Copy link
Contributor

b1rdex commented May 22, 2020

Guys, please take a look at #716. I replaced closure jeremeamia/superclosure w/ opis/closure, but there are some restrictions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants