No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

AbstractService.php 2.9KB

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