Skip to content

Commit

Permalink
Accessor, Locator, Factory: generates promoted properties
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 30, 2023
1 parent a0c9ea4 commit f90dab0
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -18,7 +18,7 @@
"php": ">=8.0 <8.4",
"ext-tokenizer": "*",
"nette/neon": "^3.3 || ^4.0",
"nette/php-generator": "^4.0",
"nette/php-generator": "^4.0.6",
"nette/robot-loader": "^4.0",
"nette/schema": "^1.2.3",
"nette/utils": "^4.0"
Expand Down
9 changes: 3 additions & 6 deletions src/DI/Definitions/AccessorDefinition.php
Expand Up @@ -112,13 +112,10 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
$class = (new Nette\PhpGenerator\ClassType)
->addImplement($this->getType());

$class->addProperty('container')
->setPrivate();

$class->addMethod('__construct')
->addBody('$this->container = $container;')
->addParameter('container')
->setType($generator->getClassName());
->addPromotedParameter('container')
->setPrivate()
->setType($generator->getClassName());

$rm = new \ReflectionMethod($this->getType(), self::MethodGet);

Expand Down
9 changes: 3 additions & 6 deletions src/DI/Definitions/FactoryDefinition.php
Expand Up @@ -202,13 +202,10 @@ public function generateMethod(Php\Method $method, Nette\DI\PhpGenerator $genera
$class = (new Php\ClassType)
->addImplement($this->getType());

$class->addProperty('container')
->setPrivate();

$class->addMethod('__construct')
->addBody('$this->container = $container;')
->addParameter('container')
->setType($generator->getClassName());
->addPromotedParameter('container')
->setPrivate()
->setType($generator->getClassName());

$methodCreate = $class->addMethod(self::MethodCreate);
$this->resultDefinition->generateMethod($methodCreate, $generator);
Expand Down
9 changes: 3 additions & 6 deletions src/DI/Definitions/LocatorDefinition.php
Expand Up @@ -133,13 +133,10 @@ public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGe
$class = (new Nette\PhpGenerator\ClassType)
->addImplement($this->getType());

$class->addProperty('container')
->setPrivate();

$class->addMethod('__construct')
->addBody('$this->container = $container;')
->addParameter('container')
->setType($generator->getClassName());
->addPromotedParameter('container')
->setPrivate()
->setType($generator->getClassName());

foreach ((new \ReflectionClass($this->getType()))->getMethods() as $rm) {
preg_match('#^(get|create)(.*)#', $rm->name, $m);
Expand Down
9 changes: 3 additions & 6 deletions tests/DI/Definitions.AccessorDefinition.render.phpt
Expand Up @@ -39,12 +39,9 @@ test('', function () {
public function createServiceAbc(): Good2
{
return new class ($this) implements Good2 {
private $container;
public function __construct($container)
{
$this->container = $container;
public function __construct(
private $container,
) {
}
Expand Down
9 changes: 3 additions & 6 deletions tests/DI/Definitions.FactoryDefinition.render.phpt
Expand Up @@ -39,12 +39,9 @@ test('', function () {
public function createServiceAbc(): Good2
{
return new class ($this) implements Good2 {
private $container;
public function __construct($container)
{
$this->container = $container;
public function __construct(
private $container,
) {
}
Expand Down
7 changes: 3 additions & 4 deletions tests/DI/Definitions.LocatorDefinition.render.create.phpt
Expand Up @@ -40,13 +40,12 @@ test('', function () {
public function createServiceAbc(): Good
{
return new class ($this) implements Good {
private $container;%A?%
private $mapping = ['first' => 'a', 'second' => 'a'];
public function __construct($container)
{
$this->container = $container;
public function __construct(
private $container,
) {
}
Expand Down
7 changes: 3 additions & 4 deletions tests/DI/Definitions.LocatorDefinition.render.get.phpt
Expand Up @@ -40,13 +40,12 @@ test('', function () {
public function createServiceAbc(): Good
{
return new class ($this) implements Good {
private $container;%A?%
private $mapping = ['first' => 'a', 'second' => 'a'];
public function __construct($container)
{
$this->container = $container;
public function __construct(
private $container,
) {
}
Expand Down
9 changes: 3 additions & 6 deletions tests/DI/Definitions.LocatorDefinition.render.multi.phpt
Expand Up @@ -42,12 +42,9 @@ test('', function () {
public function createServiceAbc(): Good
{
return new class ($this) implements Good {
private $container;
public function __construct($container)
{
$this->container = $container;
public function __construct(
private $container,
) {
}
Expand Down

0 comments on commit f90dab0

Please sign in to comment.