N°8772 - Move DIService into PSR-11 Service Locator

This commit is contained in:
Eric Espie
2025-12-30 14:26:43 +01:00
parent 4c1ad0f4f2
commit 904cd0b518
14 changed files with 41 additions and 45 deletions

View File

@@ -16,7 +16,7 @@ use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Forms\Register\OptionsRegister;
use Combodo\iTop\Forms\Register\RegisterException;
use Combodo\iTop\Service\DependencyInjection\DIException;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use utils;
/**
@@ -93,7 +93,7 @@ class AttributeChoiceFormBlock extends ChoiceFormBlock
*/
public static function ListAttributeCodesByCategory(string $sClass, string $sCategory = ''): array
{
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aAttributeCodes = [];
switch ($sCategory) {

View File

@@ -13,7 +13,7 @@ use Combodo\iTop\Forms\IO\Format\AttributeIOFormat;
use Combodo\iTop\Forms\IO\Format\ClassIOFormat;
use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Forms\Register\OptionsRegister;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Exception;
/**
@@ -59,7 +59,7 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
try {
/** @var \ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aValues = $oModelReflection->GetAllowedValues_att($sClass, $sAttCode);
$oOptionsRegister->SetOption('choices', array_flip($aValues ?? []));

View File

@@ -10,7 +10,7 @@ namespace Combodo\iTop\Forms\Block\DataModel\Dashlet;
use Combodo\iTop\Forms\Block\DataModel\AttributeChoiceFormBlock;
use Combodo\iTop\Forms\Block\FormBlockException;
use Combodo\iTop\Forms\Register\OptionsRegister;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Dict;
use Exception;
@@ -32,7 +32,7 @@ class ClassAttributeGroupByFormBlock extends AttributeChoiceFormBlock
public function UpdateOptions(OptionsRegister $oOptionsRegister): void
{
parent::UpdateOptions($oOptionsRegister);
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aGroupBy = [];
try {

View File

@@ -10,7 +10,7 @@ namespace Combodo\iTop\Forms\Block;
use Combodo\iTop\Forms\Block\Base\FormBlock;
use Combodo\iTop\Forms\Compiler\FormsCompiler;
use Combodo\iTop\Service\Cache\DataModelDependantCache;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use ModelReflection;
use ModelReflectionRuntime;
use utils;
@@ -23,7 +23,7 @@ class FormBlockService
protected function __construct(ModelReflection $oModelReflection = null)
{
DIService::GetInstance()->RegisterService('ModelReflection', $oModelReflection ?? new ModelReflectionRuntime());
ServiceLocator::GetInstance()->RegisterService('ModelReflection', $oModelReflection ?? new ModelReflectionRuntime());
$this->oCacheService = DataModelDependantCache::GetInstance();
}

View File

@@ -8,7 +8,7 @@
namespace Combodo\iTop\Forms\IO\Converter;
use Combodo\iTop\Service\DependencyInjection\DIException;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Combodo\iTop\Forms\IO\Format\ClassIOFormat;
use Combodo\iTop\Forms\IO\FormBlockIOException;
use Exception;
@@ -33,7 +33,7 @@ class OqlToClassConverter extends AbstractConverter
}
/** @var ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
try {
$oQuery = $oModelReflection->GetQuery($oData);
} catch (Exception $e) {

View File

@@ -2,7 +2,7 @@
namespace Combodo\iTop\Forms\IO\Format;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Combodo\iTop\Forms\IO\FormBlockIOException;
/**

View File

@@ -2,7 +2,7 @@
namespace Combodo\iTop\Forms\IO\Format;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Combodo\iTop\Forms\IO\FormBlockIOException;
/**
@@ -23,7 +23,7 @@ class ClassIOFormat extends AbstractIOFormat
{
// Check class validity
/** @var \ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
if (!$oModelReflection->IsValidClass($sClassName)) {
throw new FormBlockIOException("Class ".json_encode($sClassName)." is not valid");
}

View File

@@ -8,7 +8,7 @@
namespace Combodo\iTop\Forms\Validator;
use Combodo\iTop\Forms\IO\Converter\OqlToClassConverter;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\Constraint;
@@ -42,7 +42,7 @@ class AttributeExistValidator extends ConstraintValidator
$sClass = "UserRequest";
/** List attributes @var ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aAttributeCodes = array_keys($oModelReflection->ListAttributes($sClass));
if (!in_array($value, $aAttributeCodes, true)) {

View File

@@ -12,7 +12,7 @@ use Combodo\iTop\Forms\Block\Base\ChoiceFormBlock;
use Combodo\iTop\Forms\Block\Base\TextFormBlock;
use Combodo\iTop\PropertyTree\AbstractProperty;
use Combodo\iTop\PropertyTree\ValueType\AbstractValueType;
use Combodo\iTop\Service\DependencyInjection\DIService;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use utils;
class ValueTypeClass extends AbstractValueType
@@ -30,7 +30,7 @@ class ValueTypeClass extends AbstractValueType
$sCategories = $oDomNode->GetChildText('categories-csv');
/** @var \ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection');
$oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$sChoices = "[\n";
$aClasses = $oModelReflection->GetClasses($sCategories, true);

View File

@@ -7,19 +7,21 @@
namespace Combodo\iTop\Service\DependencyInjection;
class DIService
use Psr\Container\ContainerInterface;
class ServiceLocator implements ContainerInterface
{
private static DIService $oInstance;
private static ServiceLocator $oInstance;
private array $aServices = [];
protected function __construct()
{
}
final public static function GetInstance(): DIService
final public static function GetInstance(): ServiceLocator
{
if (!isset(static::$oInstance)) {
static::$oInstance = new DIService();
static::$oInstance = new ServiceLocator();
}
return static::$oInstance;
@@ -28,8 +30,6 @@ class DIService
/**
* Register a service by name
*
* @api
*
* @param string $sName Name of the service to register
* @param mixed $oService Service to register
*
@@ -43,24 +43,22 @@ class DIService
/**
* Get a previously registered service
*
* @api
*
* @param string $sName name of the service to get
* @param bool $bMustBeFound if true a DIException is thrown when the service is not found
* @param string $id Service id to search for
*
* @return mixed The service or null when the service is not found and $bMustBeFound is false
* @throws \Combodo\iTop\Service\DependencyInjection\DIException
*/
final public function GetService(string $sName, bool $bMustBeFound = true): mixed
public function get(string $id): mixed
{
if (!isset($this->aServices[$sName])) {
if ($bMustBeFound) {
throw new DIException("Service ".json_encode($sName)." not found");
}
return null;
if (!isset($this->aServices[$id])) {
throw new DIException("Service ".json_encode($id)." not found");
}
return $this->aServices[$sName];
return $this->aServices[$id];
}
public function has(string $id): bool
{
return isset($this->aServices[$id]);
}
}