summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Cache/CacheableResponseTrait.php
blob: 2940c1309736486cfd0205823098872172a28dff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace Drupal\Core\Cache;

/**
 * Provides an implementation of CacheableResponseInterface.
 *
 * @see \Drupal\Core\Cache\CacheableResponseInterface
 */
trait CacheableResponseTrait {

  /**
   * The cacheability metadata.
   *
   * @var \Drupal\Core\Cache\CacheableMetadata
   */
  protected $cacheabilityMetadata;

  /**
   * {@inheritdoc}
   */
  public function addCacheableDependency($dependency) {
    // A trait doesn't have a constructor, so initialize the cacheability
    // metadata if that hasn't happened yet.
    if (!isset($this->cacheabilityMetadata)) {
      $this->cacheabilityMetadata = new CacheableMetadata();
    }

    $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency));

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    // A trait doesn't have a constructor, so initialize the cacheability
    // metadata if that hasn't happened yet.
    if (!isset($this->cacheabilityMetadata)) {
      $this->cacheabilityMetadata = new CacheableMetadata();
    }

    return $this->cacheabilityMetadata;
  }

}