From 53e5cea3abc0866227119b780da86d88f7ac150b Mon Sep 17 00:00:00 2001 From: Daniel Esteve Date: Mon, 31 Jan 2022 16:14:53 +0100 Subject: [PATCH] Allow to push and prepend config values on new keys --- src/Illuminate/Config/Repository.php | 4 ++-- tests/Config/RepositoryTest.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Config/Repository.php b/src/Illuminate/Config/Repository.php index 9f7af6d872e9..1719e90a90c2 100644 --- a/src/Illuminate/Config/Repository.php +++ b/src/Illuminate/Config/Repository.php @@ -99,7 +99,7 @@ public function set($key, $value = null) */ public function prepend($key, $value) { - $array = $this->get($key); + $array = $this->get($key, []); array_unshift($array, $value); @@ -115,7 +115,7 @@ public function prepend($key, $value) */ public function push($key, $value) { - $array = $this->get($key); + $array = $this->get($key, []); $array[] = $value; diff --git a/tests/Config/RepositoryTest.php b/tests/Config/RepositoryTest.php index e3137da247a9..53d3cc917a22 100644 --- a/tests/Config/RepositoryTest.php +++ b/tests/Config/RepositoryTest.php @@ -143,6 +143,18 @@ public function testPush() $this->assertSame('xxx', $this->repository->get('array.2')); } + public function testPrependWithNewKey() + { + $this->repository->prepend('new_key', 'xxx'); + $this->assertSame(['xxx'], $this->repository->get('new_key')); + } + + public function testPushWithNewKey() + { + $this->repository->push('new_key', 'xxx'); + $this->assertSame(['xxx'], $this->repository->get('new_key')); + } + public function testAll() { $this->assertSame($this->config, $this->repository->all());