diff --git a/core/core.services.yml b/core/core.services.yml index b2f549d867..cd647713cb 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -436,8 +436,6 @@ services: class: Drupal\Component\Serialization\PhpSerialize serialization.yaml: class: Drupal\Component\Serialization\Yaml - calls: - - [setTranslation, ['@string_translation']] settings: class: Drupal\Core\Site\Settings diff --git a/core/lib/Drupal/Component/Serialization/TaggedSerializationInterface.php b/core/lib/Drupal/Component/Serialization/TaggedSerializationInterface.php new file mode 100644 index 0000000000..361ea65bf5 --- /dev/null +++ b/core/lib/Drupal/Component/Serialization/TaggedSerializationInterface.php @@ -0,0 +1,28 @@ + static::class . '::applyBooleanCallbacks', + ]; } /** diff --git a/core/lib/Drupal/Component/Serialization/YamlSymfony.php b/core/lib/Drupal/Component/Serialization/YamlSymfony.php index 7d864a8b6e..2ea01d4ba2 100644 --- a/core/lib/Drupal/Component/Serialization/YamlSymfony.php +++ b/core/lib/Drupal/Component/Serialization/YamlSymfony.php @@ -11,9 +11,9 @@ /** * Default serialization for YAML using the Symfony component. */ -class YamlSymfony implements TranslatableSerializationInterface { +class YamlSymfony implements TaggedSerializationInterface { - use YamlTagMappingTrait; + use TaggedSerializationTrait; /** * {@inheritdoc} @@ -39,14 +39,22 @@ public static function decode($raw) { // 'foo: bar' with no newline will fail to parse otherwise. $data = $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::PARSE_CUSTOM_TAGS); - // Support tagged values. + $is_array = is_array($data); + if (!$is_array) { + $data = [$data]; + } + + // Support Symfony 3.3 TaggedValue objects. array_walk_recursive($data, function (&$value) { if ($value instanceof TaggedValue) { - $value = static::executeTagCallback('!' . $value->getTag(), (array) $value->getValue()); + $callbacks = static::getTagCallbacks(); + $args = (array) $value->getValue(); + $tag = $value->getTag(); + return isset($callbacks[$tag]) ? $callbacks[$tag](...$args) : $args; } }); - return $data; + return $is_array ? $data : reset($data); } catch (\Exception $e) { throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e); diff --git a/core/lib/Drupal/Component/Serialization/YamlTagMappingTrait.php b/core/lib/Drupal/Component/Serialization/YamlTagMappingTrait.php deleted file mode 100644 index 6c97f2c4d3..0000000000 --- a/core/lib/Drupal/Component/Serialization/YamlTagMappingTrait.php +++ /dev/null @@ -1,58 +0,0 @@ - static::class . '::applyTranslationCallback' - ]; - } - - /** - * Callback for applying the !translate tag. - * - * @param mixed ...$args - * The arguments to pass to the Translation manager. - * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup - * A new TranslatableMarkup object. - */ - public static function applyTranslationCallback(...$args) { - return static::getTranslation()->translate(...$args); - } - -} diff --git a/core/lib/Drupal/Core/Serialization/Yaml.php b/core/lib/Drupal/Core/Serialization/Yaml.php index 566f8cad4c..1b089249cf 100644 --- a/core/lib/Drupal/Core/Serialization/Yaml.php +++ b/core/lib/Drupal/Core/Serialization/Yaml.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Serialization; +use Drupal\Component\Serialization\TaggedSerializationInterface; use Drupal\Core\Site\Settings; use Drupal\Component\Serialization\Yaml as ComponentYaml; @@ -12,6 +13,13 @@ */ class Yaml extends ComponentYaml { + /** + * Translation Manager. + * + * @var \Drupal\Core\StringTranslation\TranslationInterface + */ + protected static $translation; + /** * {@inheritdoc} */ @@ -21,8 +29,51 @@ protected static function getSerializer() { $class = Settings::get('yaml_parser_class')) { static::$serializer = $class; + + // Merge the tag callbacks from this proxy to the chosen serializer. + if (static::$serializer instanceof TaggedSerializationInterface) { + static::$serializer::setTagCallbacks(array_merge( + static::$serializer::getTagCallbacks(), + static::getTagCallbacks() + )); + } } return parent::getSerializer(); } + /** + * Retrieves the Translation Manager. + * + * @return \Drupal\Core\StringTranslation\TranslationInterface + * The Translation Manager. + */ + protected static function getTranslation() { + if (!isset(static::$translation)) { + static::$translation = \Drupal::translation(); + } + return static::$translation; + } + + /** + * {@inheritdoc} + */ + public static function getTagCallbacks() { + return [ + '!translate' => static::class . '::applyTranslateCallback', + ]; + } + + /** + * Callback for applying the !translate tag. + * + * @param mixed ... + * The arguments to pass to the Translation manager. + * + * @return \Drupal\Core\StringTranslation\TranslatableMarkup + * A new TranslatableMarkup object. + */ + public static function applyTranslateCallback(...$args) { + return static::getTranslation()->translate(...$args); + } + }