-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Illuminate\Config\Attributes; | ||
|
||
use Attribute; | ||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Contracts\Container\DependencyResolver; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class InjectFromConfig implements DependencyResolver | ||
{ | ||
/** | ||
* Key to get from config. | ||
* | ||
* @var string|array | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* Default in case config is not present or bound. | ||
* | ||
* @var mixed | ||
*/ | ||
private $default; | ||
|
||
/** | ||
* Create a new InjectFromConfig instance. | ||
* | ||
* @param array|string $key | ||
* @param mixed $default | ||
* @return void | ||
*/ | ||
public function __construct($key, $default = null) | ||
{ | ||
$this->key = $key; | ||
$this->default = $default; | ||
} | ||
|
||
/** | ||
* Resolve the dependency from the container. | ||
* | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return mixed | ||
* @throws \Illuminate\Contracts\Container\BindingResolutionException | ||
*/ | ||
public function resolve(Container $container): mixed | ||
{ | ||
if (!$container->bound('config')) { | ||
return value($this->default); | ||
} | ||
|
||
return $container->make('config')->get($this->key, $this->default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Illuminate\Contracts\Container; | ||
|
||
interface DependencyResolver | ||
{ | ||
/** | ||
* Resolve the dependency from the container. | ||
* | ||
* @param \Illuminate\Contracts\Container\Container $container | ||
* @return mixed | ||
*/ | ||
public function resolve(Container $container): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Config\Attributes; | ||
|
||
use Illuminate\Config\Attributes\InjectFromConfig; | ||
use Illuminate\Contracts\Config\Repository; | ||
use Illuminate\Contracts\Container\Container; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InjectFromConfigTest extends TestCase | ||
{ | ||
public function testItReturnsDefaultWhenConfigNotBound() | ||
{ | ||
$attribute = new InjectFromConfig('app.name', 'default'); | ||
|
||
$container = m::mock(Container::class); | ||
$container->shouldReceive('bound')->with('config')->once()->andReturnFalse(); | ||
|
||
$this->assertSame('default', $attribute->resolve($container)); | ||
} | ||
|
||
public function testItGetsFromConfig() | ||
{ | ||
$attribute = new InjectFromConfig('app.name', 'default'); | ||
|
||
$config = m::mock(Repository::class); | ||
$config->shouldReceive('get')->with('app.name', 'default')->once()->andReturn('Laravel'); | ||
|
||
$container = m::mock(Container::class); | ||
$container->shouldReceive('bound')->with('config')->once()->andReturnTrue(); | ||
$container->shouldReceive('make')->with('config')->once()->andReturn($config); | ||
|
||
$this->assertSame('Laravel', $attribute->resolve($container)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters