Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0
This commit is contained in:
parent
2f563ab520
commit
f1c8716f57
1732 changed files with 52334 additions and 11780 deletions
|
@ -64,3 +64,13 @@ services:
|
|||
class: Drupal\serialization\EntityResolver\TargetIdResolver
|
||||
tags:
|
||||
- { name: entity_resolver}
|
||||
serialization.exception.default:
|
||||
class: Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
arguments: ['@serializer', '%serializer.formats%']
|
||||
serialization.user_route_alter_subscriber:
|
||||
class: Drupal\serialization\EventSubscriber\UserRouteAlterSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
arguments: ['@serializer', '%serializer.formats%']
|
||||
|
|
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\EventSubscriber;
|
||||
|
||||
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Handles default error responses in serialization formats.
|
||||
*/
|
||||
class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
|
||||
|
||||
/**
|
||||
* The serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The available serialization formats.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $serializerFormats = [];
|
||||
|
||||
/**
|
||||
* DefaultExceptionSubscriber constructor.
|
||||
*
|
||||
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
|
||||
* The serializer service.
|
||||
* @param array $serializer_formats
|
||||
* The available serialization formats.
|
||||
*/
|
||||
public function __construct(SerializerInterface $serializer, array $serializer_formats) {
|
||||
$this->serializer = $serializer;
|
||||
$this->serializerFormats = $serializer_formats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getHandledFormats() {
|
||||
return $this->serializerFormats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static function getPriority() {
|
||||
// This will fire after the most common HTML handler, since HTML requests
|
||||
// are still more common than HTTP requests.
|
||||
return -75;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 400 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on400(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 403 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on403(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 404 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on404(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 405 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on405(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_METHOD_NOT_ALLOWED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 406 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on406(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_NOT_ACCEPTABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 422 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on422(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a 429 error for HTTP.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function on429(GetResponseForExceptionEvent $event) {
|
||||
$this->setEventResponse($event, Response::HTTP_TOO_MANY_REQUESTS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Response for the exception event.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The current exception event.
|
||||
* @param int $status
|
||||
* The HTTP status code to set for the response.
|
||||
*/
|
||||
protected function setEventResponse(GetResponseForExceptionEvent $event, $status) {
|
||||
$format = $event->getRequest()->getRequestFormat();
|
||||
$content = ['message' => $event->getException()->getMessage()];
|
||||
$encoded_content = $this->serializer->serialize($content, $format);
|
||||
$response = new Response($encoded_content, $status);
|
||||
$event->setResponse($response);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Routing\RouteBuildEvent;
|
||||
use Drupal\Core\Routing\RoutingEvents;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
/**
|
||||
* Alters user authentication routes to support additional serialization formats.
|
||||
*/
|
||||
class UserRouteAlterSubscriber implements EventSubscriberInterface {
|
||||
|
||||
/**
|
||||
* The serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The available serialization formats.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $serializerFormats = [];
|
||||
|
||||
/**
|
||||
* UserRouteAlterSubscriber constructor.
|
||||
*
|
||||
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
|
||||
* The serializer service.
|
||||
* @param array $serializer_formats
|
||||
* The available serializer formats.
|
||||
*/
|
||||
public function __construct(SerializerInterface $serializer, array $serializer_formats) {
|
||||
$this->serializer = $serializer;
|
||||
$this->serializerFormats = $serializer_formats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
$events[RoutingEvents::ALTER][] = 'onRoutingAlterAddFormats';
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds supported formats to the user authentication HTTP routes.
|
||||
*
|
||||
* @param \Drupal\Core\Routing\RouteBuildEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function onRoutingAlterAddFormats(RouteBuildEvent $event) {
|
||||
$route_names = [
|
||||
'user.login_status.http',
|
||||
'user.login.http',
|
||||
'user.logout.http',
|
||||
];
|
||||
$routes = $event->getRouteCollection();
|
||||
foreach ($route_names as $route_name) {
|
||||
if ($route = $routes->get($route_name)) {
|
||||
$formats = explode('|', $route->getRequirement('_format'));
|
||||
$formats = array_unique($formats + $this->serializerFormats);
|
||||
$route->setRequirement('_format', implode('|', $formats));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -72,6 +72,6 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
|
|||
}
|
||||
|
||||
return in_array($format, (array) $this->format);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -40,10 +40,17 @@ class RegisterSerializationClassesCompilerPass implements CompilerPassInterface
|
|||
|
||||
// Find all serialization formats known.
|
||||
$formats = array();
|
||||
foreach ($container->findTaggedServiceIds('encoder') as $attributes) {
|
||||
$formats[] = $attributes[0]['format'];
|
||||
$format_providers = [];
|
||||
foreach ($container->findTaggedServiceIds('encoder') as $service_id => $attributes) {
|
||||
$format = $attributes[0]['format'];
|
||||
$formats[] = $format;
|
||||
|
||||
if ($provider_tag = $container->getDefinition($service_id)->getTag('_provider')) {
|
||||
$format_providers[$format] = $provider_tag[0]['provider'];
|
||||
}
|
||||
}
|
||||
$container->setParameter('serializer.formats', $formats);
|
||||
$container->setParameter('serializer.format_providers', $format_providers);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,46 +2,12 @@
|
|||
|
||||
namespace Drupal\serialization\Tests;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\serialization\Kernel\NormalizerTestBase as SerializationNormalizerTestBase;
|
||||
|
||||
abstract class NormalizerTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('entity_test_mulrev');
|
||||
$this->installEntitySchema('user');
|
||||
$this->installConfig(array('field'));
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
\Drupal::moduleHandler()->invoke('rest', 'install');
|
||||
|
||||
// Auto-create a field for testing.
|
||||
FieldStorageConfig::create(array(
|
||||
'entity_type' => 'entity_test_mulrev',
|
||||
'field_name' => 'field_test_text',
|
||||
'type' => 'text',
|
||||
'cardinality' => 1,
|
||||
'translatable' => FALSE,
|
||||
))->save();
|
||||
FieldConfig::create(array(
|
||||
'entity_type' => 'entity_test_mulrev',
|
||||
'field_name' => 'field_test_text',
|
||||
'bundle' => 'entity_test_mulrev',
|
||||
'label' => 'Test text-field',
|
||||
'widget' => array(
|
||||
'type' => 'text_textfield',
|
||||
'weight' => 0,
|
||||
),
|
||||
))->save();
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Helper base class to set up some test fields for serialization testing.
|
||||
*
|
||||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use \Drupal\Tests\serialization\Kernel\NormalizerTestBase instead.
|
||||
*/
|
||||
abstract class NormalizerTestBase extends SerializationNormalizerTestBase { }
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Tests;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\serialization\RegisterSerializationClassesCompilerPass;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\serialization\RegisterSerializationClassesCompilerPass
|
||||
* @group serialization
|
||||
*/
|
||||
class RegisterSerializationClassesCompilerPassTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* @covers ::process
|
||||
*/
|
||||
public function testEncoders() {
|
||||
$container = new ContainerBuilder();
|
||||
$container->setDefinition('serializer', new Definition(Serializer::class, [[], []]));
|
||||
|
||||
$definition = new Definition('TestClass');
|
||||
$definition->addTag('encoder', ['format' => 'xml']);
|
||||
$definition->addTag('_provider', ['provider' => 'test_provider_a']);
|
||||
$container->setDefinition('encoder_1', $definition);
|
||||
|
||||
$definition = new Definition('TestClass');
|
||||
$definition->addTag('encoder', ['format' => 'json']);
|
||||
$definition->addTag('_provider', ['provider' => 'test_provider_a']);
|
||||
$container->setDefinition('encoder_2', $definition);
|
||||
|
||||
$definition = new Definition('TestClass');
|
||||
$definition->addTag('encoder', ['format' => 'hal_json']);
|
||||
$definition->addTag('_provider', ['provider' => 'test_provider_b']);
|
||||
$container->setDefinition('encoder_3', $definition);
|
||||
|
||||
$compiler_pass = new RegisterSerializationClassesCompilerPass();
|
||||
$compiler_pass->process($container);
|
||||
|
||||
$this->assertEquals(['xml', 'json', 'hal_json'], $container->getParameter('serializer.formats'));
|
||||
$this->assertEquals(['xml' => 'test_provider_a', 'json' => 'test_provider_a', 'hal_json' => 'test_provider_b'], $container->getParameter('serializer.format_providers'));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Tests;
|
||||
namespace Drupal\Tests\serialization\Kernel;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Tests;
|
||||
namespace Drupal\Tests\serialization\Kernel;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\entity_test\Entity\EntityTestMulRev;
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\serialization\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Helper base class to set up some test fields for serialization testing.
|
||||
*/
|
||||
abstract class NormalizerTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('entity_test_mulrev');
|
||||
$this->installEntitySchema('user');
|
||||
$this->installConfig(array('field'));
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
\Drupal::moduleHandler()->invoke('rest', 'install');
|
||||
|
||||
// Auto-create a field for testing.
|
||||
FieldStorageConfig::create(array(
|
||||
'entity_type' => 'entity_test_mulrev',
|
||||
'field_name' => 'field_test_text',
|
||||
'type' => 'text',
|
||||
'cardinality' => 1,
|
||||
'translatable' => FALSE,
|
||||
))->save();
|
||||
FieldConfig::create(array(
|
||||
'entity_type' => 'entity_test_mulrev',
|
||||
'field_name' => 'field_test_text',
|
||||
'bundle' => 'entity_test_mulrev',
|
||||
'label' => 'Test text-field',
|
||||
'widget' => array(
|
||||
'type' => 'text_textfield',
|
||||
'weight' => 0,
|
||||
),
|
||||
))->save();
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue