Skip to content

EspoCRM – Open Source CRM Application

Binding with qualification

Yurii456402847e55
diff --git a/application/Espo/Core/Binding/Attributes/Qualify.php b/application/Espo/Core/Binding/Attributes/Qualify.php
new file mode 100644
index 0000000000..ac113a96ef
--- /dev/null
+++ b/application/Espo/Core/Binding/Attributes/Qualify.php
@@ -0,0 +1,45 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Binding\Attributes;
+
+use Attribute;
+
+/**
+ * Assigns a qualifier to a constructor parameter.
+ *
+ * @since 10.1.0
+ */
+#[Attribute(Attribute::TARGET_PARAMETER)]
+readonly class Qualify
+{
+ public function __construct(
+ public string $qualifier,
+ ) {}
+}
diff --git a/application/Espo/Core/Binding/Binder.php b/application/Espo/Core/Binding/Binder.php
index a5d41469c9..d7d203b649 100644
--- a/application/Espo/Core/Binding/Binder.php
+++ b/application/Espo/Core/Binding/Binder.php
@@ -30,6 +30,7 @@
namespace Espo\Core\Binding;
use Espo\Core\Binding\Key\NamedClassKey;
+use Espo\Core\Binding\Key\QualifiedClassKey;
use LogicException;
use Closure;
@@ -42,11 +43,15 @@ class Binder
* Bind an interface to an implementation.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param class-string<T> $implementationClassName An implementation class name.
*/
- public function bindImplementation(string|NamedClassKey $key, string $implementationClassName): self
- {
+ public function bindImplementation(
+ string|NamedClassKey|QualifiedClassKey $key,
+ string $implementationClassName,
+ ): self {
+
$key = self::keyToString($key);
$this->validateBindingKey($key);
@@ -58,10 +63,11 @@ class Binder
/**
* Bind an interface to a specific service.
*
- * @param class-string<object>|NamedClassKey<object> $key An interface or interface with a parameter name.
+ * @param class-string<object>|NamedClassKey<object>|QualifiedClassKey<object> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param string $serviceName A service name.
*/
- public function bindService(string|NamedClassKey $key, string $serviceName): self
+ public function bindService(string|NamedClassKey|QualifiedClassKey $key, string $serviceName): self
{
$key = self::keyToString($key);
$this->validateBindingKey($key);
@@ -75,11 +81,12 @@ class Binder
* Bind an interface to a callback.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param Closure $callback A callback that will resolve a dependency.
* @todo Change to Closure(...): T Once https://github.com/phpstan/phpstan/issues/8214 is implemented.
*/
- public function bindCallback(string|NamedClassKey $key, Closure $callback): self
+ public function bindCallback(string|NamedClassKey|QualifiedClassKey $key, Closure $callback): self
{
$key = self::keyToString($key);
$this->validateBindingKey($key);
@@ -93,10 +100,11 @@ class Binder
* Bind an interface to a specific instance.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param T $instance An instance.
*/
- public function bindInstance(string|NamedClassKey $key, object $instance): self
+ public function bindInstance(string|NamedClassKey|QualifiedClassKey $key, object $instance): self
{
$key = self::keyToString($key);
$this->validateBindingKey($key);
@@ -110,10 +118,11 @@ class Binder
* Bind an interface to a factory.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param class-string<Factory<T>> $factoryClassName A factory class name.
*/
- public function bindFactory(string|NamedClassKey $key, string $factoryClassName): self
+ public function bindFactory(string|NamedClassKey|QualifiedClassKey $key, string $factoryClassName): self
{
$key = self::keyToString($key);
$this->validateBindingKey($key);
@@ -149,9 +158,9 @@ class Binder
}
/**
- * @param string|NamedClassKey<object> $key
+ * @param string|NamedClassKey<object>|QualifiedClassKey<object> $key
*/
- private static function keyToString(string|NamedClassKey $key): string
+ private static function keyToString(string|NamedClassKey|QualifiedClassKey $key): string
{
return is_string($key) ? $key : $key->toString();
}
@@ -165,5 +174,9 @@ class Binder
if ($key[0] === '$') {
throw new LogicException("Can't binding a parameter name w/o an interface globally.");
}
+
+ if ($key[0] === '#') {
+ throw new LogicException("Can't binding a qualification name w/o an interface globally.");
+ }
}
}
diff --git a/application/Espo/Core/Binding/BindingContainer.php b/application/Espo/Core/Binding/BindingContainer.php
index 96af818b2c..f8be6bdf46 100644
--- a/application/Espo/Core/Binding/BindingContainer.php
+++ b/application/Espo/Core/Binding/BindingContainer.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Binding;
+use Espo\Core\Binding\Attributes\Qualify;
use ReflectionClass;
use ReflectionParameter;
use ReflectionNamedType;
@@ -117,6 +118,18 @@ class BindingContainer
return null;
}
+ $qualifiedName = $this->getQualifiedName($param);
+
+ if ($qualifiedName) {
+ $keyQualified = $paramClassName . ' #' . $qualifiedName;
+
+ $binding = $this->getInternalByClassNameKey($class?->getName(), $keyQualified);
+
+ if ($binding) {
+ return $binding;
+ }
+ }
+
$keyWithName = $paramClassName . ' $' . $param->getName();
$binding = $this->getInternalByClassNameKey($class?->getName(), $keyWithName);
@@ -136,6 +149,19 @@ class BindingContainer
return null;
}
+ private function getQualifiedName(ReflectionParameter $param): ?string
+ {
+ $qualifierClass = $param->getAttributes(Qualify::class)[0] ?? null;
+
+ if (!$qualifierClass) {
+ return null;
+ }
+
+ $qualifier = $qualifierClass->newInstance();
+
+ return $qualifier->qualifier;
+ }
+
/**
* @param ReflectionClass<object> $class
*/
diff --git a/application/Espo/Core/Binding/BindingContainerBuilder.php b/application/Espo/Core/Binding/BindingContainerBuilder.php
index b99b3c389a..a79c5a29ed 100644
--- a/application/Espo/Core/Binding/BindingContainerBuilder.php
+++ b/application/Espo/Core/Binding/BindingContainerBuilder.php
@@ -31,6 +31,7 @@ namespace Espo\Core\Binding;
use Closure;
use Espo\Core\Binding\Key\NamedClassKey;
+use Espo\Core\Binding\Key\QualifiedClassKey;
class BindingContainerBuilder
{
@@ -47,10 +48,11 @@ class BindingContainerBuilder
* Bind an interface to an implementation.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param class-string<T> $implementationClassName An implementation class name.
*/
- public function bindImplementation(string|NamedClassKey $key, string $implementationClassName): self
+ public function bindImplementation(string|NamedClassKey|QualifiedClassKey $key, string $implementationClassName): self
{
$this->binder->bindImplementation($key, $implementationClassName);
@@ -60,11 +62,12 @@ class BindingContainerBuilder
/**
* Bind an interface to a specific service.
*
- * @param class-string<object>|NamedClassKey<object> $key An interface or interface with a parameter name.
+ * @param class-string<object>|NamedClassKey<object>|QualifiedClassKey<object> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param string $serviceName A service name.
* @noinspection PhpUnused
*/
- public function bindService(string|NamedClassKey $key, string $serviceName): self
+ public function bindService(string|NamedClassKey|QualifiedClassKey $key, string $serviceName): self
{
$this->binder->bindService($key, $serviceName);
@@ -75,12 +78,13 @@ class BindingContainerBuilder
* Bind an interface to a callback.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param Closure $callback A callback that will resolve a dependency.
* @todo Change to Closure(...): T Once https://github.com/phpstan/phpstan/issues/8214 is implemented.
* @noinspection PhpUnused
*/
- public function bindCallback(string|NamedClassKey $key, Closure $callback): self
+ public function bindCallback(string|NamedClassKey|QualifiedClassKey $key, Closure $callback): self
{
$this->binder->bindCallback($key, $callback);
@@ -91,11 +95,11 @@ class BindingContainerBuilder
* Bind an interface to a specific instance.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param T $instance An instance.
- * @noinspection PhpDocSignatureInspection
*/
- public function bindInstance(string|NamedClassKey $key, object $instance): self
+ public function bindInstance(string|NamedClassKey|QualifiedClassKey $key, object $instance): self
{
$this->binder->bindInstance($key, $instance);
@@ -106,11 +110,12 @@ class BindingContainerBuilder
* Bind an interface to a factory.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param class-string<Factory<T>> $factoryClassName A factory class name.
* @noinspection PhpUnused
*/
- public function bindFactory(string|NamedClassKey $key, string $factoryClassName): self
+ public function bindFactory(string|NamedClassKey|QualifiedClassKey $key, string $factoryClassName): self
{
$this->binder->bindFactory($key, $factoryClassName);
diff --git a/application/Espo/Core/Binding/ContextualBinder.php b/application/Espo/Core/Binding/ContextualBinder.php
index de46cb341d..e3cfd26fcf 100644
--- a/application/Espo/Core/Binding/ContextualBinder.php
+++ b/application/Espo/Core/Binding/ContextualBinder.php
@@ -32,35 +32,34 @@ namespace Espo\Core\Binding;
use Closure;
use Espo\Core\Binding\Key\NamedClassKey;
use Espo\Core\Binding\Key\NamedKey;
+use Espo\Core\Binding\Key\QualifiedClassKey;
use LogicException;
class ContextualBinder
{
- private BindingData $data;
-
- /** @var class-string<object> */
- private string $className;
-
/**
* @param class-string<object> $className
*/
- public function __construct(BindingData $data, string $className)
- {
- $this->data = $data;
- $this->className = $className;
- }
+ public function __construct(
+ private BindingData $data,
+ private string $className,
+ ) {}
/**
* Bind an interface to an implementation.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param class-string<T> $implementationClassName An implementation class name.
*/
- public function bindImplementation(string|NamedClassKey $key, string $implementationClassName): self
- {
+ public function bindImplementation(
+ string|NamedClassKey|QualifiedClassKey $key,
+ string $implementationClassName,
+ ): self {
+
$key = self::keyToString($key);
- $this->validateBindingKeyNoParameterName($key);
+ $this->validateBindingKeyNoName($key);
$binding = Binding::createFromImplementationClassName($implementationClassName);
@@ -73,13 +72,14 @@ class ContextualBinder
* Bind an interface to a specific service.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param string $serviceName A service name.
*/
- public function bindService(string|NamedClassKey $key, string $serviceName): self
+ public function bindService(string|NamedClassKey|QualifiedClassKey $key, string $serviceName): self
{
$key = self::keyToString($key);
- $this->validateBindingKeyNoParameterName($key);
+ $this->validateBindingKeyNoName($key);
$this->data->addContext($this->className, $key, Binding::createFromServiceName($serviceName));
@@ -89,7 +89,8 @@ class ContextualBinder
/**
* Bind an interface or parameter name to a specific value.
*
- * @param string|NamedKey|NamedClassKey<object> $key Parameter name (`$name`) or interface with a parameter name.
+ * @param string|NamedKey|NamedClassKey<object> $key
+ * A parameter name (`$name`) or an interface with a parameter name.
* @param mixed $value A value of any type.
*/
public function bindValue(string|NamedKey|NamedClassKey $key, $value): self
@@ -106,13 +107,14 @@ class ContextualBinder
* Bind an interface to a specific instance.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param T $instance An instance.
*/
- public function bindInstance(string|NamedClassKey $key, object $instance): self
+ public function bindInstance(string|NamedClassKey|QualifiedClassKey $key, object $instance): self
{
$key = self::keyToString($key);
- $this->validateBindingKeyNoParameterName($key);
+ $this->validateBindingKeyNoName($key);
$this->data->addContext($this->className, $key, Binding::createFromValue($instance));
@@ -122,11 +124,12 @@ class ContextualBinder
/**
* Bind an interface or parameter name to a callback.
*
- * @param class-string<object>|NamedClassKey<object>|NamedKey $key An interface, parameter name or both.
+ * @param class-string<object>|NamedClassKey<object>|NamedKey|QualifiedClassKey<object> $key
+ * An interface, parameter name or both.
* @param Closure $callback A callback that will resolve a dependency.
* @todo Change to Closure(...): mixed Once https://github.com/phpstan/phpstan/issues/8214 is implemented.
*/
- public function bindCallback(string|NamedClassKey|NamedKey $key, Closure $callback): self
+ public function bindCallback(string|NamedClassKey|NamedKey|QualifiedClassKey $key, Closure $callback): self
{
$key = self::keyToString($key);
$this->validateBinding($key);
@@ -140,13 +143,14 @@ class ContextualBinder
* Bind an interface to a factory.
*
* @template T of object
- * @param class-string<T>|NamedClassKey<T> $key An interface or interface with a parameter name.
+ * @param class-string<T>|NamedClassKey<T>|QualifiedClassKey<T> $key
+ * An interface, an interface with a parameter name or an interface with a qualifier.
* @param class-string<Factory<T>> $factoryClassName A factory class name.
*/
- public function bindFactory(string|NamedClassKey $key, string $factoryClassName): self
+ public function bindFactory(string|NamedClassKey|QualifiedClassKey $key, string $factoryClassName): self
{
$key = self::keyToString($key);
- $this->validateBindingKeyNoParameterName($key);
+ $this->validateBindingKeyNoName($key);
$this->data->addContext($this->className, $key, Binding::createFromFactoryClassName($factoryClassName));
@@ -160,13 +164,17 @@ class ContextualBinder
}
}
- private function validateBindingKeyNoParameterName(string $key): void
+ private function validateBindingKeyNoName(string $key): void
{
$this->validateBinding($key);
if ($key[0] === '$') {
throw new LogicException("Can't bind a parameter name w/o an interface.");
}
+
+ if ($key[0] === '#') {
+ throw new LogicException("Can't bind a qualification name w/o an interface.");
+ }
}
private function validateBindingKeyParameterName(string $key): void
@@ -179,9 +187,9 @@ class ContextualBinder
}
/**
- * @param string|NamedKey|NamedClassKey<object> $key
+ * @param string|NamedKey|NamedClassKey<object>|QualifiedClassKey<object> $key
*/
- private static function keyToString(string|NamedKey|NamedClassKey $key): string
+ private static function keyToString(string|NamedKey|NamedClassKey|QualifiedClassKey $key): string
{
return is_string($key) ? $key : $key->toString();
}
diff --git a/application/Espo/Core/Binding/Key/QualifiedClassKey.php b/application/Espo/Core/Binding/Key/QualifiedClassKey.php
new file mode 100644
index 0000000000..9790b7f444
--- /dev/null
+++ b/application/Espo/Core/Binding/Key/QualifiedClassKey.php
@@ -0,0 +1,69 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Binding\Key;
+
+/**
+ * A key for a class-type-hinted constructor parameter with a qualifier.
+ * Use it to bind to qualified constructor parameters. For example, one interface
+ * can be used with a qualifier and without it. In the latter case,
+ * the default binding it applied. While in the formed case, a special binding is applied.
+ * For example, a caching pool interface may be bound to two different services:
+ * core cache and application cache.
+ *
+ * @since 10.1.0
+ *
+ * @template-covariant T of object
+ */
+class QualifiedClassKey
+{
+ /**
+ * @param class-string<T> $className
+ */
+ private function __construct(private string $className, private string $qualifier)
+ {}
+
+ /**
+ * Create.
+ *
+ * @template TC of object
+ * @param class-string<TC> $className An interface.
+ * @param string $qualifier A qualification name.
+ * @return self<TC>
+ */
+ public static function create(string $className, string $qualifier): self
+ {
+ return new self($className, $qualifier);
+ }
+
+ public function toString(): string
+ {
+ return $this->className . ' #' . $this->qualifier;
+ }
+}
diff --git a/tests/integration/Espo/Core/Binding/BindingTest.php b/tests/integration/Espo/Core/Binding/BindingTest.php
index bc63443e4e..5bb2d80404 100644
--- a/tests/integration/Espo/Core/Binding/BindingTest.php
+++ b/tests/integration/Espo/Core/Binding/BindingTest.php
@@ -30,14 +30,17 @@
namespace tests\integration\Espo\Core\Binding;
use Espo\Core\Application\ApplicationParams;
+use Espo\Core\Binding\Binder;
use Espo\Core\Binding\Binding;
use Espo\Core\Binding\BindingData;
use Espo\Core\Binding\BindingLoader;
+use Espo\Core\Binding\Key\QualifiedClassKey;
use Espo\Core\Container\ContainerBuilder;
-
+use Espo\Core\InjectableFactory;
use tests\integration\Core\BaseTestCase;
use tests\integration\testClasses\Binding\SomeClass;
use tests\integration\testClasses\Binding\SomeClassRequiringService;
+use tests\integration\testClasses\Binding\SomeClassRequiringServiceAlt;
use tests\integration\testClasses\Binding\SomeClassRequiringValue;
use tests\integration\testClasses\Binding\SomeFactory;
use tests\integration\testClasses\Binding\SomeImplementation;
@@ -72,6 +75,8 @@ class BindingTest extends BaseTestCase
$injectableFactory = $container->get('injectableFactory');
+ $this->assertInstanceOf(InjectableFactory::class, $injectableFactory);
+
$obj = $injectableFactory->create(SomeClass::class);
$this->assertNotNull($obj);
@@ -108,6 +113,8 @@ class BindingTest extends BaseTestCase
$injectableFactory = $container->get('injectableFactory');
+ $this->assertInstanceOf(InjectableFactory::class, $injectableFactory);
+
$obj = $injectableFactory->create(SomeClass::class);
$this->assertNotNull($obj);
@@ -122,7 +129,7 @@ class BindingTest extends BaseTestCase
{
$bindingLoader = new class() implements BindingLoader
{
- public function load() : BindingData
+ public function load(): BindingData
{
$data = new BindingData();
@@ -146,6 +153,8 @@ class BindingTest extends BaseTestCase
$injectableFactory = $container->get('injectableFactory');
+ $this->assertInstanceOf(InjectableFactory::class, $injectableFactory);
+
$obj = $injectableFactory->create(SomeClass::class);
$this->assertNotNull($obj);
@@ -160,7 +169,7 @@ class BindingTest extends BaseTestCase
{
$bindingLoader = new class() implements BindingLoader
{
- public function load() : BindingData
+ public function load(): BindingData
{
$data = new BindingData();
@@ -185,6 +194,8 @@ class BindingTest extends BaseTestCase
$injectableFactory = $container->get('injectableFactory');
+ $this->assertInstanceOf(InjectableFactory::class, $injectableFactory);
+
$obj = $injectableFactory->create(SomeClassRequiringService::class);
$this->assertNotNull($obj);
@@ -199,7 +210,7 @@ class BindingTest extends BaseTestCase
{
$bindingLoader = new class() implements BindingLoader
{
- public function load() : BindingData
+ public function load(): BindingData
{
$data = new BindingData();
@@ -220,6 +231,8 @@ class BindingTest extends BaseTestCase
$injectableFactory = $container->get('injectableFactory');
+ $this->assertInstanceOf(InjectableFactory::class, $injectableFactory);
+
$obj = $injectableFactory->create(SomeClassRequiringValue::class);
$this->assertNotNull($obj);
@@ -229,4 +242,46 @@ class BindingTest extends BaseTestCase
$obj->getValue()
);
}
+
+ public function testQualifier(): void
+ {
+ $bindingLoader = new class() implements BindingLoader
+ {
+ public function load(): BindingData
+ {
+ $data = new BindingData();
+ $binder = new Binder($data);
+
+ $binder->bindService(SomeService::class, 'testService');
+ $binder->bindService(QualifiedClassKey::create(SomeService::class, 'alt'), 'testServiceAlt');
+
+ return $data;
+ }
+ };
+
+ $testService = new SomeService();
+ $testServiceAlt = new SomeService('alt');
+
+ $container = (new ContainerBuilder())
+ ->withBindingLoader($bindingLoader)
+ ->withParams(new ApplicationParams(noErrorHandler: true))
+ ->withServices([
+ 'testService' => $testService,
+ 'testServiceAlt' => $testServiceAlt,
+ ])
+ ->build();
+
+ $injectableFactory = $container->get('injectableFactory');
+
+ $this->assertInstanceOf(InjectableFactory::class, $injectableFactory);
+
+ $obj = $injectableFactory->create(SomeClassRequiringService::class);
+ $objAlt = $injectableFactory->create(SomeClassRequiringServiceAlt::class);
+
+ $this->assertInstanceOf(SomeService::class, $obj->getService());
+ $this->assertEquals('default', $obj->getService()->name);
+
+ $this->assertInstanceOf(SomeService::class, $objAlt->getService());
+ $this->assertEquals('alt', $objAlt->getService()->name);
+ }
}
diff --git a/tests/integration/testClasses/Binding/SomeClass.php b/tests/integration/testClasses/Binding/SomeClass.php
index 7b46554058..16644db0f2 100644
--- a/tests/integration/testClasses/Binding/SomeClass.php
+++ b/tests/integration/testClasses/Binding/SomeClass.php
@@ -31,7 +31,7 @@ namespace tests\integration\testClasses\Binding;
class SomeClass
{
- private $someImplementation;
+ private SomeInterface $someImplementation;
public function __construct(SomeInterface $someImplementation)
{
diff --git a/tests/integration/testClasses/Binding/SomeClassRequiringService.php b/tests/integration/testClasses/Binding/SomeClassRequiringService.php
index 48eb53a026..e6ad6c0c7b 100644
--- a/tests/integration/testClasses/Binding/SomeClassRequiringService.php
+++ b/tests/integration/testClasses/Binding/SomeClassRequiringService.php
@@ -31,14 +31,11 @@ namespace tests\integration\testClasses\Binding;
class SomeClassRequiringService
{
- private $service;
+ public function __construct(
+ private SomeService $service
+ ) {}
- public function __construct(SomeService $service)
- {
- $this->service = $service;
- }
-
- public function getService() : SomeService
+ public function getService(): SomeService
{
return $this->service;
}
diff --git a/tests/integration/testClasses/Binding/SomeClassRequiringServiceAlt.php b/tests/integration/testClasses/Binding/SomeClassRequiringServiceAlt.php
new file mode 100644
index 0000000000..75f4ca80d8
--- /dev/null
+++ b/tests/integration/testClasses/Binding/SomeClassRequiringServiceAlt.php
@@ -0,0 +1,46 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\integration\testClasses\Binding;
+
+use Espo\Core\Binding\Attributes\Qualify;
+
+class SomeClassRequiringServiceAlt
+{
+ public function __construct(
+ #[Qualify('alt')]
+ private SomeService $service,
+ ) {}
+
+ public function getService(): SomeService
+ {
+ return $this->service;
+ }
+}
+
diff --git a/tests/integration/testClasses/Binding/SomeService.php b/tests/integration/testClasses/Binding/SomeService.php
index 9d82e563bd..c7cb7d7c1d 100644
--- a/tests/integration/testClasses/Binding/SomeService.php
+++ b/tests/integration/testClasses/Binding/SomeService.php
@@ -29,7 +29,9 @@
namespace tests\integration\testClasses\Binding;
-class SomeService
+readonly class SomeService
{
-
+ public function __construct(
+ public ?string $name = 'default',
+ ) {}
}
diff --git a/tests/unit/Espo/Core/Binding/BindingContainerTest.php b/tests/unit/Espo/Core/Binding/BindingContainerTest.php
index 5c5b0dc1e3..82acc106eb 100644
--- a/tests/unit/Espo/Core/Binding/BindingContainerTest.php
+++ b/tests/unit/Espo/Core/Binding/BindingContainerTest.php
@@ -39,6 +39,7 @@ use Espo\Core\Binding\ContextualBinder;
use Espo\Core\Binding\Key\NamedClassKey;
use Espo\Core\Binding\Key\NamedKey;
+use Espo\Core\Binding\Key\QualifiedClassKey;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionParameter;
@@ -46,6 +47,8 @@ use ReflectionNamedType;
use tests\unit\testClasses\Core\Binding\Class0;
use tests\unit\testClasses\Core\Binding\Class1;
+use tests\unit\testClasses\Core\Binding\SomeClass1Alt;
+use tests\unit\testClasses\Core\Binding\SomeClass2Alt;
use tests\unit\testClasses\Core\Binding\SomeInterface1;
use tests\unit\testClasses\Core\Binding\SomeInterface2;
use tests\unit\testClasses\Core\Binding\SomeClass1;
@@ -53,9 +56,8 @@ use tests\unit\testClasses\Core\Binding\SomeClass2;
class BindingContainerTest extends TestCase
{
- /** @var Binder */
- private $binder;
- private $loader;
+ private ?Binder $binder = null;
+ private ?BindingLoader $loader = null;
protected function setUp(): void
{
@@ -71,7 +73,7 @@ class BindingContainerTest extends TestCase
->willReturn($data);
}
- protected function createClassMock(string $className) : ReflectionClass
+ private function createClassMock(string $className): ReflectionClass
{
$class = $this->createMock(ReflectionClass::class);
@@ -83,7 +85,7 @@ class BindingContainerTest extends TestCase
return $class;
}
- protected function createParamMock(string $name, ?string $className = null) : ReflectionParameter
+ private function createParamMock(string $name, ?string $className = null, ?string $qualifier = null): ReflectionParameter
{
$param = $this->createMock(ReflectionParameter::class);
@@ -125,6 +127,17 @@ class BindingContainerTest extends TestCase
->method('getClass')
->willReturn($class);
+ if ($qualifier) {
+ $attribute = $this->createMock(ReflectionClass::class);
+
+ $param
+ ->method('getAttributes')
+ ->willReturn([$attribute]);
+
+ $attribute->method('newInstance')
+ ->willReturn((object) ['qualifier' => $qualifier]);
+ }
+
return $param;
}
@@ -133,7 +146,7 @@ class BindingContainerTest extends TestCase
return new BindingContainer($this->loader->load());
}
- public function testHasTrue()
+ public function testHasTrue(): void
{
$this->binder->bindService('Espo\\Test', 'test');
@@ -146,7 +159,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasNoContextTrue()
+ public function testHasNoContextTrue(): void
{
$this->binder->bindService('Espo\\Test', 'test');
@@ -157,7 +170,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasFalse()
+ public function testHasFalse(): void
{
$this->binder->bindService('Espo\\Test', 'test');
@@ -170,7 +183,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasNoContextFalse()
+ public function testHasNoContextFalse(): void
{
$this->binder->bindService('Espo\\Test', 'test');
@@ -181,7 +194,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextTrue0()
+ public function testHasContextTrue0(): void
{
$this->binder
->inContext('Espo\\Context', function (ContextualBinder $binder): void {
@@ -197,7 +210,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextTrue1()
+ public function testHasContextTrue1(): void
{
$this->binder
->for('Espo\\Context')
@@ -212,7 +225,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextTrue2()
+ public function testHasContextTrue2(): void
{
$this->binder
->for('Espo\\Context')
@@ -227,7 +240,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextTrue3()
+ public function testHasContextTrue3(): void
{
$this->binder
->for('Espo\\Context')
@@ -242,7 +255,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextTrue4()
+ public function testHasContextTrue4(): void
{
$this->binder
->for('Espo\\Context')
@@ -256,7 +269,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextFalse1()
+ public function testHasContextFalse1(): void
{
$this->binder
->for('Espo\\Context')
@@ -270,7 +283,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextFalse2()
+ public function testHasContextFalse2(): void
{
$this->binder
->for('Espo\\Context')
@@ -285,7 +298,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testHasContextFalse3()
+ public function testHasContextFalse3(): void
{
$this->binder
->for('Espo\\Context')
@@ -326,7 +339,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('Espo\\TestFactory', $binding->getValue());
}
- public function testGetService()
+ public function testGetService(): void
{
$this->binder->bindService('Espo\\Test', 'test');
@@ -341,7 +354,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('test', $binding->getValue());
}
- public function testGetCallback()
+ public function testGetCallback(): void
{
$this->binder->bindCallback(
'Espo\\Test',
@@ -361,7 +374,7 @@ class BindingContainerTest extends TestCase
$this->assertIsCallable($binding->getValue());
}
- public function testBindInstance()
+ public function testBindInstance(): void
{
$className = 'Espo\\Core\\Application';
@@ -378,7 +391,7 @@ class BindingContainerTest extends TestCase
$this->assertSame($instance, $binding->getValue());
}
- public function testContextBindInstance()
+ public function testContextBindInstance(): void
{
$className = 'Espo\\Core\\Application';
@@ -399,7 +412,7 @@ class BindingContainerTest extends TestCase
$this->assertSame($instance, $binding->getValue());
}
- public function testContextGetCallback()
+ public function testContextGetCallback(): void
{
$this->binder
->for('Espo\\Context')
@@ -421,7 +434,7 @@ class BindingContainerTest extends TestCase
$this->assertIsCallable($binding->getValue());
}
- public function testRebindGlobal()
+ public function testRebindGlobal(): void
{
$this->binder->bindService('Espo\\Test', 'test');
@@ -438,7 +451,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('testHello', $binding->getValue());
}
- public function testBindInterfaceWithParamNameGlobal()
+ public function testBindInterfaceWithParamNameGlobal(): void
{
$this->binder->bindService('Espo\\Test $name', 'testName');
@@ -495,7 +508,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('Espo\\TestFactory', $binding->getValue());
}
- public function testNoContextClassName()
+ public function testNoContextClassName(): void
{
$this->binder
->for('Espo\\Context')
@@ -510,7 +523,7 @@ class BindingContainerTest extends TestCase
);
}
- public function testBindContextInterfaceWithParamNameGlobal()
+ public function testBindContextInterfaceWithParamNameGlobal(): void
{
$this->binder
->for('Espo\\Context')
@@ -537,7 +550,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('test', $binding->getValue());
}
- public function testGetContextParamValue()
+ public function testGetContextParamValue(): void
{
$this->binder
->for('Espo\\Context')
@@ -554,7 +567,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('Test Value', $binding->getValue());
}
- public function testGetContextInterfaceValue1()
+ public function testGetContextInterfaceValue1(): void
{
$instance = (object) [];
@@ -571,7 +584,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals($instance, $binding->getValue());
}
- public function testGetContextInterfaceValue2()
+ public function testGetContextInterfaceValue2(): void
{
$instance = (object) [];
@@ -588,7 +601,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals($instance, $binding->getValue());
}
- public function testGetContextService()
+ public function testGetContextService(): void
{
$this->binder
->for('Espo\\Context')
@@ -605,7 +618,7 @@ class BindingContainerTest extends TestCase
$this->assertEquals('test', $binding->getValue());
}
- public function testRebindContextService()
+ public function testRebindContextService(): void
{
$this->binder
->for('Espo\\Context')
@@ -667,7 +680,7 @@ class BindingContainerTest extends TestCase
{
$container = BindingContainerBuilder::create()
->inContext(Class0::class, function (ContextualBinder $binder): void {
- $binder->bindValue('$dep', new Class1());
+ $binder->bindValue(NamedKey::create('dep'), new Class1());
})
->build();
@@ -682,4 +695,54 @@ class BindingContainerTest extends TestCase
$this->assertEquals(Binding::VALUE, $binding->getType());
$this->assertInstanceOf(Class1::class, $binding->getValue());
}
+
+ public function testBindingQualified(): void
+ {
+ $container = BindingContainerBuilder::create()
+ ->bindImplementation(SomeInterface1::class, SomeClass1::class)
+ ->bindImplementation(QualifiedClassKey::create(SomeInterface1::class, 'q1'), SomeClass1Alt::class)
+ ->bindImplementation(QualifiedClassKey::create(SomeInterface2::class, 'q3'), SomeClass2Alt::class)
+ ->inContext(SomeClass1::class, function (ContextualBinder $binder): void {
+ $binder->bindImplementation(SomeInterface2::class, SomeClass2::class);
+ $binder->bindImplementation(QualifiedClassKey::create(SomeInterface2::class, 'q2'), SomeClass2Alt::class);
+ })
+ ->build();
+
+ $param1 = $this->createParamMock('test', SomeInterface1::class, 'q1');
+ $this->assertTrue($container->hasByParam(null, $param1));
+ $binding = $container->getByParam(null, $param1);
+ $this->assertEquals(SomeClass1Alt::class, $binding->getValue());
+
+ //
+
+ $param2 = $this->createParamMock('test', SomeInterface1::class);
+ $this->assertTrue($container->hasByParam(null, $param2));
+ $binding = $container->getByParam(null, $param2);
+ $this->assertEquals(SomeClass1::class, $binding->getValue());
+
+ //
+
+ $contextClass = $this->createClassMock(SomeClass1::class);
+
+ //
+
+ $param3 = $this->createParamMock('test', SomeInterface2::class, 'q2');
+ $this->assertTrue($container->hasByParam($contextClass, $param3));
+ $binding = $container->getByParam($contextClass, $param3);
+ $this->assertEquals(SomeClass2Alt::class, $binding->getValue());
+
+ //
+
+ $param4 = $this->createParamMock('test', SomeInterface2::class);
+ $this->assertTrue($container->hasByParam($contextClass, $param4));
+ $binding = $container->getByParam($contextClass, $param4);
+ $this->assertEquals(SomeClass2::class, $binding->getValue());
+
+ //
+
+ $param3 = $this->createParamMock('test', SomeInterface2::class, 'q3');
+ $this->assertTrue($container->hasByParam($contextClass, $param3));
+ $binding = $container->getByParam($contextClass, $param3);
+ $this->assertEquals(SomeClass2Alt::class, $binding->getValue());
+ }
}
diff --git a/tests/unit/testClasses/Core/Binding/SomeClass0.php b/tests/unit/testClasses/Core/Binding/SomeClass0.php
index 30248cac9d..98f333e7a9 100644
--- a/tests/unit/testClasses/Core/Binding/SomeClass0.php
+++ b/tests/unit/testClasses/Core/Binding/SomeClass0.php
@@ -31,10 +31,6 @@ namespace tests\unit\testClasses\Core\Binding;
class SomeClass0
{
- private $dep;
-
- public function __construct(SomeInterface1 $dep)
- {
- $this->dep = $dep;
- }
+ public function __construct(public SomeInterface1 $dep)
+ {}
}
diff --git a/tests/unit/testClasses/Core/Binding/SomeClass1.php b/tests/unit/testClasses/Core/Binding/SomeClass1.php
index e4dfeef428..4e5759ba9a 100644
--- a/tests/unit/testClasses/Core/Binding/SomeClass1.php
+++ b/tests/unit/testClasses/Core/Binding/SomeClass1.php
@@ -31,10 +31,6 @@ namespace tests\unit\testClasses\Core\Binding;
class SomeClass1 implements SomeInterface1
{
- private $dep;
-
- public function __construct(SomeInterface2 $dep)
- {
- $this->dep = $dep;
- }
+ public function __construct(public SomeInterface2 $dep)
+ {}
}
diff --git a/tests/unit/testClasses/Core/Binding/SomeClass1Alt.php b/tests/unit/testClasses/Core/Binding/SomeClass1Alt.php
new file mode 100644
index 0000000000..807a7bfbdf
--- /dev/null
+++ b/tests/unit/testClasses/Core/Binding/SomeClass1Alt.php
@@ -0,0 +1,36 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\testClasses\Core\Binding;
+
+class SomeClass1Alt implements SomeInterface1
+{
+ public function __construct(public SomeInterface2 $dep)
+ {}
+}
diff --git a/tests/unit/testClasses/Core/Binding/SomeClass2.php b/tests/unit/testClasses/Core/Binding/SomeClass2.php
index 171dc352f3..fb6d33dbf5 100644
--- a/tests/unit/testClasses/Core/Binding/SomeClass2.php
+++ b/tests/unit/testClasses/Core/Binding/SomeClass2.php
@@ -30,6 +30,4 @@
namespace tests\unit\testClasses\Core\Binding;
class SomeClass2 implements SomeInterface2
-{
-
-}
+{}
diff --git a/tests/unit/testClasses/Core/Binding/SomeClass2Alt.php b/tests/unit/testClasses/Core/Binding/SomeClass2Alt.php
new file mode 100644
index 0000000000..2f2e051a4d
--- /dev/null
+++ b/tests/unit/testClasses/Core/Binding/SomeClass2Alt.php
@@ -0,0 +1,33 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace tests\unit\testClasses\Core\Binding;
+
+class SomeClass2Alt implements SomeInterface2
+{}
diff --git a/tests/unit/testClasses/Core/Binding/SomeInterface2.php b/tests/unit/testClasses/Core/Binding/SomeInterface2.php
index 9bf19f67eb..1eec5045f7 100644
--- a/tests/unit/testClasses/Core/Binding/SomeInterface2.php
+++ b/tests/unit/testClasses/Core/Binding/SomeInterface2.php
@@ -30,6 +30,4 @@
namespace tests\unit\testClasses\Core\Binding;
interface SomeInterface2
-{
-
-}
+{}