Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ class Compiler
*/
private $entriesToCompile;

/**
* Progressive counter for definitions.
*
* Each key in $entriesToCompile is defined as 'SubEntry' + counter
* and each definition has always the same key in the CompiledContainer
* if PHP-DI configuration does not change.
*
* @var int
*/
private $subEntryCounter;

/**
* Progressive counter for CompiledContainer get methods.
*
* Each CompiledContainer method name is defined as 'get' + counter
* and remains the same after each recompilation
* if PHP-DI configuration does not change.
*
* @var int
*/
private $methodMappingCounter;

/**
* Map of entry names to method names.
*
Expand Down Expand Up @@ -165,7 +187,7 @@ public function compile(
private function compileDefinition(string $entryName, Definition $definition) : string
{
// Generate a unique method name
$methodName = str_replace('.', '', uniqid('get', true));
$methodName = 'get' . (++$this->methodMappingCounter);
$this->entryToMethodMapping[$entryName] = $methodName;

switch (true) {
Expand Down Expand Up @@ -277,7 +299,7 @@ public function compileValue($value) : string

if ($value instanceof Definition) {
// Give it an arbitrary unique name
$subEntryName = uniqid('SubEntry');
$subEntryName = 'subEntry' . (++$this->subEntryCounter);
// Compile the sub-definition in another method
$methodName = $this->compileDefinition($subEntryName, $value);
// The value is now a method call to that method (which returns the value)
Expand Down