Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

98 lines
2.8KB

  1. <?php
  2. namespace common\logic;
  3. use yii\base\ErrorException;
  4. abstract class AbstractService extends AbstractSingleton implements ServiceInterface
  5. {
  6. use ProducerContextTrait;
  7. protected function getHierarchy(): array
  8. {
  9. return [
  10. SolverInterface::class,
  11. RepositoryQueryInterface::class,
  12. RepositoryInterface::class,
  13. BuilderInterface::class,
  14. ResolverInterface::class,
  15. UtilsInterface::class,
  16. ];
  17. }
  18. protected function loadService(string $serviceClass)
  19. {
  20. if(!$this->respectHierarchy($serviceClass)) {
  21. throw new ErrorException('Le service '.$serviceClass.' ne peut pas être chargé ici.');
  22. }
  23. return $serviceClass::getInstance();
  24. }
  25. protected function loadDependencies(): void
  26. {
  27. }
  28. protected function respectHierarchy(string $serviceClassAsked): bool
  29. {
  30. $domain = \Yii::$app->logic;
  31. $serviceCurrentClass = get_class($this);
  32. $levelHierarchyServiceAsked = $this->getServiceLevelHierarchy($serviceClassAsked);
  33. $levelHierarchyServiceCurrent = $this->getServiceLevelHierarchy($serviceCurrentClass);
  34. if($levelHierarchyServiceAsked < $levelHierarchyServiceCurrent) {
  35. return true;
  36. }
  37. elseif($levelHierarchyServiceAsked == $levelHierarchyServiceCurrent
  38. && $domain->getContainerLevelHierarchyByService($serviceClassAsked)
  39. < $domain->getContainerLevelHierarchyByService($serviceCurrentClass)) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. protected function getServiceLevelHierarchy(string $serviceClass): int
  45. {
  46. $hierarchyArray = $this->getHierarchy();
  47. foreach($hierarchyArray as $key => $interface) {
  48. if($this->classImplementsInterface($interface, $serviceClass)) {
  49. return $key;
  50. }
  51. }
  52. throw new ErrorException('Service introuvable dans la hiérarchie. Attention à bien ajouter
  53. FactoryInterface, SolverInterface ou BuilderInterface au service.');
  54. }
  55. protected function isSolver(): bool
  56. {
  57. return $this->classImplementsInterface(SolverInterface::class);
  58. }
  59. protected function isBuilder(): bool
  60. {
  61. return $this->classImplementsInterface(BuilderInterface::class);
  62. }
  63. protected function isResolver(): bool
  64. {
  65. return $this->classImplementsInterface(ResolverInterface::class);
  66. }
  67. protected function isUtils(): bool
  68. {
  69. return $this->classImplementsInterface(UtilsInterface::class);
  70. }
  71. protected function classImplementsInterface(string $interface, $object = null)
  72. {
  73. if(is_null($object)) {
  74. $object = $this;
  75. }
  76. return in_array($interface, class_implements($object));
  77. }
  78. }