Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\hal\LinkManager;
|
Chris@0
|
4
|
Chris@14
|
5 use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
|
Chris@14
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Defines an abstract base-class for HAL link manager objects.
|
Chris@0
|
9 */
|
Chris@0
|
10 abstract class LinkManagerBase {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Link domain used for type links URIs.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @var string
|
Chris@0
|
16 */
|
Chris@0
|
17 protected $linkDomain;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Config factory service.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\Core\Config\ConfigFactoryInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $configFactory;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * The request stack.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var \Symfony\Component\HttpFoundation\RequestStack
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $requestStack;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 public function setLinkDomain($domain) {
|
Chris@0
|
37 $this->linkDomain = rtrim($domain, '/');
|
Chris@0
|
38 return $this;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Gets the link domain.
|
Chris@0
|
43 *
|
Chris@14
|
44 * @param array $context
|
Chris@14
|
45 * Normalization/serialization context.
|
Chris@14
|
46 *
|
Chris@0
|
47 * @return string
|
Chris@0
|
48 * The link domain.
|
Chris@14
|
49 *
|
Chris@14
|
50 * @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
|
Chris@14
|
51 * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
|
Chris@14
|
52 * @see \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY
|
Chris@0
|
53 */
|
Chris@14
|
54 protected function getLinkDomain(array $context = []) {
|
Chris@0
|
55 if (empty($this->linkDomain)) {
|
Chris@0
|
56 if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
|
Chris@14
|
57 // Bubble the appropriate cacheability metadata whenever possible.
|
Chris@14
|
58 if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
Chris@14
|
59 $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
|
Chris@14
|
60 }
|
Chris@14
|
61 return rtrim($domain, '/');
|
Chris@0
|
62 }
|
Chris@0
|
63 else {
|
Chris@14
|
64 // Bubble the relevant cacheability metadata whenever possible.
|
Chris@14
|
65 if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
Chris@14
|
66 $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
|
Chris@14
|
67 }
|
Chris@0
|
68 $request = $this->requestStack->getCurrentRequest();
|
Chris@14
|
69 return $request->getSchemeAndHttpHost() . $request->getBasePath();
|
Chris@0
|
70 }
|
Chris@0
|
71 }
|
Chris@0
|
72 return $this->linkDomain;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 }
|