You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8KB

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