You are here

abstract class ConfigurationHandler in Configuration Management 7.3

Hierarchy

Expanded class hierarchy of ConfigurationHandler

14 files declare their use of ConfigurationHandler
ContentTypeConfigurationHandler.php in src/Handlers/ContentTypeConfigurationHandler.php
EntityConfigurationHandler.php in src/Handlers/EntityConfigurationHandler.php
FieldBaseConfigurationHandler.php in src/Handlers/FieldBaseConfigurationHandler.php
FieldInstanceConfigurationHandler.php in src/Handlers/FieldInstanceConfigurationHandler.php
ImageStyleConfigurationHandler.php in src/Handlers/ImageStyleConfigurationHandler.php

... See full list

File

src/Handlers/ConfigurationHandler.php, line 12

Namespace

Configuration\Handlers
View source
abstract class ConfigurationHandler implements ConfigurationHandlerInterface {

  // Global handler that coordinates all the configuration handlers
  protected $configuration_manager;

  // The current configuration type this instance is handling.
  protected $type;
  public function __construct($type, $configuration_manager) {
    $this->type = $type;
    $this->configuration_manager = $configuration_manager;
    $this
      ->registerProcessors();
  }
  protected function registerProcessors() {
  }
  public function getType() {
    return $this->type;
  }
  public static function getSupportedTypes() {
    return array();
  }
  public abstract function getIdentifiers();
  public abstract function loadFromDatabase($identifier);
  public abstract function writeToDatabase(Configuration $configuration);
  public abstract function removeFromDatabase(Configuration $configuration);
  public static function getSubscribedEvents() {
    return array();
  }
  protected function getInternalId($identifier) {
    return substr($identifier, strpos($identifier, '.') + 1);
  }
  protected function getTypeFromId($identifier) {
    return substr($identifier, 0, strpos($identifier, '.'));
  }
  protected function triggerEvent($event_name, Configuration $configuration, $settings = array(), $append_configuration_type = TRUE) {
    $event = new ConfigurationCRUDEvent($configuration, $settings);
    if ($append_configuration_type) {
      $event_name = $event_name . '.' . $this
        ->getType();
    }
    $this->configuration_manager
      ->dispatchEvent($event_name, $event);
    return $event;
  }

  /**
   * Generates the JSON representation of this configuration.
   */
  public function export(Configuration $configuration, $format = 'json') {
    switch ($format) {
      case 'json':
        return $this
          ->exportToJson($configuration);
        break;
      default:
        return '';
        break;
    }
  }
  protected function exportToJson(Configuration $configuration) {
    $object = $configuration
      ->toObject();
    $jsonPretty = new JsonPretty();
    return $jsonPretty
      ->prettify($object, NULL, '  ');
  }
  public function getExportPath(Configuration $configuration) {
    $pattern = $this->configuration_manager
      ->settings()
      ->get('export.default_pattern');
    $overriden_patterns = $this->configuration_manager
      ->settings()
      ->get('export.overriden_patterns');
    foreach ($overriden_patterns as $current_pattern => $replacement) {
      if ($current_pattern == $this
        ->getType() . '.*') {
        $pattern = $replacement;
        break;
      }
      elseif ($current_pattern == $configuration
        ->getIdentifier()) {
        $pattern = $replacement;
        break;
      }
    }
    $tokens = array(
      '[group]' => $configuration
        ->getGroup(),
      '[type]' => $this
        ->getType(),
    );
    $path = strtr($pattern, $tokens);
    $path = rtrim($path, '/');
    $path = ltrim($path, '/');
    return $path . '/';
  }
  public function import($path, $format = "json") {
    $import_path = drupal_realpath('public://' . $this->configuration_manager
      ->settings()
      ->get('import.path'));
    $directories = array(
      $import_path,
    );
    $locator = new FileLocator($directories);
    $config_full_path = $locator
      ->locate($path);
    if (!empty($config_full_path)) {
      $file_content = file_get_contents($config_full_path);
      switch ($format) {
        case 'json':
          return $this
            ->importFromJson($file_content);
          break;
        default:
          return '';
          break;
      }
    }
  }
  protected function jsonAsArray() {
    return FALSE;
  }
  public function importFromJson($file_content) {
    $object = new \stdClass();
    if ($this
      ->jsonAsArray()) {
      $object = $this
        ->importFromJsonAsArray($file_content);
    }
    else {
      $object = json_decode($file_content);
    }
    $configuration = new Configuration();
    $configuration
      ->fromObject($object);
    return $configuration;
  }
  protected function importFromJsonAsArray($file_content) {

    // Load the view as an array, it will be converted to proper views objects later.
    $array = json_decode($file_content, TRUE);
    $object = new \stdClass();
    $object->identifier = $array['identifier'];
    $object->notes = $array['notes'];
    $object->tags = $array['tags'];
    $object->dependencies = $array['dependencies'];
    $object->parts = $array['parts'];
    $object->modules = $array['modules'];
    $object->data = $array['data'];
    unset($array);
    return $object;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurationHandler::$configuration_manager protected property
ConfigurationHandler::$type protected property
ConfigurationHandler::export public function Generates the JSON representation of this configuration.
ConfigurationHandler::exportToJson protected function
ConfigurationHandler::getExportPath public function
ConfigurationHandler::getIdentifiers abstract public function Returns the configuration identifiers handled by this instance. Overrides ConfigurationHandlerInterface::getIdentifiers 14
ConfigurationHandler::getInternalId protected function
ConfigurationHandler::getSubscribedEvents public static function 11
ConfigurationHandler::getSupportedTypes public static function Returns the types of configurations that this class can handle. Overrides ConfigurationHandlerInterface::getSupportedTypes 14
ConfigurationHandler::getType public function
ConfigurationHandler::getTypeFromId protected function
ConfigurationHandler::import public function
ConfigurationHandler::importFromJson public function
ConfigurationHandler::importFromJsonAsArray protected function 1
ConfigurationHandler::jsonAsArray protected function 9
ConfigurationHandler::loadFromDatabase abstract public function Loads the configuration from the database. Overrides ConfigurationHandlerInterface::loadFromDatabase 14
ConfigurationHandler::registerProcessors protected function 2
ConfigurationHandler::removeFromDatabase abstract public function Deletes a configuration from the database. Overrides ConfigurationHandlerInterface::removeFromDatabase 14
ConfigurationHandler::triggerEvent protected function
ConfigurationHandler::writeToDatabase abstract public function Saves the given configuration into the database. Overrides ConfigurationHandlerInterface::writeToDatabase 14
ConfigurationHandler::__construct public function 1