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

@@ -999,14 +999,12 @@ Call $this->AddInitialAttributeFlags($sAttCode, $iFlags) for all the initial att
</node> </node>
<node id="values"> <node id="values">
<label>UI:DashletHeaderDynamic:Prop-Values</label> <label>UI:DashletHeaderDynamic:Prop-Values</label>
<value-type xsi:type="Combodo-ValueType-CollectionOfValues"> <value-type xsi:type="Combodo-ValueType-ClassAttributeValue">
<data-transform> <data-transform>
<xml-format xsi:type="xml-format-csv"/> <xml-format xsi:type="xml-format-csv"/>
</data-transform> </data-transform>
<value-type xsi:type="Combodo-ValueType-ClassAttributeValue">
<class>{{query.selected_class}}</class> <class>{{query.selected_class}}</class>
<attribute>{{group_by.value}}</attribute> <attribute>{{group_by.attribute}}</attribute>
</value-type>
</value-type> </value-type>
</node> </node>
</nodes> </nodes>

View File

@@ -598,7 +598,7 @@ return array(
'Combodo\\iTop\\Service\\Base\\iDataPostProcessor' => $baseDir . '/sources/Service/Base/iDataPostProcessor.php', 'Combodo\\iTop\\Service\\Base\\iDataPostProcessor' => $baseDir . '/sources/Service/Base/iDataPostProcessor.php',
'Combodo\\iTop\\Service\\Cache\\DataModelDependantCache' => $baseDir . '/sources/Service/Cache/DataModelDependantCache.php', 'Combodo\\iTop\\Service\\Cache\\DataModelDependantCache' => $baseDir . '/sources/Service/Cache/DataModelDependantCache.php',
'Combodo\\iTop\\Service\\DependencyInjection\\DIException' => $baseDir . '/sources/Service/DependencyInjection/DIException.php', 'Combodo\\iTop\\Service\\DependencyInjection\\DIException' => $baseDir . '/sources/Service/DependencyInjection/DIException.php',
'Combodo\\iTop\\Service\\DependencyInjection\\DIService' => $baseDir . '/sources/Service/DependencyInjection/DIService.php', 'Combodo\\iTop\\Service\\DependencyInjection\\ServiceLocator' => $baseDir . '/sources/Service/DependencyInjection/ServiceLocator.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDataDescription' => $baseDir . '/sources/Service/Events/Description/EventDataDescription.php', 'Combodo\\iTop\\Service\\Events\\Description\\EventDataDescription' => $baseDir . '/sources/Service/Events/Description/EventDataDescription.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDescription' => $baseDir . '/sources/Service/Events/Description/EventDescription.php', 'Combodo\\iTop\\Service\\Events\\Description\\EventDescription' => $baseDir . '/sources/Service/Events/Description/EventDescription.php',
'Combodo\\iTop\\Service\\Events\\EventData' => $baseDir . '/sources/Service/Events/EventData.php', 'Combodo\\iTop\\Service\\Events\\EventData' => $baseDir . '/sources/Service/Events/EventData.php',
@@ -3932,5 +3932,5 @@ return array(
'privUITransactionFile' => $baseDir . '/application/transaction.class.inc.php', 'privUITransactionFile' => $baseDir . '/application/transaction.class.inc.php',
'privUITransactionSession' => $baseDir . '/application/transaction.class.inc.php', 'privUITransactionSession' => $baseDir . '/application/transaction.class.inc.php',
'utils' => $baseDir . '/application/utils.inc.php', 'utils' => $baseDir . '/application/utils.inc.php',
'©' => $vendorDir . '/symfony/cache/Traits/ValueWrapper.php', '<EFBFBD>' => $vendorDir . '/symfony/cache/Traits/ValueWrapper.php',
); );

View File

@@ -984,7 +984,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'Combodo\\iTop\\Service\\Base\\iDataPostProcessor' => __DIR__ . '/../..' . '/sources/Service/Base/iDataPostProcessor.php', 'Combodo\\iTop\\Service\\Base\\iDataPostProcessor' => __DIR__ . '/../..' . '/sources/Service/Base/iDataPostProcessor.php',
'Combodo\\iTop\\Service\\Cache\\DataModelDependantCache' => __DIR__ . '/../..' . '/sources/Service/Cache/DataModelDependantCache.php', 'Combodo\\iTop\\Service\\Cache\\DataModelDependantCache' => __DIR__ . '/../..' . '/sources/Service/Cache/DataModelDependantCache.php',
'Combodo\\iTop\\Service\\DependencyInjection\\DIException' => __DIR__ . '/../..' . '/sources/Service/DependencyInjection/DIException.php', 'Combodo\\iTop\\Service\\DependencyInjection\\DIException' => __DIR__ . '/../..' . '/sources/Service/DependencyInjection/DIException.php',
'Combodo\\iTop\\Service\\DependencyInjection\\DIService' => __DIR__ . '/../..' . '/sources/Service/DependencyInjection/DIService.php', 'Combodo\\iTop\\Service\\DependencyInjection\\ServiceLocator' => __DIR__ . '/../..' . '/sources/Service/DependencyInjection/ServiceLocator.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDataDescription' => __DIR__ . '/../..' . '/sources/Service/Events/Description/EventDataDescription.php', 'Combodo\\iTop\\Service\\Events\\Description\\EventDataDescription' => __DIR__ . '/../..' . '/sources/Service/Events/Description/EventDataDescription.php',
'Combodo\\iTop\\Service\\Events\\Description\\EventDescription' => __DIR__ . '/../..' . '/sources/Service/Events/Description/EventDescription.php', 'Combodo\\iTop\\Service\\Events\\Description\\EventDescription' => __DIR__ . '/../..' . '/sources/Service/Events/Description/EventDescription.php',
'Combodo\\iTop\\Service\\Events\\EventData' => __DIR__ . '/../..' . '/sources/Service/Events/EventData.php', 'Combodo\\iTop\\Service\\Events\\EventData' => __DIR__ . '/../..' . '/sources/Service/Events/EventData.php',
@@ -4318,7 +4318,7 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
'privUITransactionFile' => __DIR__ . '/../..' . '/application/transaction.class.inc.php', 'privUITransactionFile' => __DIR__ . '/../..' . '/application/transaction.class.inc.php',
'privUITransactionSession' => __DIR__ . '/../..' . '/application/transaction.class.inc.php', 'privUITransactionSession' => __DIR__ . '/../..' . '/application/transaction.class.inc.php',
'utils' => __DIR__ . '/../..' . '/application/utils.inc.php', 'utils' => __DIR__ . '/../..' . '/application/utils.inc.php',
'©' => __DIR__ . '/..' . '/symfony/cache/Traits/ValueWrapper.php', '<EFBFBD>' => __DIR__ . '/..' . '/symfony/cache/Traits/ValueWrapper.php',
); );
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)

View File

@@ -16,7 +16,7 @@ use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Forms\Register\OptionsRegister; use Combodo\iTop\Forms\Register\OptionsRegister;
use Combodo\iTop\Forms\Register\RegisterException; use Combodo\iTop\Forms\Register\RegisterException;
use Combodo\iTop\Service\DependencyInjection\DIException; use Combodo\iTop\Service\DependencyInjection\DIException;
use Combodo\iTop\Service\DependencyInjection\DIService; use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use utils; use utils;
/** /**
@@ -93,7 +93,7 @@ class AttributeChoiceFormBlock extends ChoiceFormBlock
*/ */
public static function ListAttributeCodesByCategory(string $sClass, string $sCategory = ''): array public static function ListAttributeCodesByCategory(string $sClass, string $sCategory = ''): array
{ {
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection'); $oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aAttributeCodes = []; $aAttributeCodes = [];
switch ($sCategory) { 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\IO\Format\ClassIOFormat;
use Combodo\iTop\Forms\Register\IORegister; use Combodo\iTop\Forms\Register\IORegister;
use Combodo\iTop\Forms\Register\OptionsRegister; use Combodo\iTop\Forms\Register\OptionsRegister;
use Combodo\iTop\Service\DependencyInjection\DIService; use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Exception; use Exception;
/** /**
@@ -59,7 +59,7 @@ class AttributeValueChoiceFormBlock extends ChoiceFormBlock
try { try {
/** @var \ModelReflection $oModelReflection */ /** @var \ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection'); $oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aValues = $oModelReflection->GetAllowedValues_att($sClass, $sAttCode); $aValues = $oModelReflection->GetAllowedValues_att($sClass, $sAttCode);
$oOptionsRegister->SetOption('choices', array_flip($aValues ?? [])); $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\DataModel\AttributeChoiceFormBlock;
use Combodo\iTop\Forms\Block\FormBlockException; use Combodo\iTop\Forms\Block\FormBlockException;
use Combodo\iTop\Forms\Register\OptionsRegister; use Combodo\iTop\Forms\Register\OptionsRegister;
use Combodo\iTop\Service\DependencyInjection\DIService; use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Dict; use Dict;
use Exception; use Exception;
@@ -32,7 +32,7 @@ class ClassAttributeGroupByFormBlock extends AttributeChoiceFormBlock
public function UpdateOptions(OptionsRegister $oOptionsRegister): void public function UpdateOptions(OptionsRegister $oOptionsRegister): void
{ {
parent::UpdateOptions($oOptionsRegister); parent::UpdateOptions($oOptionsRegister);
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection'); $oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aGroupBy = []; $aGroupBy = [];
try { try {

View File

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

View File

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

View File

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

View File

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

View File

@@ -8,7 +8,7 @@
namespace Combodo\iTop\Forms\Validator; namespace Combodo\iTop\Forms\Validator;
use Combodo\iTop\Forms\IO\Converter\OqlToClassConverter; 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\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraint;
@@ -42,7 +42,7 @@ class AttributeExistValidator extends ConstraintValidator
$sClass = "UserRequest"; $sClass = "UserRequest";
/** List attributes @var ModelReflection $oModelReflection */ /** List attributes @var ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection'); $oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$aAttributeCodes = array_keys($oModelReflection->ListAttributes($sClass)); $aAttributeCodes = array_keys($oModelReflection->ListAttributes($sClass));
if (!in_array($value, $aAttributeCodes, true)) { 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\Forms\Block\Base\TextFormBlock;
use Combodo\iTop\PropertyTree\AbstractProperty; use Combodo\iTop\PropertyTree\AbstractProperty;
use Combodo\iTop\PropertyTree\ValueType\AbstractValueType; use Combodo\iTop\PropertyTree\ValueType\AbstractValueType;
use Combodo\iTop\Service\DependencyInjection\DIService; use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use utils; use utils;
class ValueTypeClass extends AbstractValueType class ValueTypeClass extends AbstractValueType
@@ -30,7 +30,7 @@ class ValueTypeClass extends AbstractValueType
$sCategories = $oDomNode->GetChildText('categories-csv'); $sCategories = $oDomNode->GetChildText('categories-csv');
/** @var \ModelReflection $oModelReflection */ /** @var \ModelReflection $oModelReflection */
$oModelReflection = DIService::GetInstance()->GetService('ModelReflection'); $oModelReflection = ServiceLocator::GetInstance()->get('ModelReflection');
$sChoices = "[\n"; $sChoices = "[\n";
$aClasses = $oModelReflection->GetClasses($sCategories, true); $aClasses = $oModelReflection->GetClasses($sCategories, true);

View File

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

View File

@@ -6,7 +6,7 @@
*/ */
use Combodo\iTop\Forms\Compiler\FormsCompiler; use Combodo\iTop\Forms\Compiler\FormsCompiler;
use Combodo\iTop\Service\DependencyInjection\DIService; use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase; use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
class FormsCompilerTest extends ItopDataTestCase class FormsCompilerTest extends ItopDataTestCase
@@ -24,7 +24,7 @@ class FormsCompilerTest extends ItopDataTestCase
*/ */
public function testCompileFormFromXML(string $sXMLContent, string $sExpectedPHP) public function testCompileFormFromXML(string $sXMLContent, string $sExpectedPHP)
{ {
DIService::GetInstance()->RegisterService('ModelReflection', new ModelReflectionRuntime()); ServiceLocator::GetInstance()->RegisterService('ModelReflection', new ModelReflectionRuntime());
$sProducedPHP = FormsCompiler::GetInstance()->CompileFormFromXML($sXMLContent); $sProducedPHP = FormsCompiler::GetInstance()->CompileFormFromXML($sXMLContent);