Skip to content

Commit

Permalink
Inline service factories when in production mode
Browse files Browse the repository at this point in the history
This makes the OPCache more efficient by decreasing the amount of files
to be managed by that layer.
  • Loading branch information
lcobucci committed Oct 12, 2021
1 parent cf9a0bd commit cc1cc9b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 26 additions & 6 deletions test/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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],
],
);
Expand All @@ -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__));
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down

0 comments on commit cc1cc9b

Please sign in to comment.