Open Data Catalog v2.0.0
MetastoreApiResponse.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Drupal\metastore;
4 
5 use Drupal\Core\Cache\CacheableJsonResponse;
6 use Drupal\Core\Cache\CacheableMetadata;
8 use Symfony\Component\HttpFoundation\ParameterBag;
9 
14 
21  public function __construct(MetastoreItemFactoryInterface $metastoreItemFactory) {
22  $this->metastoreItemFactory = $metastoreItemFactory;
23  }
24 
45  public function cachedJsonResponse(
46  $data,
47  int $code = 200,
48  array $dependencies = [],
49  ?ParameterBag $params = NULL
50  ): CacheableJsonResponse {
51 
52  $response = CacheableJsonResponse::create($data, $code, []);
53  if ($cacheMetadata = $this->getCacheMetadata($dependencies, $params)) {
54  $response->addCacheableDependency($cacheMetadata);
55  }
56 
57  return $response;
58  }
59 
73  private function getCacheMetadata(array $dependencies, ?ParameterBag $params) {
74  $cacheMetadata = new CacheableMetadata();
75 
76  foreach ($dependencies as $key => $item) {
77  $this->addDependency($cacheMetadata, $key, $item);
78  }
79 
80  $this->addContexts($cacheMetadata, $params ?? new ParameterBag([]));
81 
82  return $cacheMetadata;
83  }
84 
95  private function addDependency(CacheableMetadata $cacheMetadata, $key, $item) {
96  if (is_string($key) && is_array($item)) {
97  $this->addItemDependencies($cacheMetadata, $item);
98  }
99  elseif (is_string($item)) {
100  $this->addSchemaDependency($cacheMetadata, $item);
101  }
102  else {
103  throw new \InvalidArgumentException("Invalid cacheable dependency. " . print_r([$key => $item], TRUE));
104  }
105  }
106 
115  private function addItemDependencies(CacheableMetadata $cacheMetadata, array $ids) {
116  foreach ($ids as $identifier) {
117  $item = $this->getMetastoreItemFactory()->getInstance($identifier);
118  $cacheMetadata->addCacheableDependency($item);
119  $this->addReferenceDependencies($cacheMetadata, $item);
120  }
121  }
122 
131  protected function addReferenceDependencies(CacheableMetadata $cacheMetadata, MetastoreItemInterface $item) {
132  $metadata = $item->getMetaData();
133  $ids = [];
134  foreach ($metadata as $propertyId => $value) {
135  if (substr($propertyId, 0, 4) == '%Ref') {
136  $this->addReferenceIdentifier($ids, $value);
137  }
138  }
139  $ids = array_filter($ids);
140  $this->addItemDependencies($cacheMetadata, $ids);
141  }
142 
151  private function addReferenceIdentifier(array &$ids, $value) {
152  if (is_array($value)) {
153  foreach ($value as $ref) {
154  $ids[] = $ref->identifier ?? NULL;
155  }
156  }
157  else {
158  $ids[] = $value->identifier ?? NULL;
159  }
160  }
161 
173  private function addSchemaDependency(CacheableMetadata $cacheMetadata, $schema) {
174  // Silly line to make linters happy. Right now the itemFactory doesn't
175  // require a schema so it's not used.
176  $schema = $schema;
177  $cacheTags = $this->getMetastoreItemFactory()->getCacheTags();
178  $cacheMetadata->addCacheTags($cacheTags);
179  }
180 
187  protected function getMetastoreItemFactory() {
188  return $this->metastoreItemFactory;
189  }
190 
199  private function addContexts(CacheableMetadata $cacheMetadata, ParameterBag $params) {
200  $cacheMetadata->addCacheContexts(["url"]);
201  foreach ($params->keys() as $key) {
202  $cacheMetadata->addCacheContexts(["url.query_args:$key"]);
203  }
204  }
205 
206 }
Drupal\metastore\MetastoreApiResponse\getMetastoreItemFactory
getMetastoreItemFactory()
Definition: MetastoreApiResponse.php:187
Drupal\metastore\Factory\MetastoreItemFactoryInterface
Definition: MetastoreItemFactoryInterface.php:15
Drupal\metastore
Drupal\metastore\MetastoreApiResponse\__construct
__construct(MetastoreItemFactoryInterface $metastoreItemFactory)
Definition: MetastoreApiResponse.php:21
Drupal\metastore\MetastoreApiResponse
Definition: MetastoreApiResponse.php:13
Drupal\metastore\MetastoreItemInterface
Definition: MetastoreItemInterface.php:10
Drupal\metastore\MetastoreApiResponse\addReferenceDependencies
addReferenceDependencies(CacheableMetadata $cacheMetadata, MetastoreItemInterface $item)
Definition: MetastoreApiResponse.php:131
Drupal\metastore\MetastoreApiResponse\cachedJsonResponse
cachedJsonResponse( $data, int $code=200, array $dependencies=[], ?ParameterBag $params=NULL)
Definition: MetastoreApiResponse.php:45