laravel-shift / blueprint

A code generation tool for Laravel developers.
MIT License
2.89k stars 273 forks source link

Allow multiple dispatch, fire, notify and send statements #623

Closed faustbrian closed 1 year ago

faustbrian commented 1 year ago

Alternative to https://github.com/laravel-shift/blueprint/pull/612 for https://github.com/laravel-shift/blueprint/issues/611

This new approach preprocesses the YAML content and transforms duplicate keys into keys that are suffixed with their line number to avoid key collisions. This also reverts https://github.com/laravel-shift/blueprint/pull/619.

Input https://github.com/laravel-shift/blueprint/pull/623/files#diff-a4f4e06b3fdb4a8424880f544fca3c75a0ecf2ebb5158feba4963f7066e2d265

Output https://github.com/laravel-shift/blueprint/pull/623/files#diff-72d9e08bf23239b5487ea8767fe78e8f3e9be1b14e5bd64e1ae2c4848da167f1

faustbrian commented 1 year ago

This requires quite some regex voodoo to get it working. It's almost working but there seems to be something somewhere that causes duplication, for example when you run vendor/bin/phpunit --filter="output_writes_events" you'll get duplicated events. Not sure yet what causes that but I assume the multiple calls to the controller lexer.

jasonmccreary commented 1 year ago

I can take a stab if you want to leave it for the weekend.

faustbrian commented 1 year ago

I'll continue tomorrow on it but feel free to poke around, probably something obvious that causes the array items to get duplicated 😅

jasonmccreary commented 1 year ago

Cool. You on Twitter? I'll give you a shout out.

jasonmccreary commented 1 year ago

I think parsing out the controller section via regex will prove challenging and likely have edge cases. If you wanted to produce the dashed array syntax, then you'd might be better off going line by line through the controller section.

However, thinking about it more, these statements may not be sequential. And moving them into a single array, may not generate the code the user intended.

For example:

      validate: title, content
      save: ticket
      send: ReviewNotification to:ticket.reviewer with:ticket
      dispatch: SyncMedia with:ticket
      send: TicketOpened to:ticket.author with:ticket

As such, I think simply finding these indented properties and giving them a numerical suffix is the way to go. Then update the lexer to handle numbered properties when creating the statement. If so, you could likely revert #619.

faustbrian commented 1 year ago

@jasonmccreary updated the OP with the (I think) final changes

jasonmccreary commented 1 year ago

I foresee a lot of bugs, but here we go...