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

No entry or class found (for two or more definition sources) #240

Closed
ghost opened this issue Mar 23, 2015 · 7 comments
Closed

No entry or class found (for two or more definition sources) #240

ghost opened this issue Mar 23, 2015 · 7 comments
Labels

Comments

@ghost
Copy link

ghost commented Mar 23, 2015

This is a very serious problem, which I do not know how to solve.

The following code can help to reproduce it:

use DI\Container;
use DI\ContainerBuilder;
use DI\Definition\Source\ArrayDefinitionSource;

class A {
    public function getData()
    {
        return 'data';
    }
}

class B {
    private $data;
    public function __construct($data)
    {
        $this->data = $data;
    }
}

class C {
    private $a;
    public function __construct(A $a)
    {
        $this->a = $a;
    }
}
class D {
    private $a;
    private $b;
    private $params;

    public function __construct(A $a, B $b, $params)
    {
        $this->a = $a;
        $this->b = $b;
        $this->params = $params;
    }
}

$definitions1 = [
// TODO: add some definitions ...
];
$definitions2 = [
    A::class => \DI\object(),
    B::class => \DI\factory(function (Container $c) {
        return new B($c->get(A::class)->getData());
    }),
    C::class => \DI\object()->constructor(\DI\link(A::class)),
    D::class => \DI\factory(function (Container $c) {
        return new D($c->get(A::class), $c->get(B::class), 'params');
    }),
];

$builder = new ContainerBuilder();
$builder->useAutowiring(false);
$builder->useAnnotations(false);
$builder->addDefinitions(new ArrayDefinitionSource($definitions1));
$builder->addDefinitions(new ArrayDefinitionSource($definitions2));
$container = $builder->build();
$c = $container->get(C::class);
$d = $container->get(D::class);

var_dump($c, $d);

Output:

PHP Fatal error:  Uncaught exception 'DI\NotFoundException' with message 'No entry or class found for 'Example\C'' in /home/kilte/projects/crazyarts/backend/vendor/mnapoli/php-di/src/DI/Container.php:120
Stack trace:
#0 /home/kilte/projects/crazyarts/backend/t.php(63): DI\Container->get('Example\\C')
#1 {main}
  thrown in /home/kilte/projects/crazyarts/backend/vendor/mnapoli/php-di/src/DI/Container.php on line 120

I think this problem may be caused by this line: https://github.com/mnapoli/PHP-DI/blob/master/src/DI/Definition/Source/ArrayDefinitionSource.php#L77

@mnapoli
Copy link
Member

mnapoli commented Mar 23, 2015

Thank you for the report, I have to admit I don't know why you are getting the exception. I will try to reproduce with your example.

@mnapoli
Copy link
Member

mnapoli commented Mar 23, 2015

OK reproduced and I think I've identified the source. I have tried a quick patch like this and it works:

@@ -57,9 +57,8 @@ class ArrayDefinitionSource implements ChainableDefinitionSource
         if ($definition === null) {
             // Not found, we use the chain or return null
             if ($this->chainedSource) {
-                return $this->chainedSource->getDefinition($name, $parentDefinition);
+                $definition = $this->chainedSource->getDefinition($name, $parentDefinition);
             }
-            return null;
         }

I will write correct tests and ensure there is no regression with this fix before releasing it (will do today).

Thanks a lot for reporting that!

@mnapoli mnapoli added the bug label Mar 23, 2015
@ghost
Copy link
Author

ghost commented Mar 24, 2015

Thank you for your quick reply.

@ghost ghost closed this as completed Mar 24, 2015
@mnapoli
Copy link
Member

mnapoli commented Mar 24, 2015

No worries. I'm keeping this issue open until it is fixed to keep track of it

@mnapoli mnapoli reopened this Mar 24, 2015
mnapoli added a commit that referenced this issue Mar 24, 2015
@mnapoli mnapoli closed this as completed Mar 24, 2015
@mnapoli
Copy link
Member

mnapoli commented Mar 24, 2015

That wasn't too long actually. I have released 4.4.7 please give it a try and let me know if all works fine for you!

mnapoli added a commit that referenced this issue Mar 24, 2015
@ghost
Copy link
Author

ghost commented Mar 24, 2015

Everything works fine for me 👍. Thanks again.

@mnapoli
Copy link
Member

mnapoli commented Nov 12, 2018

@konstantinosDMS please do not ressuscitate closed issues.

Also please do not post this as an issue, this is support and should go in the chat or StackOverflow (which you already did). You don't need to spam everywhere.

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

No branches or pull requests

1 participant