binder inContextd

This commit is contained in:
Yuri Kuznetsov
2022-06-03 11:50:53 +03:00
parent d6b48962ce
commit e6b7914ccd
3 changed files with 34 additions and 2 deletions

View File

@@ -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.
*

View File

@@ -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.

View File

@@ -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