class LinkManagerBase

Same name in this branch
  1. 8.9.x core/modules/rest/src/LinkManager/LinkManagerBase.php \Drupal\rest\LinkManager\LinkManagerBase
Same name in other branches
  1. 9 core/modules/hal/src/LinkManager/LinkManagerBase.php \Drupal\hal\LinkManager\LinkManagerBase

Defines an abstract base-class for HAL link manager objects.

Hierarchy

Expanded class hierarchy of LinkManagerBase

1 file declares its use of LinkManagerBase
LinkManagerBase.php in core/modules/rest/src/LinkManager/LinkManagerBase.php

File

core/modules/hal/src/LinkManager/LinkManagerBase.php, line 10

Namespace

Drupal\hal\LinkManager
View source
abstract class LinkManagerBase {
    
    /**
     * Link domain used for type links URIs.
     *
     * @var string
     */
    protected $linkDomain;
    
    /**
     * Config factory service.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * {@inheritdoc}
     */
    public function setLinkDomain($domain) {
        $this->linkDomain = rtrim($domain, '/');
        return $this;
    }
    
    /**
     * Gets the link domain.
     *
     * @param array $context
     *   Normalization/serialization context.
     *
     * @return string
     *   The link domain.
     *
     * @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
     * @see \Symfony\Component\Serializer\SerializerInterface::serialize()
     * @see \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY
     */
    protected function getLinkDomain(array $context = []) {
        if (empty($this->linkDomain)) {
            if ($domain = $this->configFactory
                ->get('hal.settings')
                ->get('link_domain')) {
                // Bubble the appropriate cacheability metadata whenever possible.
                if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
                    $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory
                        ->get('hal.settings'));
                }
                return rtrim($domain, '/');
            }
            else {
                // Bubble the relevant cacheability metadata whenever possible.
                if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
                    $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts([
                        'url.site',
                    ]);
                }
                $request = $this->requestStack
                    ->getCurrentRequest();
                return $request->getSchemeAndHttpHost() . $request->getBasePath();
            }
        }
        return $this->linkDomain;
    }

}

Members

Title Sort descending Modifiers Object type Summary
LinkManagerBase::$configFactory protected property Config factory service.
LinkManagerBase::$linkDomain protected property Link domain used for type links URIs.
LinkManagerBase::$requestStack protected property The request stack.
LinkManagerBase::getLinkDomain protected function Gets the link domain.
LinkManagerBase::setLinkDomain public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.