You are here

trait CacheableResponseTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Cache/CacheableResponseTrait.php \Drupal\Core\Cache\CacheableResponseTrait

Provides an implementation of CacheableResponseInterface.

Hierarchy

See also

\Drupal\Core\Cache\CacheableResponseInterface

6 files declare their use of CacheableResponseTrait
CacheableResourceResponse.php in core/modules/jsonapi/src/CacheableResourceResponse.php
CacheableSecuredRedirectResponse.php in core/lib/Drupal/Core/Routing/CacheableSecuredRedirectResponse.php
CacheableTestResponse.php in core/modules/system/tests/modules/early_rendering_controller_test/src/CacheableTestResponse.php
HtmlResponse.php in core/lib/Drupal/Core/Render/HtmlResponse.php
ResourceResponse.php in core/modules/rest/src/ResourceResponse.php

... See full list

File

core/lib/Drupal/Core/Cache/CacheableResponseTrait.php, line 10

Namespace

Drupal\Core\Cache
View source
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;
  }

}

Members