Attempt to infer theme variables using an attribute on theme object propertirs

Created on 3 August 2024, 6 months ago

Problem/Motivation

I wonder if we could add a property attribute e.g #[Theme\Variable] and put that on constructor Params and have the theme hook variables inferred from a theme object

Between the constructor, a factory method, the theme definition and the build method, there's a lot of boilerplate around the variable names

I also wonder if the method that does the build could auto fill # keys from properties with attributes so if you don't need any logic these get passed through to the render array automatically

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @larowlan
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    I mentally approved this, but was on holiday at the time.

    -

    Further ideas, aligning with other Attribute style features.

    The attribute should be able to target classes or methods.

    If its on the class, the constructor is chosen. Needs compiler time validation to ensure constructor is public.

    If its on a method, use this instead. Same public check.

    Otherwise further enforcement of only one method per class (throw exceptions if found on multiple?)

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Implemented in library @ https://github.com/dpi/pinto/pull/16

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Slots feature @ πŸ“Œ Implement Slots Active

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    This issue was from ✨ Allow objects in theme definitions Active

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    This is integrated and merged.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Had some further thoughts about this,

    I wonder if we could add a property attribute e.g #[Theme\Variable] and put that on constructor Params and have the theme hook variables inferred from a theme object

    Right now all slots come from reflection. This requirement kind of matches the other part of slots that needs to be implemented, strict listings of slots instead of reflection: https://github.com/dpi/pinto/issues/20

    I also wonder if the method that does the build could auto fill # keys from properties with attributes so if you don't need any logic these get passed through to the render array automatically

    Maybe each slot can have a thing where Pinto can lift out a value from where a slot definition is on a promoted public property, so the build method doesnt need to fill the slot value. And the return value could become optional if all slots are derived from promoted properties.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    Explicit slot definitions are now a thing, per ✨ Allow theme objects to define a list of slots instead of only by reflection Active

    E.g:

    #[Slots(
      slots: [
        new Slot(name: SlotEnum::Slot1),
        new Slot(name: SlotEnum::Slot2, defaultValue: 3),
      ],
    )]
    final class SlotsObjectExplicitEnums {}
    

    What remains is the promoted public property ideas.

  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    This is what I imaging it would look like:

    \Pinto\Attribute\ObjectType\Slots.

    Add new public bool $bindPromotedProperties = false, param:

    When true, and when there is nothing in $slots, values of promoted properties will be set on the \Pinto\Slots\Build object provided to the builder (usually __invoke) AFTER the builder method has run, and only if the builder method did not already set a value for the slot.

    This would effectively allow the builder method to be completely empty.

    I think it might also be helpful for a new attribute that could be added to regular class public properties (not promoted properties), that slot values can similarly be derived from.

    class MyObj {
      #[BindSlotTo[target: 'name_of_a_slot_OR_enumCase']
      public $mySlot = 'Foo';
    }
    

    Is this what you had in mind?

    Any better vocablary ~bind?

    • dpi β†’ committed 924ba97a on 1.x
      Issue #3465597 by dpi, larowlan: ✨ Bind slots values from from promoted...
  • πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

    As found in 0.2.1

  • Status changed to Fixed about 2 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024