diff --git a/application/Espo/Core/Binding/Binder.php b/application/Espo/Core/Binding/Binder.php index a9be79c13f..163373f3f6 100644 --- a/application/Espo/Core/Binding/Binder.php +++ b/application/Espo/Core/Binding/Binder.php @@ -30,10 +30,11 @@ namespace Espo\Core\Binding; use LogicException; +use Closure; class Binder { - private $data; + private BindingData $data; public function __construct(BindingData $data) { @@ -130,6 +131,21 @@ class Binder return $this; } + /** + * Creates a contextual binder and pass it as an argument of a callback. + * + * @param string $className A context. + * @param Closure(ContextualBinder): void $callback A callback with a `ContextualBinder` argument. + */ + public function inContext(string $className, Closure $callback): self + { + $contextualBinder = new ContextualBinder($this->data, $className); + + $callback($contextualBinder); + + return $this; + } + /** * Creates a contextual binder. * diff --git a/application/Espo/Core/Binding/BindingContainerBuilder.php b/application/Espo/Core/Binding/BindingContainerBuilder.php index 6d39450750..b4a7424ec0 100644 --- a/application/Espo/Core/Binding/BindingContainerBuilder.php +++ b/application/Espo/Core/Binding/BindingContainerBuilder.php @@ -109,7 +109,7 @@ class BindingContainerBuilder } /** - * Creates a contextual binder. + * Creates a contextual binder and pass it as an argument of a callback. * * @param string $className A context. * @param Closure(ContextualBinder): void $callback A callback with a `ContextualBinder` argument. diff --git a/tests/unit/Espo/Core/Binding/BindingContainerTest.php b/tests/unit/Espo/Core/Binding/BindingContainerTest.php index dc500c9c41..f9f3c70638 100644 --- a/tests/unit/Espo/Core/Binding/BindingContainerTest.php +++ b/tests/unit/Espo/Core/Binding/BindingContainerTest.php @@ -179,6 +179,22 @@ class BindingContainerTest extends \PHPUnit\Framework\TestCase ); } + public function testHasContextTrue0() + { + $this->binder + ->inContext('Espo\\Context', function (ContextualBinder $binder): void { + $binder->bindService('Espo\\Test', 'test'); + }); + + $class = $this->createClassMock('Espo\\Context'); + + $param = $this->createParamMock('test', 'Espo\\Test'); + + $this->assertTrue( + $this->createContainer()->has($class, $param) + ); + } + public function testHasContextTrue1() { $this->binder