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