CacheContext.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer;
  7. /**
  8. * Class Context
  9. */
  10. class CacheContext implements \Magento\Framework\DataObject\IdentityInterface
  11. {
  12. /**
  13. * @var array
  14. */
  15. protected $entities = [];
  16. /**
  17. * @var array
  18. */
  19. private $tags = [];
  20. /**
  21. * Register entity Ids
  22. *
  23. * @param string $cacheTag
  24. * @param array $ids
  25. * @return $this
  26. */
  27. public function registerEntities($cacheTag, $ids)
  28. {
  29. $this->entities[$cacheTag] = array_merge($this->getRegisteredEntity($cacheTag), $ids);
  30. return $this;
  31. }
  32. /**
  33. * Register entity tags
  34. *
  35. * @param array $cacheTags
  36. * @return $this
  37. */
  38. public function registerTags($cacheTags)
  39. {
  40. $this->tags = array_merge($this->tags, $cacheTags);
  41. return $this;
  42. }
  43. /**
  44. * Returns registered entities
  45. *
  46. * @param string $cacheTag
  47. * @return array
  48. */
  49. public function getRegisteredEntity($cacheTag)
  50. {
  51. if (empty($this->entities[$cacheTag])) {
  52. return [];
  53. } else {
  54. return $this->entities[$cacheTag];
  55. }
  56. }
  57. /**
  58. * Returns identities
  59. *
  60. * @return array
  61. */
  62. public function getIdentities()
  63. {
  64. $identities = [];
  65. foreach ($this->entities as $cacheTag => $ids) {
  66. foreach ($ids as $id) {
  67. $identities[] = $cacheTag . '_' . $id;
  68. }
  69. }
  70. return array_merge($identities, array_unique($this->tags));
  71. }
  72. }