Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container factory method #229

Merged
merged 5 commits into from Nov 13, 2022
Merged

Add container factory method #229

merged 5 commits into from Nov 13, 2022

Conversation

Chemaclass
Copy link
Member

@Chemaclass Chemaclass commented Nov 12, 2022

馃摎 Description

Currently, the Container caches in memory the result of the service after Container::get('service-key') is called. This is useful for Facades but it might not be the best when creating other types of dependencies.

With this new feature, you can define in your DependencyProvider to resolve a particular service every time, avoiding caching it in memory. This is useful when you want to force the creation of a dependency instead of sharing it as singleton.

馃敄 Changes

  • Add Container::factory() to avoid caching the result of the service when calling Container::get('service-key').

@Chemaclass Chemaclass added the enhancement New feature or request label Nov 12, 2022
@Chemaclass Chemaclass self-assigned this Nov 12, 2022
{
$this->container->set(
'service_name',
$this->container->factory(
Copy link
Member

Choose a reason for hiding this comment

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

I am not completely convinced of the factory name.
Probably, I would rename the methods to:

  • instance (for the factory method)
  • singleton (for the get method)

What do you think? 馃

Similar to how Laravel does.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good observation, but actually this is the same naming Spryker is using in their internal Container as well. I kinda like it, and I would prefer to have a similar name in this area.

@Chemaclass Chemaclass merged commit ba5ff2d into master Nov 13, 2022
@Chemaclass Chemaclass deleted the feature/container-factory branch November 13, 2022 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

None yet

2 participants