You are here

class ConfigName in Update helper 8

Same name and namespace in other branches
  1. 2.x src/ConfigName.php \Drupal\update_helper\ConfigName

Configuration name class for easier handling of configuration references.

@package Drupal\update_helper

Hierarchy

Expanded class hierarchy of ConfigName

1 file declares its use of ConfigName
ConfigNameTest.php in tests/src/Kernel/ConfigNameTest.php

File

src/ConfigName.php, line 10

Namespace

Drupal\update_helper
View source
class ConfigName {
  const SYSTEM_SIMPLE_CONFIG = 'system.simple';

  /**
   * Config type.
   *
   * @var string
   */
  protected $type;

  /**
   * Config name.
   *
   * @var string
   */
  protected $name;

  /**
   * Entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Create ConfigName instance from full configuration name.
   *
   * @param string $full_config_name
   *   Full config name.
   *
   * @return ConfigName
   *   Return instance of ConfigName.
   */
  public static function createByFullName($full_config_name) {
    $config_name = new static();
    $configPair = $config_name
      ->parseFullName($full_config_name);
    $config_name->type = $configPair['type'];
    $config_name->name = $configPair['name'];
    return $config_name;
  }

  /**
   * Create ConfigName instance from configuration type and name.
   *
   * @param string $config_type
   *   Config type.
   * @param string $config_name
   *   Config name.
   *
   * @return ConfigName
   *   Return instance of ConfigName.
   */
  public static function createByTypeName($config_type, $config_name) {
    $config_name_instance = new static();
    $config_name_instance->type = $config_type;
    $config_name_instance->name = $config_name;
    return $config_name_instance;
  }

  /**
   * Parse full config name and create array with config type and name.
   *
   * @param string $full_config_name
   *   Full config name.
   *
   * @return array
   *   Returns array with config type and name.
   */
  protected function parseFullName($full_config_name) {
    $result = [
      'type' => static::SYSTEM_SIMPLE_CONFIG,
      'name' => $full_config_name,
    ];
    $prefix = static::SYSTEM_SIMPLE_CONFIG . '.';
    if (strpos($full_config_name, $prefix)) {
      $result['name'] = substr($full_config_name, strlen($prefix));
    }
    else {
      foreach ($this
        ->entityTypeManager()
        ->getDefinitions() as $entityType => $definition) {
        if ($definition
          ->entityClassImplements('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface')) {
          $prefix = $definition
            ->getConfigPrefix() . '.';
          if (strpos($full_config_name, $prefix) === 0) {
            $result['type'] = $entityType;
            $result['name'] = substr($full_config_name, strlen($prefix));
          }
        }
      }
    }
    return $result;
  }

  /**
   * Create full configuration name from config type and name.
   *
   * @param string $type
   *   Config type.
   * @param string $name
   *   Config name.
   *
   * @return string
   *   Returns full configuration name.
   */
  protected function generateFullName($type, $name) {
    if ($type == 'system.simple' || !$type) {
      return $name;
    }
    $definition = $this
      ->entityTypeManager()
      ->getDefinition($type);
    $prefix = $definition
      ->getConfigPrefix() . '.';
    return $prefix . $name;
  }

  /**
   * Retrieves the entity manager service.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The entity manager service.
   */
  protected function entityTypeManager() {
    if (!$this->entityTypeManager) {
      $this->entityTypeManager = \Drupal::service('entity_type.manager');
    }
    return $this->entityTypeManager;
  }

  /**
   * Get full configuration name.
   *
   * @return string
   *   Returns full configuration name.
   */
  public function getFullName() {
    return $this
      ->generateFullName($this->type, $this->name);
  }

  /**
   * Get configuration type.
   *
   * @return string
   *   Config type.
   */
  public function getType() {
    return $this->type;
  }

  /**
   * Get configuration name.
   *
   * @return string
   *   Config name.
   */
  public function getName() {
    return $this->name;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigName::$entityTypeManager protected property Entity manager service.
ConfigName::$name protected property Config name.
ConfigName::$type protected property Config type.
ConfigName::createByFullName public static function Create ConfigName instance from full configuration name.
ConfigName::createByTypeName public static function Create ConfigName instance from configuration type and name.
ConfigName::entityTypeManager protected function Retrieves the entity manager service.
ConfigName::generateFullName protected function Create full configuration name from config type and name.
ConfigName::getFullName public function Get full configuration name.
ConfigName::getName public function Get configuration name.
ConfigName::getType public function Get configuration type.
ConfigName::parseFullName protected function Parse full config name and create array with config type and name.
ConfigName::SYSTEM_SIMPLE_CONFIG constant