You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php namespace Wpstudio\Helpers\Classes\Observer;
  2. use Illuminate\Database\Eloquent\Model;
  3. use Illuminate\Support\Collection;
  4. use Cache;
  5. abstract class BaseObserver implements Observer
  6. {
  7. protected static string $class;
  8. public static function clearCache(?Model $model = null, ?array $additionalPieces = []): void
  9. {
  10. Cache::tags(
  11. static::getCacheTag(
  12. !is_null($model) ? $model->getKey() : null,
  13. $additionalPieces,
  14. true
  15. )
  16. )->flush();
  17. }
  18. /**
  19. * @param int|Collection|array|null $identifiers
  20. * @param array|null $additionalPieces
  21. * @param bool $excludeBaseClassNameTagWithoutIdentifiers - это опция нужна, если вы хотите исключить имя базового класса обсервера из результирующего набора тегов
  22. * @desc $excludeBaseClassNameTagWithoutIdentifiers - выключает из результирующего массива тег кеша только с базовым классом, если переданы идентификаторы
  23. * @desc То есть в итоговой выборке - не будет тега, без идентификатора, если вообще идентификаторы переданы
  24. * @return array
  25. */
  26. public static function getCacheTag($identifiers = null, ?array $additionalPieces = [], bool $excludeBaseClassNameTagWithoutIdentifiers = false): array
  27. {
  28. return static::getCacheTagsWithMaybeIdentifiers(static::getClass(), $identifiers, $additionalPieces, $excludeBaseClassNameTagWithoutIdentifiers);
  29. }
  30. public static function formatCacheTag(array $pieces): string
  31. {
  32. return implode('.', $pieces);
  33. }
  34. /**
  35. * @param int|Collection|array|null $value
  36. * @return array
  37. */
  38. private static function forceArray($value = null): array
  39. {
  40. return $value instanceof Collection ? $value->toArray() : (array)$value;
  41. }
  42. /**
  43. * @param string $baseClassName
  44. * @param int|Collection|array|null $iterateIdentifiers
  45. * @param array|null $additionalTagPieces
  46. * @param bool $excludeBaseClassNameTagWithoutIdentifiers
  47. * @return array
  48. */
  49. private static function getCacheTagsWithMaybeIdentifiers(string $baseClassName, $iterateIdentifiers = [], ?array $additionalTagPieces = [], bool $excludeBaseClassNameTagWithoutIdentifiers = false): array
  50. {
  51. $iterateIdentifiers = static::forceArray($iterateIdentifiers);
  52. $cacheTags = [];
  53. if (!$excludeBaseClassNameTagWithoutIdentifiers || !$iterateIdentifiers) {
  54. $cacheTags = (array)static::formatCacheTag(array_merge([$baseClassName], $additionalTagPieces));
  55. }
  56. if ($iterateIdentifiers) {
  57. $cacheTags = array_merge(
  58. $cacheTags,
  59. array_map(
  60. fn ($identifier) => static::formatCacheTag(
  61. array_merge([$baseClassName, $identifier], $additionalTagPieces)
  62. ),
  63. $iterateIdentifiers
  64. )
  65. );
  66. }
  67. return $cacheTags;
  68. }
  69. }