-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fall back to autoloader when class is not in container #88
Comments
using |
No. |
@samdark it seems already implemented: di/tests/Unit/ContainerTest.php Lines 64 to 69 in 7dd3ce1
|
Does that work for sub-dependencies i.e. class Test
{
public function __construct(TestDependency $testDependency)
{
}
}
class TestDependency
{
}
$container->get(Test::class); |
There is the incomplete test about it: di/tests/Unit/ContainerTest.php Lines 42 to 51 in 7dd3ce1
The test passes successfully in this form: public function testOptionalClassDependency(): void
{
$container = new Container();
$container->set(A::class, A::class);
$a = $container->get(A::class);
$this->assertInstanceOf(B::class, $a->b);
} Console output: $ ./vendor/bin/phpunit --filter=testOptionalClassDependency
PHPUnit 7.5.16 by Sebastian Bergmann and contributors.
Runtime: PHP 7.2.22-1+ubuntu16.04.1+deb.sury.org+1 with Xdebug 2.7.2
Configuration: /tmp-for-test/yii-dev/dev/di/phpunit.xml.dist
. 1 / 1 (100%)
Time: 79 ms, Memory: 4.00 MB
OK (1 test, 1 assertion) Therefore, it is not clear what exactly needs to be changed. |
It seems like we can just adjust the tests and close this task. Do you agree? |
Yes. I've re-checked it and it works. |
@samdark, it seems that things are not so simple. I see that design solutions are needed here. Now autoload is triggered by calling method Line 128 in 7dd3ce1
If you add a new test to the test suite, then it passes: public function testDependenciesWithoutDefinition(): void
{
$container = new Container();
$a = $container->get(A::class);
$this->assertInstanceOf(A::class, $a);
$this->assertInstanceOf(B::class, $a->b);
} Thus, autoload is triggered for undeclared classes and their dependencies, BUT for consistency, method Lines 182 to 191 in 7dd3ce1
Moreover, there is a test suite that relies on this behavior of di/tests/Unit/ServiceProviderTest.php Lines 30 to 41 in 7dd3ce1
So, it looks like the implementation of the container is now inconsistent. We should discuss this and make some decisions about how it should work. |
has() should behave same as get() now. Could be achieved by triggering autoloading for class_exists() in case there's nothing directly registered in container. |
But this approach can cause performance problems. What do you think about it? |
Why should it cause performance problems? |
Typically, method
Calling method |
@samdark I just created benchmarks for method Test N1Method
|
|
Yes. I used: composer install --optimize-autoloader
Ok, I also think that we can ignore this, because these are fractions of microseconds.
Ok, I'll check it too. |
@samdark, mark the item completed in the roadmap: https://github.com/yiisoft/docs/blob/master/003-roadmap.md#psr-11-container |
Done. |
Is the Factory issue https://github.com/yiisoft/factory/issues/3 also closed? |
No, because the future of package |
Currently there's a need to declare everything that is repetitive if we need to set a single class without an interface into container:
In such cases we can try to fall back to class autoloading.
The text was updated successfully, but these errors were encountered: