d771d98eacd0477b35dd43ac06b9099fd06867e6
[yaffs-website] / web / core / modules / block_content / src / BlockContentUuidLookup.php
1 <?php
2
3 namespace Drupal\block_content;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\CacheCollector;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Lock\LockBackendInterface;
9
10 /**
11  * A cache collector that caches IDs for block_content UUIDs.
12  *
13  * As block_content entities are used as block plugin derivatives, it is a
14  * fairly safe limitation that there are not hundreds of them, a site will
15  * likely run into problems with too many block content entities in other places
16  * than a cache that only stores UUID's and IDs. The same assumption is not true
17  * for other content entities.
18  *
19  * @internal
20  */
21 class BlockContentUuidLookup extends CacheCollector {
22
23   /**
24    * The entity type manager.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
27    */
28   protected $entityTypeManager;
29
30   /**
31    * Constructs a BlockContentUuidLookup instance.
32    *
33    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
34    *   The cache backend.
35    * @param \Drupal\Core\Lock\LockBackendInterface $lock
36    *   The lock backend.
37    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38    *   The entity type manager.
39    */
40   public function __construct(CacheBackendInterface $cache, LockBackendInterface $lock, EntityTypeManagerInterface $entity_type_manager) {
41     parent::__construct('block_content_uuid', $cache, $lock);
42     $this->entityTypeManager = $entity_type_manager;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function resolveCacheMiss($key) {
49     $ids = $this->entityTypeManager->getStorage('block_content')->getQuery()
50       ->condition('uuid', $key)
51       ->execute();
52
53     // Only cache if there is a match, otherwise creating new entities would
54     // require to invalidate the cache.
55     $id = reset($ids);
56     if ($id) {
57       $this->storage[$key] = $id;
58       $this->persist($key);
59     }
60     return $id;
61   }
62
63 }