選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractService.php 2.8KB

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