Skip to content

Add Closure support to items() for lazy evaluation#148

Open
WarLikeLaux wants to merge 6 commits intoyiisoft:masterfrom
WarLikeLaux:add-closure-items
Open

Add Closure support to items() for lazy evaluation#148
WarLikeLaux wants to merge 6 commits intoyiisoft:masterfrom
WarLikeLaux:add-closure-items

Conversation

@WarLikeLaux
Copy link
Copy Markdown
Contributor

Q A
Is bugfix?
New feature? ✔️
Docs added?
Tests added? ✔️
Breaks BC?

What does this PR do?

Adds Closure support to items() in Breadcrumbs, Dropdown, and Menu for lazy evaluation.

When a widget is configured via DI container, items are defined before runtime data (repositories, current user, locale) is available. Passing a Closure defers item resolution until render() is called.

Menu::widget()
    ->items(fn() => $this->buildMenuItems())
    ->render();

Array input works exactly as before.

No BC break: the items() parameter type is widened from array to array|Closure, so all existing calls remain valid.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (011785f) to head (113a785).

Additional details and impacted files
@@             Coverage Diff             @@
##              master      #148   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       289       292    +3     
===========================================
  Files              8         8           
  Lines            911       914    +3     
===========================================
+ Hits             911       914    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be documented with an example on why/when it's needed.

Comment thread CHANGELOG.md Outdated
@WarLikeLaux
Copy link
Copy Markdown
Contributor Author

Should be documented with an example on why/when it's needed.

Added docs and fixed the changelog number.

While writing the documentation I realized the main use case I had in mind (DI widget definitions) doesn't actually work well in practice. With Yii3's ArrayDefinition, method arguments are passed as plain values, so the Closure inside a DI config has no access to the container or runtime services. The only scenario left is when a widget instance is created early and rendered later, but in most cases you can just pass the array directly at render time.

If you think this feature doesn't carry its weight, I'm fine with closing the PR.

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