Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractService.php 2.4KB

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