Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Compiler
/** @var string */
private $className = 'Container';

/** @var bool */
private $strictTypes = true;


public function __construct(ContainerBuilder $builder = null)
{
Expand Down Expand Up @@ -100,6 +103,17 @@ public function setClassName(string $className)
}


/**
* Enables/disables declare(strict_types=1) in generated code.
* @return static
*/
public function setStrictTypes(bool $on)
{
$this->strictTypes = $on;
return $this;
}


/**
* Adds new configuration.
* @return static
Expand Down Expand Up @@ -281,7 +295,7 @@ public function generateCode(): string

$this->builder->complete();

$generator = new PhpGenerator($this->builder);
$generator = new PhpGenerator($this->builder, $this->strictTypes);
$class = $generator->generate($this->className);
$this->dependencies->add($this->builder->getDependencies());

Expand Down
19 changes: 17 additions & 2 deletions src/DI/PhpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,25 @@ class PhpGenerator
/** @var string */
private $className;

/** @var bool */
private $strictTypes = true;

public function __construct(ContainerBuilder $builder)

public function __construct(ContainerBuilder $builder, bool $strictTypes = true)
{
$this->builder = $builder;
$this->strictTypes = $strictTypes;
}


/**
* Enables/disables declare(strict_types=1) in generated code.
* @return static
*/
public function setStrictTypes(bool $on)
{
$this->strictTypes = $on;
return $this;
}


Expand Down Expand Up @@ -76,7 +91,7 @@ public function toString(Php\ClassType $class): string
{
return '/** @noinspection PhpParamsInspection,PhpMethodMayBeStaticInspection */

declare(strict_types=1);
' . ($this->strictTypes ? 'declare(strict_types=1);' : '') . '

' . $class->__toString();
}
Expand Down