EntityManagerWrapper.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Drupal\webprofiler\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
  4. use Drupal\Core\Entity\EntityTypeInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Entity\EntityViewBuilderInterface;
  7. use Drupal\Core\Plugin\DefaultPluginManager;
  8. use Drupal\webprofiler\Entity\Decorators\Config\ConfigEntityStorageDecorator;
  9. use Drupal\webprofiler\Entity\Decorators\Config\ImageStyleStorageDecorator;
  10. use Drupal\webprofiler\Entity\Decorators\Config\RoleStorageDecorator;
  11. use Drupal\webprofiler\Entity\Decorators\Config\ShortcutSetStorageDecorator;
  12. use Drupal\webprofiler\Entity\Decorators\Config\VocabularyStorageDecorator;
  13. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. /**
  16. * Class EntityManagerWrapper
  17. */
  18. class EntityManagerWrapper extends DefaultPluginManager implements EntityTypeManagerInterface, ContainerAwareInterface {
  19. /**
  20. * @var array
  21. */
  22. private $loaded;
  23. /**
  24. * @var array
  25. */
  26. private $rendered;
  27. /**
  28. * @var \Drupal\Core\Entity\EntityManagerInterface
  29. */
  30. private $entityManager;
  31. /**
  32. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityManager
  33. */
  34. public function __construct(EntityTypeManagerInterface $entityManager) {
  35. $this->entityManager = $entityManager;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getStorage($entity_type) {
  41. /** @var ConfigEntityStorageInterface $handler */
  42. $handler = $this->getHandler($entity_type, 'storage');
  43. $type = ($handler instanceof ConfigEntityStorageInterface) ? 'config' : 'content';
  44. if (!isset($this->loaded[$type][$entity_type])) {
  45. $handler = $this->getStorageDecorator($entity_type, $handler);
  46. $this->loaded[$type][$entity_type] = $handler;
  47. }
  48. else {
  49. $handler = $this->loaded[$type][$entity_type];
  50. }
  51. return $handler;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getViewBuilder($entity_type) {
  57. /** @var EntityViewBuilderInterface $handler */
  58. $handler = $this->getHandler($entity_type, 'view_builder');
  59. if ($handler instanceof EntityViewBuilderInterface) {
  60. if (!isset($this->rendered[$entity_type])) {
  61. $handler = new EntityViewBuilderDecorator($handler);
  62. $this->rendered[$entity_type] = $handler;
  63. }
  64. else {
  65. $handler = $this->rendered[$entity_type];
  66. }
  67. }
  68. return $handler;
  69. }
  70. /**
  71. * @param $entity_type
  72. * @param $handler
  73. *
  74. * @return \Drupal\webprofiler\Entity\EntityDecorator
  75. */
  76. private function getStorageDecorator($entity_type, $handler) {
  77. if ($handler instanceof ConfigEntityStorageInterface) {
  78. switch ($entity_type) {
  79. case 'taxonomy_vocabulary':
  80. return new VocabularyStorageDecorator($handler);
  81. break;
  82. case 'user_role':
  83. return new RoleStorageDecorator($handler);
  84. break;
  85. case 'shortcut_set':
  86. return new ShortcutSetStorageDecorator($handler);
  87. break;
  88. case 'image_style':
  89. return new ImageStyleStorageDecorator($handler);
  90. break;
  91. default:
  92. return new ConfigEntityStorageDecorator($handler);
  93. break;
  94. }
  95. }
  96. return $handler;
  97. }
  98. /**
  99. * @param $type
  100. * @param $entity_type
  101. *
  102. * @return array
  103. */
  104. public function getLoaded($type, $entity_type) {
  105. return isset($this->loaded[$type][$entity_type]) ? $this->loaded[$type][$entity_type] : NULL;
  106. }
  107. /**
  108. * @param $entity_type
  109. *
  110. * @return array
  111. */
  112. public function getRendered( $entity_type) {
  113. return isset($this->rendered[$entity_type]) ? $this->rendered[$entity_type] : NULL;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function useCaches($use_caches = FALSE) {
  119. $this->entityManager->useCaches($use_caches);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function hasDefinition($plugin_id) {
  125. return $this->entityManager->hasDefinition($plugin_id);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function getAccessControlHandler($entity_type) {
  131. return $this->entityManager->getAccessControlHandler($entity_type);
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function clearCachedDefinitions() {
  137. $this->entityManager->clearCachedDefinitions();
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getListBuilder($entity_type) {
  143. return $this->entityManager->getListBuilder($entity_type);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getFormObject($entity_type, $operation) {
  149. return $this->entityManager->getFormObject($entity_type, $operation);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getRouteProviders($entity_type) {
  155. return $this->entityManager->getRouteProviders($entity_type);
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function hasHandler($entity_type, $handler_type) {
  161. return $this->entityManager->hasHandler($entity_type, $handler_type);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function getHandler($entity_type, $handler_type) {
  167. return $this->entityManager->getHandler($entity_type, $handler_type);
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function createHandlerInstance(
  173. $class,
  174. EntityTypeInterface $definition = NULL
  175. ) {
  176. return $this->entityManager->createHandlerInstance($class, $definition);
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function getDefinition($entity_type_id, $exception_on_invalid = TRUE) {
  182. return $this->entityManager->getDefinition(
  183. $entity_type_id,
  184. $exception_on_invalid
  185. );
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getDefinitions() {
  191. return $this->entityManager->getDefinitions();
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function createInstance($plugin_id, array $configuration = []) {
  197. return $this->entityManager->createInstance($plugin_id, $configuration);
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getInstance(array $options) {
  203. return $this->entityManager->getInstance($options);
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. public function setContainer(ContainerInterface $container = NULL) {
  209. $this->entityManager->setContainer($container);
  210. }
  211. }