Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI] add LazyString for lazy computation of string values injected into services #34013

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

Merged
merged 1 commit into from
Oct 18, 2019

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Oct 17, 2019

Q A
Branch? 4.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

This is an idea I should have had years ago :)

By wrapping any callable into a LazyString, we allow resolving the corresponding string value lazily (eg because the value comes from a remote server).

The tricky parts are memoization and error handling, which are both dealt with in the class.

This is currently part of #33997

@stof
Copy link
Member

stof commented Oct 17, 2019

should we support lazy-loading a service reference in a array($object, $method) callback, as we do for event listeners ?

@nicolas-grekas nicolas-grekas force-pushed the di-lazystring branch 2 times, most recently from 6bce16d to aebd082 Compare October 17, 2019 13:11
@nicolas-grekas
Copy link
Member Author

should we support lazy-loading a service reference in a array($object, $method) callback, as we do for event listeners ?

good idea, updated

@nicolas-grekas
Copy link
Member Author

(fabbot failure is false positive)

@Devristo
Copy link
Contributor

Could we do something similar, yet different, for string formatting? Some languages / libraries offer a string interpolation / formatting function which is lazily evaluated. An example use case is constructing a log message, which may or not be evaluated depending on the log level.

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Oct 17, 2019

@Devristo I feel like that's what this provides already:
$s = LazyString::fromCallable(function () { return compute_log_line(); }); would do it
(of course, it requires $s to not be resolved as a string before the very last moment, but that's out of scope here.)

@fabpot
Copy link
Member

fabpot commented Oct 18, 2019

Thank you @nicolas-grekas.

fabpot added a commit that referenced this pull request Oct 18, 2019
…alues injected into services (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[DI] add `LazyString` for lazy computation of string values injected into services

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

This is an idea I should have had years ago :)

By wrapping any callable into a `LazyString`, we allow resolving the corresponding string value lazily (eg because the value comes from a remote server).

The tricky parts are memoization and error handling, which are both dealt with in the class.

This is currently part of #33997

Commits
-------

ccb0365 [DI] add `LazyString` for lazy computation of string values injected into services
@fabpot fabpot merged commit ccb0365 into symfony:4.4 Oct 18, 2019
@nicolas-grekas nicolas-grekas deleted the di-lazystring branch October 18, 2019 12:46
@symfony symfony deleted a comment from stof Oct 19, 2019
nicolas-grekas added a commit that referenced this pull request Oct 22, 2019
…s-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[EventDispatcher] handle lazy-callable invokable

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Extracted from #34013, for consistency mostly.

Commits
-------

9df4c7d [EventDispatcher] handle lazy-callable invokable
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants