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

How to use the decorator feature for Asset methods #4

Open
gaufde opened this issue Jul 28, 2024 · 5 comments
Open

How to use the decorator feature for Asset methods #4

gaufde opened this issue Jul 28, 2024 · 5 comments

Comments

@gaufde
Copy link

gaufde commented Jul 28, 2024

Hello,

I'm not sure if this is the correct place to post something like this, so feel free to redirect me if needed.

I am trying to generate a stub so that PhpStorm doesn't complain that read() is not a method of Kirby\Filesystem\Asset when doing this: asset('logo.svg')->read().

My interpretation of the docs is that the decorator feature was designed to handle this, so I edited my /site/config/config.php:

return [
    'debug' => true,
    'email' => [
        'transport' => [
            'type' => 'smtp',
            'host' => 'localhost',
            'port' => 1025,
            'security' => false
        ]
    ],
    'lukaskleinschmidt.types' => [
        'decorators' => [
            \Kirby\Filesystem\Asset::class => [
                'read' => [
                    '@return string|false',
                ],
            ],
        ],
    ],
];

However, it doesn't work as I would expect. If I do a diff with the generated types file before and after I added the above to my config, I see that there is no change:
image

For fun, I also tried editing the /plugins/kirby-types/config.php file to add:

        Asset::class => [
            'fields' => [
                '@return' => 'string|false',
            ],
        ],

But that didn't seem to have an effect either. So, is there something I am doing wrong or is this a bug?

@lukaskleinschmidt
Copy link
Owner

lukaskleinschmidt commented Jul 29, 2024

The issue is that the read method does not exists on the Kirby\Filesystem\Asset itself. Rather it gets called with the magic __call method dynamically on s underlying Kirby\Filesystem\File object. So the decorators do not work in that case.

Usually you could do something like this for such dynamic classes:

namespace Kirby\Filesystem
{
    /**
     * @mixin \Kirby\Filesystem\File
     */
    class Asset
    {
        // ...
    }
}

This nothing this plugin can currently provide.
But perhaps an interesting concept to add.

@gaufde
Copy link
Author

gaufde commented Jul 29, 2024

Oh interesting. I knew read() was a method of Kirby\Filesystem\File but I didn't realize that meant it should be inserted in a different manner compared to the methods dynamically inserted via closures.

Would it make more sense to ask the core team to add the @mixin annotation directly to Kirby\Filesystem\Asset?

@lukaskleinschmidt
Copy link
Owner

It would make sense to have this in the core. Not sure where their stance is on using @mixin though

@gaufde
Copy link
Author

gaufde commented Aug 1, 2024

Okay! I'll close this topic here and then approach the Kirby team about the possibility of adding @mixin.

Do you know where the best place to approach them about this is? I think the options are GitHub, Discord, the forum, and https://feedback.getkirby.com/, but it isn't exactly clear to me what is the preferred place to reach the Dev team about something like this.

@lukaskleinschmidt
Copy link
Owner

Github is probably a good place to start. You could also try to directly create a PR for that if you can.

Feel free to reference this issue and you can leave it open. I'm considering adding the @mixin functionality it to the types plugin as it is probably helpful in other cases as well

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

2 participants