vendor/pimcore/pimcore/models/DataObject/Classificationstore/DefinitionCache.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\DataObject\Classificationstore;
  15. /**
  16. * @internal
  17. */
  18. class DefinitionCache
  19. {
  20. /**
  21. * @var array
  22. */
  23. public static $cache = [];
  24. /**
  25. * @param int $id
  26. * @param string $type
  27. *
  28. * @return KeyConfig|null
  29. */
  30. public static function get($id, $type = 'key')
  31. {
  32. $key = $type . $id;
  33. $config = isset(self::$cache[$key]) ? self::$cache[$key] : null;
  34. if ($config) {
  35. return $config;
  36. }
  37. $config = KeyConfig::getById($id);
  38. if (!$config) {
  39. return null;
  40. }
  41. self::put($config);
  42. return $config;
  43. }
  44. /**
  45. * @param KeyConfig|GroupConfig $config
  46. */
  47. public static function put($config)
  48. {
  49. $type = self::getType($config);
  50. if (!$type) {
  51. return;
  52. }
  53. $key = $type . $config->getId();
  54. self::$cache[$key] = $config;
  55. }
  56. /**
  57. * @param KeyConfig|GroupConfig|null $config
  58. */
  59. public static function clear($config)
  60. {
  61. if ($config) {
  62. $type = self::getType($config);
  63. if (!$type) {
  64. return;
  65. }
  66. $key = $type . $config->getId();
  67. unset(self::$cache[$key]);
  68. } else {
  69. self::$cache = [];
  70. }
  71. }
  72. /**
  73. * @param KeyConfig|GroupConfig $config
  74. *
  75. * @return string|null
  76. */
  77. protected static function getType($config)
  78. {
  79. $type = null;
  80. if ($config instanceof KeyConfig) {
  81. $type = 'key';
  82. } elseif ($config instanceof GroupConfig) {
  83. $type = 'group';
  84. }
  85. return $type;
  86. }
  87. }