Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Cache / CacheableResponseTrait.php
diff --git a/web/core/lib/Drupal/Core/Cache/CacheableResponseTrait.php b/web/core/lib/Drupal/Core/Cache/CacheableResponseTrait.php
new file mode 100644 (file)
index 0000000..2940c13
--- /dev/null
@@ -0,0 +1,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;
+  }
+
+}