Php circular dependency detected while trying to resolve entry

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Circular definitions #816

Circular definitions #816

Comments

I am using Slim Framework 4 and adding multiple definition files, but getting Circular Errors when I try to decorate() an existing definition from another file.

/var/www/vendor/php-di/php-di/src/Container.php: line 384
if (isset($this->entriesBeingResolved[$entryName])) throw new DependencyException(«Circular dependency detected while trying to resolve entry ‘$entryName'»);
>

All the below files returns an array of definitions, and works fine when I remove definitions_b.php or definitions_c.php, meaning it works fine when there are not more than 2 files trying to define and/or alter the same definition.

$containerBuilder->addDefinitions(['definitions_a.php', 'definitions_b.php', 'definitions_c.php']); 
return [ 'settings' => function () < return ['acme' =>123]; > ]; 
use function DI\decorate; return [ 'settings' => decorate(function ($previous, ContainerInterface $c) < $previous['settings]['acme'] = 456; $result = Db::getOne('app'); $previous['settings']['color'] = $result->color; return $previous; > ];` 
use function DI\decorate; return [ 'settings' => decorate(function ($previous, ContainerInterface $c) < $previous['settings']['acme'] =789; return $previous; >]; 

I do understand the non-related problem of order-of-adding, but would please like your advice on the above.

Читайте также:  Index php joomla 404

The text was updated successfully, but these errors were encountered:

Hi,
Sorry about the errors, I was typing examples directly via the comment editor.
I believe how I’m doing it above is correct and the same as the official docs https://php-di.org/doc/definition-overriding.html#decorators — unless what you mean is I should not be altering $previous[‘settings’] directly, but add an intermediary, then assign and return $previous?

Like I explained above, 2 files with ‘settings’ works fine, but not more than 2 files — if my way of doing it is wrong then all combinations of below will produce an error, unless its an implicit restriction to decorate the same entry more than once, which from a practical strategy point makes sense?:

$containerBuilder->addDefinitions(['definitions_a.php', 'definitions_b.php']); //OR $containerBuilder->addDefinitions(['definitions_a.php', 'definitions_c.php']); 

But this produces an error:

$containerBuilder->addDefinitions(['definitions_a.php', 'definitions_b.php', 'definitions_c.php']); 

Well, if you change your code to

$containerBuilder->addDefinitions('definitions_a.php', 'definitions_b.php'); // no square brackets, no array of file names! 

then it does work with more than two files for me (using PHP-DI 6.3.5 during my tests after downgrading from 6.4.0, on PHP 8.0). Note that if you pass an array, this is seen as a single parameter, which has to contain definitions. The addDefinitions() allows for infinitely many parameters, but if they are arrays, they are treated as definitions. Strings are treated as files containing definitions.

Regarding your decorating code: Remember that you’ll $container->get(‘settings’) at some point, so anything the decorator is working on is the result of that undecorated operation. And the definition

is returning [‘acme’ => 123] when asked about settings, so your decorator has to alter that acme key, or maybe add more keys, but it has NOT to return the settings key from the definition. The decorator is getting [‘acme’ => 123] as the $previous value. Want to change it to [‘acme’ => 789] , then the decorator should just do that:

$previous['acme'] =789; return $previous; 

And if you want to ask the database for the value, you can do that as well. But consider avoiding unnecessary work. PHP-DI will probably decorate multiple times because if you add for example a caching layer and then another layer around an object, you’ll expect both layers. So in your case where only one scalar result is returned, it is useless to ask the database if the last decorator is overwriting the result anyways. I assume your original situation is different, but still I feel this should be mentioned.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Circular dependency detected #38

Circular dependency detected #38

Comments

Occurs error while resolving lazy injection:

Circular dependency detected while trying to resolve entry 'config' 

Seems, in this case PHP-DI tries resolve each entry of config, because it is ArrayDefinition .

use DI\ContainerBuilder; use Elie\PHPDI\Config\Config; use Psr\Container\ContainerInterface; include __DIR__ . '/vendor/autoload.php'; class Foo < public function __construct(string $key) < >> class Bar < public function __construct(Foo $foo) < >public function compute(): void < >> $config = new Config([ 'key' => 'null', 'dependencies' => [ 'factories' => [ Foo::class => function (ContainerInterface $c) < return new Foo($c->get('config')['key']); >, ] ], ]); $containerBuilder = new ContainerBuilder(); $config->configureContainer($containerBuilder); $containerBuilder->addDefinitions([ Bar::class => DI\create() ->constructor(DI\get(Foo::class)) ->lazy(), ]); $container = $containerBuilder->build(); $bar = $container->get(Bar::class); $bar->compute();
php-di/php-di:^6.0 elie29/zend-phpdi-config:^4.0 ocramius/proxy-manager:^2.1 

If delete config.dependencies from the definitions this solves the issue. But, I do not know whether this can lead to issues in other cases?

private function setDependencies(): void < $this->dependencies = $this->definitions[$this::CONFIG]['dependencies'] ?? []; + unset($this->definitions[$this::CONFIG]['dependencies']); >

The text was updated successfully, but these errors were encountered:

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Getting exception #530

Getting exception #530

Comments

I am getting this error. Please advise what does it mean and how to fix it?

Fatal error: Uncaught DI\DependencyException: Circular dependency detected while trying to resolve entry ‘router’ in /vendor/php-di/php-di/src/DI/Container.php:281 Stack trace: #0 /vendor/php-di/php-di/src/DI/Container.php(124): DI\Container->resolveDefinition(Object(DI\Definition\AliasDefinition)) #1 /vendor/php-di/php-di/src/DI/Definition/AliasDefinition.php(63): DI\Container->get(‘router’) #2 /vendor/php-di/php-di/src/DI/Definition/Resolver/SelfResolver.php(34): DI\Definition\AliasDefinition->resolve(Object(DI\Container)) #3 /vendor/php-di/php-di/src/DI/Definition/Resolver/ResolverDispatcher.php(58): DI\Definition\Resolver\SelfResolver->resolve(Object(DI\Definition\AliasDefinition), Array) #4 /vendor/php-di/php-di/src/DI/Container.php(287): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\ in /vendor/php-di/php-di/src/DI/Container.php on line 281

The text was updated successfully, but these errors were encountered:

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

Compile does not work #522

Compile does not work #522

Comments

class Foo < private $bar; public function __construct(Bar $bar) < $this->bar = $bar; > > class Bar < >$builder = new \DI\ContainerBuilder(); $builder->addDefinitions([ Bar::class => autowire(), Foo::class => autowire(), ]); $builder->enableCompilation(__DIR__ . '/CompiledContainer'); $container = $builder->build(); $foo = $container->get('Foo');

PHP Fatal error: Uncaught DI\DependencyException: Circular dependency detected while trying to resolve entry ‘Bar’ in E:\project\test\di\vendor\php-di\php-di\src\CompiledContainer.php:47
Fatal error: Uncaught DI\DependencyException: Circular dependency detected while trying to resolve entry ‘Bar’ in E:\project\test\di\vendor\php-di\php-di\src\CompiledContainer.php:47

The text was updated successfully, but these errors were encountered:

Источник

Оцените статью