You are here

private function YamlFileLoader::parseDefaults in Drupal 10

Parameters

array $content:

string $file:

Return value

array

Throws

InvalidArgumentException

File

core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php, line 146

Class

YamlFileLoader
YamlFileLoader loads YAML files service definitions.

Namespace

Drupal\Core\DependencyInjection

Code

private function parseDefaults(array &$content, string $file) : array {
  if (!\array_key_exists('_defaults', $content['services'])) {
    return [];
  }
  $defaults = $content['services']['_defaults'];
  unset($content['services']['_defaults']);
  if (!\is_array($defaults)) {
    throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', \gettype($defaults), $file));
  }
  foreach ($defaults as $key => $default) {
    if (!isset(self::DEFAULTS_KEYWORDS[$key])) {
      throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::DEFAULTS_KEYWORDS)));
    }
  }
  if (isset($defaults['tags'])) {
    if (!\is_array($tags = $defaults['tags'])) {
      throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in "%s". Check your YAML syntax.', $file));
    }
    foreach ($tags as $tag) {
      if (!\is_array($tag)) {
        $tag = [
          'name' => $tag,
        ];
      }
      if (!isset($tag['name'])) {
        throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".', $file));
      }
      $name = $tag['name'];
      unset($tag['name']);
      if (!\is_string($name) || '' === $name) {
        throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in "%s".', $file));
      }
      foreach ($tag as $attribute => $value) {
        if (!is_scalar($value) && null !== $value) {
          throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in "%s". Check your YAML syntax.', $name, $attribute, $file));
        }
      }
    }
  }
  return $defaults;
}