Skip to content

Conversation

marcorieser
Copy link
Contributor

@marcorieser marcorieser commented Apr 14, 2025

This PR adds support for accessing public properties on plain objects (non-augmentable / non-arrayaccess).

Backstory:
In the Statamic Livewire addon, I've added support for computed properties. Objects returned from there are not always augmentable objects. So I thought it would be nice to be able to process those objects in Antlers, too, meaning calling public methods and accessing public properties (on non-augmentable / non-arrayaccess objects).

{{ livewire:computed:property:nested_path:plain_object:public_method }}
{{ livewire:computed:property:nested_path:plain_object:public_property }}
class Dummy
{
    public string $public_property = 'Public Property';

    public function publicMethod()
    {
        return 'Public Method';
    }
}

I started iterating the path segments and handling all the different types of values/objects myself, but then realized Statamic already can do that (but didn't find the correct place where this is handled in the runtime parser). I had a quick talk with Duncan and John Koster on how to handle that. We (basically John 😅) figured out, it's the PathDataManager. This works perfectly, but it currently doesn't check for public properties on objects.

https://discord.com/channels/489818810157891584/1360647492701519963

@marcorieser marcorieser changed the title Add support for public properties to PathDataManager [5.x] Add support for public properties to PathDataManager Apr 14, 2025
@jasonvarga
Copy link
Member

Please edit the description to explain the PR.

@marcorieser
Copy link
Contributor Author

Sure, will do. It's currently just a draft.

@marcorieser

This comment was marked as outdated.

@marcorieser marcorieser marked this pull request as ready for review April 16, 2025 09:49
@jasonvarga
Copy link
Member

jasonvarga commented Apr 22, 2025

A few things:

  1. This needs a test
  2. If the property is protected or private, you get an error. I think it should return null.
  3. Properties in conditions don't work. e.g. {{ if myclass:myproperty }}yes{{ else }}no{{ /if }} always returns true regardless of the value of the property and even if it doesn't exist.

I know for 3 that this is how it already worked, so its still just as broken, but if we're adding support for outputting properties, they should be able to be used in conditions. It works for methods.

@marcorieser marcorieser marked this pull request as draft May 1, 2025 08:05
@marcorieser
Copy link
Contributor Author

@jasonvarga I've fixed the points you mentioned. But I'm not sure if all the tests belong in that test class.

@marcorieser marcorieser marked this pull request as ready for review June 28, 2025 06:54
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

Successfully merging this pull request may close these issues.

2 participants