Issue #2854830 by Wim Leers: Move rest/serialization module's "link manager" services to HAL module

8.4.x
Nathaniel Catchpole 2017-02-27 12:22:01 +00:00
parent 7562fb2476
commit 55a4f988c1
42 changed files with 337 additions and 281 deletions

View File

@ -1,3 +1,3 @@
# Set the domain for Serialization type and relation links.
# Set the domain for HAL type and relation links.
# If left blank, the site's domain will be used.
link_domain: ~

View File

@ -0,0 +1,8 @@
# Schema for the configuration files of the HAL module.
hal.settings:
type: config_object
label: 'HAL settings'
mapping:
link_domain:
type: string
label: 'Domain of the relation'

View File

@ -2,7 +2,7 @@
/**
* @file
* Describes hooks provided by the Serialization module.
* Describes hooks provided by the HAL module.
*/
/**
@ -11,7 +11,7 @@
*/
/**
* Alter the serialization type URI.
* Alter the HAL type URI.
*
* Modules may wish to alter the type URI generated for a resource based on the
* context of the serializer/normalizer operation.
@ -26,16 +26,16 @@
* @see \Symfony\Component\Serializer\NormalizerInterface::normalize()
* @see \Symfony\Component\Serializer\DenormalizerInterface::denormalize()
*/
function hook_serialization_type_uri_alter(&$uri, $context = array()) {
function hook_hal_type_uri_alter(&$uri, $context = array()) {
if ($context['mymodule'] == TRUE) {
$base = \Drupal::config('serialization.settings')->get('link_domain');
$base = \Drupal::config('hal.settings')->get('link_domain');
$uri = str_replace($base, 'http://mymodule.domain', $uri);
}
}
/**
* Alter the serialization relation URI.
* Alter the HAL relation URI.
*
* Modules may wish to alter the relation URI generated for a resource based on
* the context of the serializer/normalizer operation.
@ -50,9 +50,9 @@ function hook_serialization_type_uri_alter(&$uri, $context = array()) {
* @see \Symfony\Component\Serializer\NormalizerInterface::normalize()
* @see \Symfony\Component\Serializer\DenormalizerInterface::denormalize()
*/
function hook_serialization_relation_uri_alter(&$uri, $context = array()) {
function hook_hal_relation_uri_alter(&$uri, $context = array()) {
if ($context['mymodule'] == TRUE) {
$base = \Drupal::config('serialization.settings')->get('link_domain');
$base = \Drupal::config('hal.settings')->get('link_domain');
$uri = str_replace($base, 'http://mymodule.domain', $uri);
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* @file
* Update functions for the HAL module.
*/
/**
* @defgroup updates-8.2.x-to-8.3.x Updates from 8.2.x to 8.3.x
* @{
* Update functions from 8.2.x to 8.3.x.
*/
/**
* Move 'link_domain' from 'rest.settings' to 'hal.settings'.
*/
function hal_update_8301() {
$config_factory = \Drupal::configFactory();
// The default value for the 'link_domain' key is `~`, which is the YAML
// equivalent of PHP's `NULL`. If the REST module is not installed, this is
// the value we will store in 'hal.settings'.
$link_domain = NULL;
// But if the REST module was installed, we migrate its 'link_domain' setting,
// because we are actually moving that setting from 'rest.settings' to
// 'hal.settings'.
$rest_settings = $config_factory->getEditable('rest.settings');
if ($rest_settings->getRawData() !== []) {
$link_domain = $rest_settings->get('link_domain');
// Remove the 'link_domain' setting from 'rest.settings'.
$rest_settings->clear('link_domain')
->save();
}
$hal_settings = $config_factory->getEditable('hal.settings');
$hal_settings->set('link_domain', $link_domain);
$hal_settings->save(TRUE);
}
/**
* @} End of "defgroup updates-8.2.x-to-8.3.x".
*/

View File

@ -1,7 +1,7 @@
services:
serializer.normalizer.entity_reference_item.hal:
class: Drupal\hal\Normalizer\EntityReferenceItemNormalizer
arguments: ['@serialization.link_manager', '@serializer.entity_resolver']
arguments: ['@hal.link_manager', '@serializer.entity_resolver']
tags:
- { name: normalizer, priority: 10 }
serializer.normalizer.field_item.hal:
@ -16,13 +16,24 @@ services:
class: Drupal\hal\Normalizer\FileEntityNormalizer
tags:
- { name: normalizer, priority: 20 }
arguments: ['@entity.manager', '@http_client', '@serialization.link_manager', '@module_handler']
arguments: ['@entity.manager', '@http_client', '@hal.link_manager', '@module_handler']
serializer.normalizer.entity.hal:
class: Drupal\hal\Normalizer\ContentEntityNormalizer
arguments: ['@serialization.link_manager', '@entity.manager', '@module_handler']
arguments: ['@hal.link_manager', '@entity.manager', '@module_handler']
tags:
- { name: normalizer, priority: 10 }
serializer.encoder.hal:
class: Drupal\hal\Encoder\JsonEncoder
tags:
- { name: encoder, priority: 10, format: hal_json }
# Link managers.
hal.link_manager:
class: Drupal\hal\LinkManager\LinkManager
arguments: ['@hal.link_manager.type', '@hal.link_manager.relation']
hal.link_manager.type:
class: Drupal\hal\LinkManager\TypeLinkManager
arguments: ['@cache.default', '@module_handler', '@config.factory', '@request_stack', '@entity_type.bundle.info']
hal.link_manager.relation:
class: Drupal\hal\LinkManager\RelationLinkManager
arguments: ['@cache.default', '@entity.manager', '@module_handler', '@config.factory', '@request_stack']

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
/**
* Defines an interface for a link manager with a configurable domain.

View File

@ -1,29 +1,29 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
class LinkManager implements LinkManagerInterface {
/**
* The type link manager.
*
* @var \Drupal\serialization\LinkManager\TypeLinkManagerInterface
* @var \Drupal\hal\LinkManager\TypeLinkManagerInterface
*/
protected $typeLinkManager;
/**
* The relation link manager.
*
* @var \Drupal\serialization\LinkManager\RelationLinkManagerInterface
* @var \Drupal\hal\LinkManager\RelationLinkManagerInterface
*/
protected $relationLinkManager;
/**
* Constructor.
*
* @param \Drupal\serialization\LinkManager\TypeLinkManagerInterface $type_link_manager
* @param \Drupal\hal\LinkManager\TypeLinkManagerInterface $type_link_manager
* Manager for handling bundle URIs.
* @param \Drupal\serialization\LinkManager\RelationLinkManagerInterface $relation_link_manager
* @param \Drupal\hal\LinkManager\RelationLinkManagerInterface $relation_link_manager
* Manager for handling bundle URIs.
*/
public function __construct(TypeLinkManagerInterface $type_link_manager, RelationLinkManagerInterface $relation_link_manager) {

View File

@ -1,9 +1,9 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
/**
* Defines an abstract base-class for Serialization link manager objects.
* Defines an abstract base-class for HAL link manager objects.
*/
abstract class LinkManagerBase {
@ -44,7 +44,7 @@ abstract class LinkManagerBase {
*/
protected function getLinkDomain() {
if (empty($this->linkDomain)) {
if ($domain = $this->configFactory->get('serialization.settings')->get('link_domain')) {
if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
$this->linkDomain = rtrim($domain, '/');
}
else {

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
/**
* Interface implemented by link managers.

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
@ -69,7 +69,7 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
// different (e.g., include a language prefix), then the module must also
// override the RelationLinkManager class/service to return the desired URL.
$uri = $this->getLinkDomain() . "/rest/relation/$entity_type/$bundle/$field_name";
$this->moduleHandler->alter('serialization_relation_uri', $uri, $context);
$this->moduleHandler->alter('hal_relation_uri', $uri, $context);
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
// hook is invoked to maintain backwards compatibility
$this->moduleHandler->alter('rest_relation_uri', $uri, $context);
@ -104,7 +104,7 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
* by corresponding relation URI.
*/
protected function getRelations($context = array()) {
$cid = 'serialization:links:relations';
$cid = 'hal:links:relations';
$cache = $this->cache->get($cid);
if (!$cache) {
$this->writeCache($context);
@ -138,7 +138,7 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
}
// These URIs only change when field info changes, so cache it permanently
// and only clear it when the fields cache is cleared.
$this->cache->set('serialization:links:relations', $data, Cache::PERMANENT, array('entity_field_info'));
$this->cache->set('hal:links:relations', $data, Cache::PERMANENT, array('entity_field_info'));
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
interface RelationLinkManagerInterface extends ConfigurableLinkManagerInterface {

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
@ -70,7 +70,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
// (e.g., include a language prefix), then the module must also override the
// TypeLinkManager class/service to return the desired URL.
$uri = $this->getLinkDomain() . "/rest/type/$entity_type/$bundle";
$this->moduleHandler->alter('serialization_type_uri', $uri, $context);
$this->moduleHandler->alter('hal_type_uri', $uri, $context);
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
// hook is invoked to maintain backwards compatibility
$this->moduleHandler->alter('rest_type_uri', $uri, $context);
@ -99,7 +99,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
* corresponding type URI.
*/
protected function getTypes($context = array()) {
$cid = 'serialization:links:types';
$cid = 'hal:links:types';
$cache = $this->cache->get($cid);
if (!$cache) {
$data = $this->writeCache($context);
@ -143,7 +143,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
}
// These URIs only change when entity info changes, so cache it permanently
// and only clear it when entity_info is cleared.
$this->cache->set('serialization:links:types', $data, Cache::PERMANENT, array('entity_types'));
$this->cache->set('hal:links:types', $data, Cache::PERMANENT, array('entity_types'));
return $data;
}

View File

@ -1,6 +1,6 @@
<?php
namespace Drupal\serialization\LinkManager;
namespace Drupal\hal\LinkManager;
interface TypeLinkManagerInterface extends ConfigurableLinkManagerInterface {

View File

@ -6,7 +6,7 @@ use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\serialization\LinkManager\LinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
@ -27,7 +27,7 @@ class ContentEntityNormalizer extends NormalizerBase {
/**
* The hypermedia link manager.
*
* @var \Drupal\serialization\LinkManager\LinkManagerInterface
* @var \Drupal\hal\LinkManager\LinkManagerInterface
*/
protected $linkManager;
@ -41,7 +41,7 @@ class ContentEntityNormalizer extends NormalizerBase {
/**
* Constructs an ContentEntityNormalizer object.
*
* @param \Drupal\serialization\LinkManager\LinkManagerInterface $link_manager
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
*/
public function __construct(LinkManagerInterface $link_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {

View File

@ -3,7 +3,7 @@
namespace Drupal\hal\Normalizer;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\serialization\LinkManager\LinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use Drupal\serialization\EntityResolver\EntityResolverInterface;
use Drupal\serialization\EntityResolver\UuidReferenceInterface;
@ -22,7 +22,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
/**
* The hypermedia link manager.
*
* @var \Drupal\serialization\LinkManager\LinkManagerInterface
* @var \Drupal\hal\LinkManager\LinkManagerInterface
*/
protected $linkManager;
@ -36,7 +36,7 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidR
/**
* Constructs an EntityReferenceItemNormalizer object.
*
* @param \Drupal\serialization\LinkManager\LinkManagerInterface $link_manager
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
* The entity resolver.

View File

@ -4,7 +4,7 @@ namespace Drupal\hal\Normalizer;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\serialization\LinkManager\LinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface;
use GuzzleHttp\ClientInterface;
/**
@ -33,7 +33,7 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
* The entity manager.
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP Client.
* @param \Drupal\serialization\LinkManager\LinkManagerInterface $link_manager
* @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
* The hypermedia link manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.

View File

@ -3,7 +3,7 @@
/**
* @file
* Contains database additions to drupal-8.bare.standard.php.gz for testing the
* upgrade path of serialization_update_8301().
* upgrade path of hal_update_8301().
*/
use Drupal\Core\Database\Database;
@ -12,6 +12,11 @@ $connection = Database::getConnection();
// Set the schema version.
$connection->insert('key_value')
->fields([
'collection' => 'system.schema',
'name' => 'hal',
'value' => 'i:8000;',
])
->fields([
'collection' => 'system.schema',
'name' => 'serialization',
@ -27,6 +32,7 @@ $extensions = $connection->select('config')
->execute()
->fetchField();
$extensions = unserialize($extensions);
$extensions['module']['hal'] = 0;
$extensions['module']['serialization'] = 0;
$connection->update('config')
->fields([

View File

@ -0,0 +1,6 @@
name: HAL test module
type: module
description: "Support module for HAL tests."
package: Testing
version: VERSION
core: 8.x

View File

@ -0,0 +1,46 @@
<?php
/**
* @file
* Contains hook implementations for testing HAL module.
*/
/**
* Implements hook_hal_type_uri_alter().
*/
function hal_test_hal_type_uri_alter(&$uri, $context = array()) {
if (!empty($context['hal_test'])) {
$uri = 'hal_test_type';
}
}
/**
* Implements hook_hal_relation_uri_alter().
*/
function hal_test_hal_relation_uri_alter(&$uri, $context = array()) {
if (!empty($context['hal_test'])) {
$uri = 'hal_test_relation';
}
}
/**
* Implements hook_rest_type_uri_alter().
*
* @deprecated Kept only for BC test coverage, see \Drupal\Tests\hal\Kernel\HalLinkManagerTest::testGetTypeUri().
*/
function hal_test_rest_type_uri_alter(&$uri, $context = array()) {
if (!empty($context['rest_test'])) {
$uri = 'rest_test_type';
}
}
/**
* Implements hook_rest_relation_uri_alter().
*
* @deprecated Kept only for BC test coverage, see \Drupal\Tests\hal\Kernel\HalLinkManagerTest::testGetRelationUri().
*/
function hal_test_rest_relation_uri_alter(&$uri, $context = array()) {
if (!empty($context['rest_test'])) {
$uri = 'rest_test_relation';
}
}

View File

@ -92,7 +92,7 @@ trait HalEntityNormalizationTrait {
* {@inheritdoc}
*/
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
// \Drupal\serialization\Normalizer\EntityNormalizer::denormalize(): entity
// \Drupal\hal\Normalizer\EntityNormalizer::denormalize(): entity
// types with bundles MUST send their bundle field to be denormalizable.
if ($this->entity->getEntityType()->hasKey('bundle')) {
$normalization = $this->getNormalizedPostEntity();

View File

@ -0,0 +1,42 @@
<?php
namespace Drupal\Tests\hal\Functional\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* Tests that 'hal.settings' is created, to store 'link_domain'.
*
* @see https://www.drupal.org/node/2758897
*
* @group hal
*/
class CreateHalSettingsForLinkDomainUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
public function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.hal-hal_update_8301.php',
];
}
/**
* Tests hal_update_8301().
*/
public function testHalSettingsCreated() {
// Make sure we have the expected values before the update.
$hal_settings = $this->config('hal.settings');
$this->assertIdentical([], $hal_settings->getRawData());
$this->runUpdates();
// Make sure we have the expected values after the update.
$hal_settings = \Drupal::configFactory()->get('hal.settings');
$this->assertTrue(array_key_exists('link_domain', $hal_settings->getRawData()));
$this->assertIdentical(NULL, $hal_settings->getRawData()['link_domain']);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace Drupal\Tests\hal\Functional\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* 'link_domain' is migrated from 'rest.settings' to 'hal.settings'.
*
* @see https://www.drupal.org/node/2758897
*
* @group hal
*/
class MigrateLinkDomainSettingFromRestToHalUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
public function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.hal-hal_update_8301.php',
__DIR__ . '/../../../fixtures/update/drupal-8.rest-hal_update_8301.php',
];
}
/**
* Tests hal_update_8301().
*/
public function testLinkDomainMigratedFromRestSettingsToHalSettings() {
// Make sure we have the expected values before the update.
$hal_settings = $this->config('hal.settings');
$this->assertIdentical([], $hal_settings->getRawData());
$rest_settings = $this->config('rest.settings');
$this->assertTrue(array_key_exists('link_domain', $rest_settings->getRawData()));
$this->assertIdentical('http://example.com', $rest_settings->getRawData()['link_domain']);
$this->runUpdates();
// Make sure we have the expected values after the update.
$hal_settings = \Drupal::configFactory()->get('hal.settings');
$this->assertTrue(array_key_exists('link_domain', $hal_settings->getRawData()));
$this->assertIdentical('http://example.com', $hal_settings->getRawData()['link_domain']);
$rest_settings = $this->config('rest.settings');
$this->assertFalse(array_key_exists('link_domain', $rest_settings->getRawData()));
}
}

View File

@ -5,11 +5,11 @@ namespace Drupal\Tests\hal\Kernel;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\file\Entity\File;
use Drupal\hal\Encoder\JsonEncoder;
use Drupal\hal\LinkManager\LinkManager;
use Drupal\hal\LinkManager\RelationLinkManager;
use Drupal\hal\LinkManager\TypeLinkManager;
use Drupal\hal\Normalizer\FieldItemNormalizer;
use Drupal\hal\Normalizer\FileEntityNormalizer;
use Drupal\serialization\LinkManager\LinkManager;
use Drupal\serialization\LinkManager\RelationLinkManager;
use Drupal\serialization\LinkManager\TypeLinkManager;
use Symfony\Component\Serializer\Serializer;

View File

@ -1,20 +1,20 @@
<?php
namespace Drupal\Tests\serialization\Kernel;
namespace Drupal\Tests\hal\Kernel;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\serialization\LinkManager\LinkManager
* @group serialization
* @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
* @group hal
*/
class SerializationLinkManagerTest extends KernelTestBase {
class HalLinkManagerTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['serialization', 'serialization_test', 'system'];
public static $modules = ['hal', 'hal_test', 'serialization', 'system'];
/**
* {@inheritdoc}
@ -29,13 +29,13 @@ class SerializationLinkManagerTest extends KernelTestBase {
*/
public function testGetTypeUri() {
/* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
$type_manager = \Drupal::service('serialization.link_manager.type');
$type_manager = \Drupal::service('hal.link_manager.type');
$base = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
$link = $type_manager->getTypeUri('node', 'page');
$this->assertSame($link, $base . 'rest/type/node/page');
// Now with optional context.
$link = $type_manager->getTypeUri('node', 'page', ['serialization_test' => TRUE]);
$this->assertSame($link, 'serialization_test_type');
$link = $type_manager->getTypeUri('node', 'page', ['hal_test' => TRUE]);
$this->assertSame($link, 'hal_test_type');
// Test BC: hook_rest_type_uri_alter().
$link = $type_manager->getTypeUri('node', 'page', ['rest_test' => TRUE]);
$this->assertSame($link, 'rest_test_type');
@ -46,13 +46,13 @@ class SerializationLinkManagerTest extends KernelTestBase {
*/
public function testGetRelationUri() {
/* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
$relation_manager = \Drupal::service('serialization.link_manager.relation');
$relation_manager = \Drupal::service('hal.link_manager.relation');
$base = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
$link = $relation_manager->getRelationUri('node', 'page', 'field_ref');
$this->assertSame($link, $base . 'rest/relation/node/page/field_ref');
// Now with optional context.
$link = $relation_manager->getRelationUri('node', 'page', 'foobar', ['serialization_test' => TRUE]);
$this->assertSame($link, 'serialization_test_relation');
$link = $relation_manager->getRelationUri('node', 'page', 'foobar', ['hal_test' => TRUE]);
$this->assertSame($link, 'hal_test_relation');
// Test BC: hook_rest_relation_uri_alter().
$link = $relation_manager->getRelationUri('node', 'page', 'foobar', ['rest_test' => TRUE]);
$this->assertSame($link, 'rest_test_relation');
@ -61,9 +61,9 @@ class SerializationLinkManagerTest extends KernelTestBase {
/**
* @covers ::setLinkDomain
*/
public function testSerializationLinkManagersSetLinkDomain() {
public function testHalLinkManagersSetLinkDomain() {
/* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
$link_manager = \Drupal::service('serialization.link_manager');
$link_manager = \Drupal::service('hal.link_manager');
$link_manager->setLinkDomain('http://example.com/');
$link = $link_manager->getTypeUri('node', 'page');
$this->assertEqual($link, 'http://example.com/rest/type/node/page');

View File

@ -5,14 +5,14 @@ namespace Drupal\Tests\hal\Kernel;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\field\Entity\FieldConfig;
use Drupal\hal\Encoder\JsonEncoder;
use Drupal\hal\LinkManager\LinkManager;
use Drupal\hal\LinkManager\RelationLinkManager;
use Drupal\hal\LinkManager\TypeLinkManager;
use Drupal\hal\Normalizer\ContentEntityNormalizer;
use Drupal\hal\Normalizer\EntityReferenceItemNormalizer;
use Drupal\hal\Normalizer\FieldItemNormalizer;
use Drupal\hal\Normalizer\FieldNormalizer;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\serialization\LinkManager\LinkManager;
use Drupal\serialization\LinkManager\RelationLinkManager;
use Drupal\serialization\LinkManager\TypeLinkManager;
use Drupal\serialization\EntityResolver\ChainEntityResolver;
use Drupal\serialization\EntityResolver\TargetIdResolver;
use Drupal\serialization\EntityResolver\UuidResolver;

View File

@ -11,22 +11,6 @@ services:
# @todo Remove this service in Drupal 9.0.0.
access_check.rest.csrf:
alias: access_check.header.csrf
# Link managers.
# @deprecated in Drupal 8.3.x and will be removed before 9.0.0. Use
# serialization.link_manager instead.
rest.link_manager:
parent: serialization.link_manager
class: Drupal\rest\LinkManager\LinkManager
# @deprecated in Drupal 8.3.x and will be removed before 9.0.0. Use
# serialization.link_manager.type instead.
rest.link_manager.type:
parent: serialization.link_manager.type
class: Drupal\rest\LinkManager\TypeLinkManager
# @deprecated in Drupal 8.3.x and will be removed before 9.0.0. Use
# serialization.link_manager.relation instead.
rest.link_manager.relation:
parent: serialization.link_manager.relation
class: Drupal\rest\LinkManager\RelationLinkManager
rest.resource_routes:
class: Drupal\rest\Routing\ResourceRoutes
arguments: ['@plugin.manager.rest', '@entity_type.manager', '@logger.channel.rest']

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\ConfigurableLinkManagerInterface as MovedConfigurableLinkManagerInterface;
use Drupal\hal\LinkManager\ConfigurableLinkManagerInterface as MovedConfigurableLinkManagerInterface;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
interface ConfigurableLinkManagerInterface extends MovedConfigurableLinkManagerInterface {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\LinkManager as MovedLinkManager;
use Drupal\hal\LinkManager\LinkManager as MovedLinkManager;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
class LinkManager extends MovedLinkManager implements LinkManagerInterface {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\LinkManagerBase as MovedLinkManagerBase;
use Drupal\hal\LinkManager\LinkManagerBase as MovedLinkManagerBase;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
abstract class LinkManagerBase extends MovedLinkManagerBase {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\LinkManagerInterface as MovedLinkManagerInterface;
use Drupal\hal\LinkManager\LinkManagerInterface as MovedLinkManagerInterface;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
interface LinkManagerInterface extends MovedLinkManagerInterface {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\RelationLinkManager as MovedLinkRelationManager;
use Drupal\hal\LinkManager\RelationLinkManager as MovedLinkRelationManager;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
class RelationLinkManager extends MovedLinkRelationManager implements RelationLinkManagerInterface {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\RelationLinkManagerInterface as MovedRelationLinkManagerInterface;
use Drupal\hal\LinkManager\RelationLinkManagerInterface as MovedRelationLinkManagerInterface;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
interface RelationLinkManagerInterface extends MovedRelationLinkManagerInterface {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\TypeLinkManager as MovedTypeLinkManager;
use Drupal\hal\LinkManager\TypeLinkManager as MovedTypeLinkManager;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
class TypeLinkManager extends MovedTypeLinkManager implements TypeLinkManagerInterface {}

View File

@ -2,10 +2,10 @@
namespace Drupal\rest\LinkManager;
use Drupal\serialization\LinkManager\TypeLinkManagerInterface as MovedTypeLinkManagerInterface;
use Drupal\hal\LinkManager\TypeLinkManagerInterface as MovedTypeLinkManagerInterface;
/**
* @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This has
* been moved to the serialization module. This exists solely for BC.
* been moved to the hal module. This exists solely for BC.
*/
interface TypeLinkManagerInterface extends MovedTypeLinkManagerInterface {}

View File

@ -0,0 +1,48 @@
<?php
namespace Drupal\rest;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\rest\LinkManager\LinkManager;
use Drupal\rest\LinkManager\RelationLinkManager;
use Drupal\rest\LinkManager\TypeLinkManager;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
/**
* Provides BC services.
*
* These services are not added via rest.services.yml because the service
* classes extend classes from the HAL module. They also have no use without
* that module.
*/
class RestServiceProvider implements ServiceProviderInterface {
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
$modules = $container->getParameter(('container.modules'));
if (isset($modules['hal'])) {
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
// Use hal.link_manager instead.
$service_definition = new DefinitionDecorator(new Reference('hal.link_manager'));
$service_definition->setClass(LinkManager::class);
$container->setDefinition('rest.link_manager', $service_definition);
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
// Use hal.link_manager.type instead.
$service_definition = new DefinitionDecorator(new Reference('hal.link_manager.type'));
$service_definition->setClass(TypeLinkManager::class);
$container->setDefinition('rest.link_manager.type', $service_definition);
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
// Use hal.link_manager.relation instead.
$service_definition = new DefinitionDecorator(new Reference('hal.link_manager.relation'));
$service_definition->setClass(RelationLinkManager::class);
$container->setDefinition('rest.link_manager.relation', $service_definition);
}
}
}

View File

@ -1,8 +0,0 @@
# Schema for the configuration files of the Serialization module.
serialization.settings:
type: config_object
label: 'Serialization settings'
mapping:
link_domain:
type: string
label: 'Domain of the relation'

View File

@ -2,7 +2,7 @@
/**
* @file
* Update functions for the serialization module.
* Update functions for the Serialization module.
*/
/**
@ -12,31 +12,9 @@
*/
/**
* Move 'link_domain' from 'rest.settings' to 'serialization.settings'.
* @see hal_update_8301()
*/
function serialization_update_8301() {
$config_factory = \Drupal::configFactory();
// The default value for the 'link_domain' key is `~`, which is the YAML
// equivalent of PHP's `NULL`. If the REST module is not installed, this is
// the value we will store in 'serialization.settings'.
$link_domain = NULL;
// But if the REST module was installed, we migrate its 'link_domain' setting,
// because we are actually moving that setting from 'rest.settings' to
// 'serialization.settings'.
$rest_settings = $config_factory->getEditable('rest.settings');
if ($rest_settings->getRawData() !== []) {
$link_domain = $rest_settings->get('link_domain');
// Remove the 'link_domain' setting from 'rest.settings'.
$rest_settings->clear('link_domain')
->save();
}
$serialization_settings = $config_factory->getEditable('serialization.settings');
$serialization_settings->set('link_domain', $link_domain);
$serialization_settings->save(TRUE);
}
function serialization_update_8301() {}
/**
* @} End of "defgroup updates-8.2.x-to-8.3.x".

View File

@ -89,13 +89,3 @@ services:
tags:
- { name: event_subscriber }
arguments: ['%serializer.formats%']
# Link managers.
serialization.link_manager:
class: Drupal\serialization\LinkManager\LinkManager
arguments: ['@serialization.link_manager.type', '@serialization.link_manager.relation']
serialization.link_manager.type:
class: Drupal\serialization\LinkManager\TypeLinkManager
arguments: ['@cache.default', '@module_handler', '@config.factory', '@request_stack', '@entity_type.bundle.info']
serialization.link_manager.relation:
class: Drupal\serialization\LinkManager\RelationLinkManager
arguments: ['@cache.default', '@entity.manager', '@module_handler', '@config.factory', '@request_stack']

View File

@ -1,46 +0,0 @@
<?php
/**
* @file
* Contains hook implementations for testing Serialization module.
*/
/**
* Implements hook_serialization_type_uri_alter().
*/
function serialization_test_serialization_type_uri_alter(&$uri, $context = array()) {
if (!empty($context['serialization_test'])) {
$uri = 'serialization_test_type';
}
}
/**
* Implements hook_rest_relation_uri_alter().
*/
function serialization_test_serialization_relation_uri_alter(&$uri, $context = array()) {
if (!empty($context['serialization_test'])) {
$uri = 'serialization_test_relation';
}
}
/**
* Implements hook_rest_type_uri_alter().
*
* @deprecated Kept only for BC test coverage, see \Drupal\Tests\serialization\Kernel\SerializationLinkManagerTest::testGetTypeUri().
*/
function serialization_test_rest_type_uri_alter(&$uri, $context = array()) {
if (!empty($context['rest_test'])) {
$uri = 'rest_test_type';
}
}
/**
* Implements hook_rest_relation_uri_alter().
*
* @deprecated Kept only for BC test coverage, see \Drupal\Tests\serialization\Kernel\SerializationLinkManagerTest::testGetRelationUri().
*/
function serialization_test_rest_relation_uri_alter(&$uri, $context = array()) {
if (!empty($context['rest_test'])) {
$uri = 'rest_test_relation';
}
}

View File

@ -1,47 +0,0 @@
<?php
namespace Drupal\Tests\serialization\Functional\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* Tests that 'serialization.settings' is created, to store 'link_domain'.
*
* @see https://www.drupal.org/node/2758897
*
* @group serialization
*/
class CreateSerializationSettingsForLinkDomainUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['serialization'];
/**
* {@inheritdoc}
*/
public function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.serialization-serialization_update_8301.php',
];
}
/**
* Tests serialization_update_8301().
*/
public function testSerializationSettingsCreated() {
// Make sure we have the expected values before the update.
$serialization_settings = $this->config('serialization.settings');
$this->assertIdentical([], $serialization_settings->getRawData());
$this->runUpdates();
// Make sure we have the expected values after the update.
$serialization_settings = \Drupal::configFactory()->get('serialization.settings');
$this->assertTrue(array_key_exists('link_domain', $serialization_settings->getRawData()));
$this->assertIdentical(NULL, $serialization_settings->getRawData()['link_domain']);
}
}

View File

@ -1,53 +0,0 @@
<?php
namespace Drupal\Tests\serialization\Functional\Update;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* 'link_domain' is migrated from 'rest.settings' to 'serialization.settings'.
*
* @see https://www.drupal.org/node/2758897
*
* @group serialization
*/
class MigrateLinkDomainSettingFromRestToSerializationUpdateTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['rest', 'serialization'];
/**
* {@inheritdoc}
*/
public function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
__DIR__ . '/../../../fixtures/update/drupal-8.serialization-serialization_update_8301.php',
__DIR__ . '/../../../fixtures/update/drupal-8.rest-serialization_update_8301.php',
];
}
/**
* Tests serialization_update_8301().
*/
public function testLinkDomainMigratedFromRestSettingsToSerializationSettings() {
// Make sure we have the expected values before the update.
$serialization_settings = $this->config('serialization.settings');
$this->assertIdentical([], $serialization_settings->getRawData());
$rest_settings = $this->config('rest.settings');
$this->assertTrue(array_key_exists('link_domain', $rest_settings->getRawData()));
$this->assertIdentical('http://example.com', $rest_settings->getRawData()['link_domain']);
$this->runUpdates();
// Make sure we have the expected values after the update.
$serialization_settings = \Drupal::configFactory()->get('serialization.settings');
$this->assertTrue(array_key_exists('link_domain', $serialization_settings->getRawData()));
$this->assertIdentical('http://example.com', $serialization_settings->getRawData()['link_domain']);
$rest_settings = $this->config('rest.settings');
$this->assertFalse(array_key_exists('link_domain', $rest_settings->getRawData()));
}
}