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