Skip to content

Commit

Permalink
Added support to override the debug info function in build containers
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Jul 21, 2023
1 parent 76c10d8 commit f203ed2
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/ContainerBuilder.php
Expand Up @@ -37,6 +37,13 @@ class ContainerBuilder
*/
protected ?string $containerNamespace = null;

/**
* Should we override the debug function in the generated container
* So that when the container is var_dump't we do not end in an
* infinite recrusion?
*/
protected bool $overrideDebugInfo = true;

/**
* An array of paramters to be builded directly
* as propterty.
Expand Down Expand Up @@ -64,7 +71,7 @@ class ContainerBuilder
*
* @var array<string>
*/
protected array$shared = [];
protected array $shared = [];

/**
* An array of converted service names
Expand Down Expand Up @@ -114,6 +121,18 @@ public function setContainerName(string $containerName)
}
}

/**
* Sets the override debug info flag
* When set to false the generated container will not override the __debugInfo method.
*
* @param bool $overrideDebugInfo
* @return void
*/
public function setOverrideDebugInfo(bool $overrideDebugInfo) : void
{
$this->overrideDebugInfo = $overrideDebugInfo;
}

/**
* Get the current container full name
*
Expand Down Expand Up @@ -343,6 +362,10 @@ public function generate() : string
$buffer .= $this->generateResolverMappings() . "\n";
$buffer .= $this->generateResolverMethods() . "\n";

if ($this->overrideDebugInfo) {
$buffer .= $this->generateDebugInfo() . "\n";
}

return $buffer . "\n}";
}

Expand Down Expand Up @@ -555,4 +578,17 @@ private function generateResolverMethods() : string

return $buffer;
}

private function generateDebugInfo() : string
{
return <<<EOF
/**
* Override the debug info function so that we do not end in an infinite recrusion.
*/
public function __debugInfo() : array
{
return ['services' => \$this->available()];
}
EOF;
}
}

0 comments on commit f203ed2

Please sign in to comment.