SerializerDecorator.php 3.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
<?php

namespace Drupal\jsonapi_extras;

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Drupal\jsonapi\Serializer\Serializer;

/**
 * A decorated JSON API serializer, with lazily initialized fallback serializer.
 */
class SerializerDecorator implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface {

  /**
   * The decorated JSON API serializer service.
   *
   * @var \Drupal\jsonapi\Serializer\Serializer
   */
  protected $decoratedSerializer;

  /**
   * Whether the lazy dependency has been initialized.
   *
   * @var bool
   */
  protected $isInitialized = FALSE;

  /**
   * Constructs a SerializerDecorator.
   *
   * @param \Drupal\jsonapi\Serializer\Serializer $serializer
   *   The decorated JSON API serializer.
   */
  public function __construct(Serializer $serializer) {
    $this->decoratedSerializer = $serializer;
  }

  /**
   * Lazily initializes the fallback serializer for the JSON API serializer.
   *
   * Breaks circular dependency.
   */
  protected function lazilyInitialize() {
    if (!$this->isInitialized) {
      $core_serializer = \Drupal::service('serializer');
      $this->decoratedSerializer->setFallbackNormalizer($core_serializer);
      $this->isInitialized = TRUE;
    }
  }

  /**
   * Relays a method call to the decorated service.
   *
   * @param string $method_name
   *   The method to invoke on the decorated serializer.
   * @param array $args
   *   The arguments to pass to the invoked method on the decorated serializer.
   *
   * @return mixed
   *   The return value.
   */
  protected function relay($method_name, array $args) {
    $this->lazilyInitialize();
    return call_user_func_array([$this->decoratedSerializer, $method_name], $args);
  }

  /**
   * {@inheritdoc}
   */
  public function decode($data, $format, array $context = []) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function deserialize($data, $type, $format, array $context = []) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data, $format, array $context = []) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function serialize($data, $format, array $context = []) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = NULL) {
    return $this->relay(__FUNCTION__, func_get_args());
  }

}