Data.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Mview\Config;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. /**
  9. * Provides materialized view configuration
  10. */
  11. class Data extends \Magento\Framework\Config\Data
  12. {
  13. /**
  14. * @var \Magento\Framework\Mview\View\State\CollectionInterface
  15. */
  16. protected $stateCollection;
  17. /**
  18. * Constructor
  19. *
  20. * @param Reader $reader
  21. * @param \Magento\Framework\Config\CacheInterface $cache
  22. * @param \Magento\Framework\Mview\View\State\CollectionInterface $stateCollection
  23. * @param string|null $cacheId
  24. * @param SerializerInterface|null $serializer
  25. */
  26. public function __construct(
  27. \Magento\Framework\Mview\Config\Reader $reader,
  28. \Magento\Framework\Config\CacheInterface $cache,
  29. \Magento\Framework\Mview\View\State\CollectionInterface $stateCollection,
  30. $cacheId = 'mview_config',
  31. SerializerInterface $serializer = null
  32. ) {
  33. $this->stateCollection = $stateCollection;
  34. $isCacheExists = $cache->test($cacheId);
  35. parent::__construct($reader, $cache, $cacheId, $serializer);
  36. if (!$isCacheExists) {
  37. $this->deleteNonexistentStates();
  38. }
  39. }
  40. /**
  41. * Delete all states that are not in configuration
  42. *
  43. * @return void
  44. */
  45. protected function deleteNonexistentStates()
  46. {
  47. foreach ($this->stateCollection->getItems() as $state) {
  48. /** @var \Magento\Framework\Mview\View\StateInterface $state */
  49. if (!isset($this->_data[$state->getViewId()])) {
  50. $state->delete();
  51. }
  52. }
  53. }
  54. }