|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
-
- namespace common\logic;
-
- use yii\base\ErrorException;
-
- abstract class AbstractService extends AbstractSingleton implements ServiceInterface
- {
- use ProducerContextTrait;
-
- protected function getHierarchy(): array
- {
- return [
- DefinitionInterface::class,
- SolverInterface::class,
- RepositoryQueryInterface::class,
- RepositoryInterface::class,
- BuilderInterface::class,
- ResolverInterface::class,
- UtilsInterface::class,
- ];
- }
-
- protected function loadService(string $serviceClass)
- {
- if(!$this->respectHierarchy($serviceClass)) {
- throw new ErrorException('Le service '.$serviceClass.' ne peut pas être chargé ici.');
- }
-
- return $serviceClass::getInstance();
- }
-
- protected function loadDependencies(): void
- {
- }
-
- protected function respectHierarchy(string $serviceClassAsked): bool
- {
- $domain = \Yii::$app->logic;
- $serviceCurrentClass = get_class($this);
- $levelHierarchyServiceAsked = $this->getServiceLevelHierarchy($serviceClassAsked);
- $levelHierarchyServiceCurrent = $this->getServiceLevelHierarchy($serviceCurrentClass);
-
- if($levelHierarchyServiceAsked < $levelHierarchyServiceCurrent) {
- return true;
- }
- elseif($levelHierarchyServiceAsked == $levelHierarchyServiceCurrent
- && $domain->getContainerLevelHierarchyByService($serviceClassAsked)
- < $domain->getContainerLevelHierarchyByService($serviceCurrentClass)) {
-
- return true;
- }
-
- return false;
- }
-
- protected function getServiceLevelHierarchy(string $serviceClass): int
- {
- $hierarchyArray = $this->getHierarchy();
-
- foreach($hierarchyArray as $key => $interface) {
- if($this->classImplementsInterface($interface, $serviceClass)) {
- return $key;
- }
- }
-
- throw new ErrorException('Service introuvable dans la hiérarchie. Attention à bien ajouter
- FactoryInterface, SolverInterface ou BuilderInterface au service.');
- }
-
- protected function isSolver(): bool
- {
- return $this->classImplementsInterface(SolverInterface::class);
- }
-
- protected function isBuilder(): bool
- {
- return $this->classImplementsInterface(BuilderInterface::class);
- }
-
- protected function isResolver(): bool
- {
- return $this->classImplementsInterface(ResolverInterface::class);
- }
-
- protected function isUtils(): bool
- {
- return $this->classImplementsInterface(UtilsInterface::class);
- }
-
- protected function classImplementsInterface(string $interface, $object = null)
- {
- if(is_null($object)) {
- $object = $this;
- }
-
- return in_array($interface, class_implements($object));
- }
- }
|