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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allow users to specify configuration keys to be used for primitive binding #36241

Merged
merged 1 commit into from
Feb 12, 2021

Conversation

simensen
Copy link
Contributor

Syntactic sugar around fancy calls to ->give() related to getting primitives from configuration.

Most of the time when I'm doing primitive binding it is for the purpose of using configuration values as constructor arguments. A lot of function calls are used and feel pretty bulky compared to being able to just say, "hey, get the value from this config key."

This PR provides the ability to do just that. Need an API key? ->need('$apiKey')->giveConfig('awesome_api.key') is all you'd need to set in a service provider now.

$container
    ->when(ContainerTestContextInjectFromConfigIndividualValues::class)
    ->needs('$username')
    ->giveConfig('test.username');

Is the same as:

$container
    ->when(ContainerTestContextInjectFromConfigIndividualValues::class)
    ->needs('$username')
    ->give(function () {
        return config('test.username');
    });

and

$container
    ->when(ContainerTestContextInjectFromConfigIndividualValues::class)
    ->needs('$username')
    ->give(fn() => config('test.username'));

I believe this may require additional work on the contract (?) that I'll do if this gets approved. Would just need to know which branch to target that change on. I can also do the docs on this if it looks like this will be able to be merged. :)

…nding

Syntactic sugar around fancy calls to `->give()`.

```
$container
    ->when(ContainerTestContextInjectFromConfigIndividualValues::class)
    ->needs('$username')
    ->giveConfig('test.username');
```

Is the same as:

```
$container
    ->when(ContainerTestContextInjectFromConfigIndividualValues::class)
    ->needs('$username')
    ->give(function () {
        return config('test.username');
    });
```

and

```
$container
    ->when(ContainerTestContextInjectFromConfigIndividualValues::class)
    ->needs('$username')
    ->give(fn() => config('test.username'));
```
@taylorotwell taylorotwell merged commit 20a5688 into laravel:8.x Feb 12, 2021
@simensen simensen deleted the bind-from-config branch February 12, 2021 21:58
@GrahamCampbell GrahamCampbell changed the title Allow users to specify configuration keys to be used for primitive binding [8.x] Allow users to specify configuration keys to be used for primitive binding Feb 13, 2021
* Define configuration key to be used to look up in configuration to bind as a primitive.
*
* @param string $key
* @param ?string $default
Copy link
Member

Choose a reason for hiding this comment

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

bad cs

Copy link

@matason matason Feb 13, 2021

Choose a reason for hiding this comment

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

Bad CS because the first line of the docblock should be wrapped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad on this. My guess might be there are two spaces between string and $key instead of just one? Or maybe the long line. :) Either way, my bad. I had actually fixed a few other things that the styleci bot complained about and figured it was all good since it passed.

Copy link

Choose a reason for hiding this comment

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

@simensen I found this which says two spaces between @param and the type and then two spaces before the variable name so it could be that.

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.

4 participants