Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically build nested attributes from object #130

Closed
Eydamos opened this issue Mar 24, 2023 · 2 comments
Closed

Automatically build nested attributes from object #130

Eydamos opened this issue Mar 24, 2023 · 2 comments

Comments

@Eydamos
Copy link

Eydamos commented Mar 24, 2023

If I'm not missing something in the documentation it is currently very cumbersome to have nested attributes as the nested attributes use the new keyword.
Therefore I would need to manually build a string like new SomeClassName('argument1', someOptionalParam: 'argument2') and then wrap it with Literal() to be able to generate nested attributes.
Instead it would be very helpful if the generator would recognise that there is an object as a parameter for the attribute and render it accordingly.

Example generator:

$file = new PhpFile();
$namespace = $file->addNamespace('App');
$class = $namespace->addClass('SomeEntity');

$tableAttributeParams = [
    'name'    => new Literal('self::TABLE_NAME'),
    'indexes' => [],
];

$index = new ORM\Index();
$index->name = new Literal('self::INDEX_STATUS');
$index->columns = [new Literal('self::COLUMN_STATUS')];
$tableAttributeParams['indexes'][] = $index;

$printer = new PsrPrinter();
echo $printer->printFile($file);

Expected result:

#[ORM\Table(
    name: self::TABLE_NAME,
    indexes: [
        new ORM\Index(columns: [self::COLUMN_STATUS], name: self::INDEX_STATUS)
    ]
)]
// ...

Actual result:

#[ORM\Table(
    name: self::TABLE_NAME,
    indexes: [
        \Nette\PhpGenerator\Dumper::createObject(\Doctrine\ORM\Mapping\Index::class, [
            'name' => self::INDEX_STATUS,
            'columns' => [self::COLUMN_STATUS],
            'fields' => null,
            'flags' => null,
            'options' => null,
        ]),
    ],
    uniqueConstraints: [],
    options: [],
)]
// ...
@JanTvrdik
Copy link
Contributor

Agree with the problem, not so much with the proposed solution. You cannot revert __construct call, unless it uses promoted properties only.

What I think should work always is nesting Nette\PhpGenerator\Attribute objects, i.e.

$class->addAttribute(ORM\Table::class, [
    'name' => new Literal('self::TABLE_NAME'),
    'indexes' => [
        new Attribute(ORM\Index::class, [
            'columns' => [...],
        ]),
    ],
]);

@dg dg closed this as completed in a4669d1 Aug 29, 2023
@dg
Copy link
Member

dg commented Aug 29, 2023

This works:

$class->addAttribute(ORM\Table::class, [
    'name' => new Literal('self::TABLE_NAME'),
    'indexes' => [
        Literal::new(ORM\Index::class, [
            'columns' => [...],
        ]),
    ],
]);

dg added a commit that referenced this issue Aug 29, 2023
dg added a commit that referenced this issue Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants