From cc1cc9b20f75a4ef27a67f82b283f9d1785bd47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Tue, 12 Oct 2021 21:54:16 +0200 Subject: [PATCH] Inline service factories when in production mode This makes the OPCache more efficient by decreasing the amount of files to be managed by that layer. --- src/ContainerBuilder.php | 3 ++- test/CompilerTest.php | 32 ++++++++++++++++++++++++++------ test/ContainerBuilderTest.php | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/ContainerBuilder.php b/src/ContainerBuilder.php index 20627b96..d8fc7cb6 100644 --- a/src/ContainerBuilder.php +++ b/src/ContainerBuilder.php @@ -79,7 +79,7 @@ public static function delegating(string $configurationFile, string $namespace): private function setDefaultConfiguration(): void { $this->parameterBag->set('app.devmode', false); - $this->parameterBag->set('container.dumper.inline_factories', false); + $this->parameterBag->set('container.dumper.inline_factories', true); $this->parameterBag->set('container.dumper.inline_class_loader', true); $this->config->addPass($this->parameterBag); @@ -132,6 +132,7 @@ public function addPackage(string $className, array $constructArguments = []): B public function useDevelopmentMode(): Builder { $this->parameterBag->set('app.devmode', true); + $this->parameterBag->set('container.dumper.inline_factories', false); $this->parameterBag->set('container.dumper.inline_class_loader', false); return $this; diff --git a/test/CompilerTest.php b/test/CompilerTest.php index 53a0f0d1..0589bac2 100644 --- a/test/CompilerTest.php +++ b/test/CompilerTest.php @@ -46,6 +46,7 @@ final class CompilerTest extends TestCase private ContainerConfiguration $config; private ConfigCache $dump; private string $dumpDir; + private ParameterBag $parameters; /** @before */ public function configureDependencies(): void @@ -56,10 +57,10 @@ public function configureDependencies(): void ['services.yml' => 'services: { testing: { class: stdClass } }'], ); - $parameterBag = new ParameterBag(); - $parameterBag->set('app.devmode', true); - $parameterBag->set('container.dumper.inline_factories', false); - $parameterBag->set('container.dumper.inline_class_loader', true); + $this->parameters = new ParameterBag(); + $this->parameters->set('app.devmode', true); + $this->parameters->set('container.dumper.inline_factories', false); + $this->parameters->set('container.dumper.inline_class_loader', true); $this->dumpDir = $this->createDumpDirectory(); $this->dump = new ConfigCache($this->dumpDir . '/AppContainer.php', false); @@ -68,7 +69,7 @@ public function configureDependencies(): void 'Me\\MyApp', [vfsStream::url('tests/services.yml')], [ - [$parameterBag, PassConfig::TYPE_BEFORE_OPTIMIZATION], + [$this->parameters, PassConfig::TYPE_BEFORE_OPTIMIZATION], [[MakeServicesPublic::class, []], PassConfig::TYPE_BEFORE_OPTIMIZATION], ], ); @@ -91,7 +92,7 @@ public function cleanUpDumpDirectory(): void } /** @test */ - public function compileShouldCreateMultipleFiles(): void + public function compileShouldCreateMultipleFilesForDevelopmentMode(): void { $compiler = new Compiler(); $compiler->compile($this->config, $this->dump, new Yaml(__FILE__)); @@ -106,6 +107,25 @@ public function compileShouldCreateMultipleFiles(): void } } + /** @test */ + public function compileShouldInlineFactoriesForProductionMode(): void + { + $this->parameters->set('app.devmode', false); + $this->parameters->set('container.dumper.inline_factories', true); + + $compiler = new Compiler(); + $compiler->compile($this->config, $this->dump, new Yaml(__FILE__)); + + $expectedFiles = self::EXPECTED_FILES; + $generatedFiles = iterator_to_array($this->getGeneratedFiles()); + + self::assertCount(count($expectedFiles) - 1, $generatedFiles); + + foreach ($generatedFiles as $name => $file) { + self::assertContains($name, $expectedFiles); + } + } + /** @test */ public function compileShouldTrackChangesOnTheConfigurationFile(): void { diff --git a/test/ContainerBuilderTest.php b/test/ContainerBuilderTest.php index ae3f292f..8debb4f0 100644 --- a/test/ContainerBuilderTest.php +++ b/test/ContainerBuilderTest.php @@ -90,7 +90,7 @@ public function constructShouldReceiveTheDependenciesAsArguments(): void self::assertNotEmpty($this->config->getPassList()); self::assertFalse($this->parameterBag->get('app.devmode')); self::assertTrue($this->parameterBag->get('container.dumper.inline_class_loader')); - self::assertFalse($this->parameterBag->get('container.dumper.inline_factories')); + self::assertTrue($this->parameterBag->get('container.dumper.inline_factories')); } /**