Skip to content

Commit 92c3aa2

Browse files
committed
Allow users to specify configuration keys to be used for primitive binding
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')); ```
1 parent 5ab3464 commit 92c3aa2

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

src/Illuminate/Container/ContextualBindingBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,20 @@ public function giveTagged($tag)
8181
return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices);
8282
});
8383
}
84+
85+
/**
86+
* Define configuration key to be used to look up in configuration to bind as a primitive.
87+
*
88+
* @param string $key
89+
* @param ?string $default
90+
* @return void
91+
*/
92+
public function giveConfig($key, $default = null)
93+
{
94+
$this->give(function ($container) use ($key, $default) {
95+
$config = $container->get('config');
96+
97+
return $config->get($key, $default);
98+
});
99+
}
84100
}

tests/Container/ContextualBindingTest.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Container;
44

55
use Illuminate\Container\Container;
6+
use Illuminate\Config\Repository;
67
use PHPUnit\Framework\TestCase;
78

89
class ContextualBindingTest extends TestCase
@@ -359,6 +360,127 @@ public function testContextualBindingGivesTagsForVariadic()
359360
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
360361
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
361362
}
363+
364+
public function testContextualBindingGivesValuesFromConfigOptionalValueNull()
365+
{
366+
$container = new Container;
367+
368+
$container->singleton('config', function () {
369+
return new Repository([
370+
'test' => [
371+
'username' => 'laravel',
372+
'password' => 'hunter42',
373+
]
374+
]);
375+
});
376+
377+
$container
378+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
379+
->needs('$username')
380+
->giveConfig('test.username');
381+
382+
$container
383+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
384+
->needs('$password')
385+
->giveConfig('test.password');
386+
387+
$resolvedInstance = $container->make(ContainerTestContextInjectFromConfigIndividualValues::class);
388+
389+
$this->assertEquals('laravel', $resolvedInstance->username);
390+
$this->assertEquals('hunter42', $resolvedInstance->password);
391+
$this->assertNull($resolvedInstance->alias);
392+
}
393+
394+
public function testContextualBindingGivesValuesFromConfigOptionalValueSet()
395+
{
396+
$container = new Container;
397+
398+
$container->singleton('config', function () {
399+
return new Repository([
400+
'test' => [
401+
'username' => 'laravel',
402+
'password' => 'hunter42',
403+
'alias' => 'lumen',
404+
]
405+
]);
406+
});
407+
408+
$container
409+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
410+
->needs('$username')
411+
->giveConfig('test.username');
412+
413+
$container
414+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
415+
->needs('$password')
416+
->giveConfig('test.password');
417+
418+
$container
419+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
420+
->needs('$alias')
421+
->giveConfig('test.alias');
422+
423+
$resolvedInstance = $container->make(ContainerTestContextInjectFromConfigIndividualValues::class);
424+
425+
$this->assertEquals('laravel', $resolvedInstance->username);
426+
$this->assertEquals('hunter42', $resolvedInstance->password);
427+
$this->assertEquals('lumen', $resolvedInstance->alias);
428+
}
429+
430+
public function testContextualBindingGivesValuesFromConfigWithDefault()
431+
{
432+
$container = new Container;
433+
434+
$container->singleton('config', function () {
435+
return new Repository([
436+
'test' => [
437+
'password' => 'hunter42',
438+
]
439+
]);
440+
});
441+
442+
$container
443+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
444+
->needs('$username')
445+
->giveConfig('test.username', 'DEFAULT_USERNAME');
446+
447+
$container
448+
->when(ContainerTestContextInjectFromConfigIndividualValues::class)
449+
->needs('$password')
450+
->giveConfig('test.password');
451+
452+
$resolvedInstance = $container->make(ContainerTestContextInjectFromConfigIndividualValues::class);
453+
454+
$this->assertEquals('DEFAULT_USERNAME', $resolvedInstance->username);
455+
$this->assertEquals('hunter42', $resolvedInstance->password);
456+
$this->assertNull($resolvedInstance->alias);
457+
}
458+
459+
public function testContextualBindingGivesValuesFromConfigArray()
460+
{
461+
$container = new Container;
462+
463+
$container->singleton('config', function () {
464+
return new Repository([
465+
'test' => [
466+
'username' => 'laravel',
467+
'password' => 'hunter42',
468+
'alias' => 'lumen',
469+
]
470+
]);
471+
});
472+
473+
$container
474+
->when(ContainerTestContextInjectFromConfigArray::class)
475+
->needs('$settings')
476+
->giveConfig('test');
477+
478+
$resolvedInstance = $container->make(ContainerTestContextInjectFromConfigArray::class);
479+
480+
$this->assertEquals('laravel', $resolvedInstance->settings['username']);
481+
$this->assertEquals('hunter42', $resolvedInstance->settings['password']);
482+
$this->assertEquals('lumen', $resolvedInstance->settings['alias']);
483+
}
362484
}
363485

364486
interface IContainerContextContractStub
@@ -474,3 +596,27 @@ public function __construct(ContainerContextNonContractStub $other, IContainerCo
474596
$this->stubs = $stubs;
475597
}
476598
}
599+
600+
class ContainerTestContextInjectFromConfigIndividualValues
601+
{
602+
public $username;
603+
public $password;
604+
public $alias = null;
605+
606+
public function __construct($username, $password, $alias = null)
607+
{
608+
$this->username = $username;
609+
$this->password = $password;
610+
$this->alias = $alias;
611+
}
612+
}
613+
614+
class ContainerTestContextInjectFromConfigArray
615+
{
616+
public $settings;
617+
618+
public function __construct($settings)
619+
{
620+
$this->settings = $settings;
621+
}
622+
}

0 commit comments

Comments
 (0)