diff --git a/src/Container/Container.php b/src/Container/Container.php index 293297c..076ad1a 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -58,10 +58,12 @@ public function __construct(bool $shouldBuildContainerMixin = true) $this->registerConfiguredProviders(); $this->bootstrap(); - } - if ($shouldBuildContainerMixin) { - $this->buildContainerMixinManifest(); + // The mixin describes the registered extensions, so it only needs checking when they are + // initialized: checking it on every call costs several filesystem stats per generated value. + if ($shouldBuildContainerMixin) { + $this->buildContainerMixinManifest(); + } } } diff --git a/tests/Unit/ContainerMixinManifestTest.php b/tests/Unit/ContainerMixinManifestTest.php index 6dce686..6d0dc63 100644 --- a/tests/Unit/ContainerMixinManifestTest.php +++ b/tests/Unit/ContainerMixinManifestTest.php @@ -154,4 +154,26 @@ public function testNoExtension() $this->assertFileExists('/tmp/ContainerMixin.php'); } + + public function testContainerMixinIsOnlyCheckedWhenExtensionsAreInitialized() + { + $container = new \Xefi\Faker\Container\Container(shouldBuildContainerMixin: false); + $container->forgetExtensions(); + $container->forgetBootstrappers(); + @unlink('/tmp/ContainerMixin.php'); + + new \Xefi\Faker\Container\Container(); + $this->assertFileExists('/tmp/ContainerMixin.php'); + + unlink('/tmp/ContainerMixin.php'); + new \Xefi\Faker\Container\Container(); + $this->assertFileDoesNotExist('/tmp/ContainerMixin.php'); + + $container->forgetExtensions(); + $container->forgetBootstrappers(); + new \Xefi\Faker\Container\Container(); + $this->assertFileExists('/tmp/ContainerMixin.php'); + + unlink('/tmp/ContainerMixin.php'); + } }