-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] Add config_or_fail
helper
#56255
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
base: 12.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||
use Illuminate\Support\Collection; | ||||||||||||||||||||
use Illuminate\Support\Traits\Macroable; | ||||||||||||||||||||
use InvalidArgumentException; | ||||||||||||||||||||
use RuntimeException; | ||||||||||||||||||||
|
||||||||||||||||||||
class Repository implements ArrayAccess, ConfigContract | ||||||||||||||||||||
{ | ||||||||||||||||||||
|
@@ -57,6 +58,19 @@ public function get($key, $default = null) | |||||||||||||||||||
return Arr::get($this->items, $key, $default); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Get the specified configuration value. | ||||||||||||||||||||
* | ||||||||||||||||||||
* @param string $key | ||||||||||||||||||||
* @return mixed | ||||||||||||||||||||
* | ||||||||||||||||||||
* @throws \RuntimeException | ||||||||||||||||||||
*/ | ||||||||||||||||||||
public function getOrFail(string $key) | ||||||||||||||||||||
{ | ||||||||||||||||||||
return $this->get($key) ?? throw new RuntimeException("Configuration value for key [$key] is not set."); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the config is actually null this will not be ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is true, but also expected. If you know it may be null, you are probably better off using the regular What is your expectation on how the method should work? Only throw if the config key is missing? That would hardly be of any use, as most config keys should be static as they're build from the files in ./config/ returning arrays. @macropay-solutions I've added my use case to the description so you may understand my intentions better now. What is your use case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dallyger in that case getNonNullOrFail would be a better naming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not totally convinced yet, because your example seems to deviate from the default out of the box experience. And if you know you do that you should know not to use that helper. I mean this does not change any existing behavior, only adds some small new feature. Let's wait for some more feedback from other peoples and check what they think about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getOrFail is analog to findOrFail firstOrFail in db. If the row is there is should not throw. Analog if the config exists, no matter the value it should not throw. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. Further testing showed me, that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you very much for this trick! However, I'm currently undecided on how I would like to proceed with this PR, as that would fix the issue mentioned above, but also deviate from my use case and no longer solve my issue. Will take some time to think about it. |
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Get many configuration values. | ||||||||||||||||||||
* | ||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.