StorableConfigBase.php 6.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\Core\Config\StorableConfigBase.
 */

namespace Drupal\Core\Config;

use Drupal\Component\Utility\String;
11
use Drupal\Core\Config\Schema\Ignore;
12 13 14 15
use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\TypedData\PrimitiveInterface;
use Drupal\Core\TypedData\Type\FloatInterface;
use Drupal\Core\TypedData\Type\IntegerInterface;
16
use Drupal\Core\Config\Schema\Undefined;
17

18 19 20 21 22 23 24 25 26 27 28 29
/**
 * Provides a base class for configuration objects with storage support.
 *
 * Encapsulates all capabilities needed for configuration handling for a
 * specific configuration object, including storage and data type casting.
 *
 * The default implementation in \Drupal\Core\Config\Config adds support for
 * runtime overrides. Extend from StorableConfigBase directly to manage
 * configuration with a storage backend that does not support overrides.
 *
 * @see \Drupal\Core\Config\Config
 */
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
abstract class StorableConfigBase extends ConfigBase {

  /**
   * The storage used to load and save this configuration object.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $storage;

  /**
   * The config schema wrapper object for this configuration object.
   *
   * @var \Drupal\Core\Config\Schema\Element
   */
  protected $schemaWrapper;

  /**
   * The typed config manager.
   *
49
   * @var \Drupal\Core\Config\TypedConfigManagerInterface
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
   */
  protected $typedConfigManager;

  /**
   * Whether the configuration object is new or has been saved to the storage.
   *
   * @var bool
   */
  protected $isNew = TRUE;

  /**
   * The data of the configuration object.
   *
   * @var array
   */
  protected $originalData = array();

  /**
   * Saves the configuration object.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  abstract public function save();

  /**
   * Deletes the configuration object.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  abstract public function delete();

  /**
   * Initializes a configuration object with pre-loaded data.
   *
   * @param array $data
   *   Array of loaded data for this configuration object.
   *
   * @return $this
   *   The configuration object.
   */
  public function initWithData(array $data) {
    $this->isNew = FALSE;
    $this->setData($data);
    $this->originalData = $this->data;
    return $this;
  }

  /**
   * Returns whether this configuration object is new.
   *
   * @return bool
   *   TRUE if this configuration object does not exist in storage.
   */
  public function isNew() {
    return $this->isNew;
  }

  /**
   * Retrieves the storage used to load and save this configuration object.
   *
   * @return \Drupal\Core\Config\StorageInterface
   *   The configuration storage object.
   */
  public function getStorage() {
    return $this->storage;
  }

  /**
   * Gets the schema wrapper for the whole configuration object.
   *
   * The schema wrapper is dependent on the configuration name and the whole
   * data structure, so if the name or the data changes in any way, the wrapper
   * should be reset.
   *
   * @return \Drupal\Core\Config\Schema\Element
   */
  protected function getSchemaWrapper() {
    if (!isset($this->schemaWrapper)) {
      $definition = $this->typedConfigManager->getDefinition($this->name);
      $this->schemaWrapper = $this->typedConfigManager->create($definition, $this->data);
    }
    return $this->schemaWrapper;
  }

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * Validate the values are allowed data types.
   *
   * @throws UnsupportedDataTypeConfigException
   *   If there is any invalid value.
   */
  protected function validateValue($key, $value) {
    // Minimal validation. Should not try to serialize resources or non-arrays.
    if (is_array($value)) {
      foreach ($value as $nested_value_key => $nested_value) {
        $this->validateValue($key . '.' . $nested_value_key, $nested_value);
      }
    }
    elseif ($value !== NULL && !is_scalar($value)) {
      throw new UnsupportedDataTypeConfigException(String::format('Invalid data type for config element @name:@key', array(
        '@name' => $this->getName(),
        '@key' => $key,
      )));
    }
  }

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  /**
   * Casts the value to correct data type using the configuration schema.
   *
   * @param string $key
   *   A string that maps to a key within the configuration data.
   * @param string $value
   *   Value to associate with the key.
   *
   * @return mixed
   *   The value cast to the type indicated in the schema.
   *
   * @throws \Drupal\Core\Config\UnsupportedDataTypeConfigException
   *   Exception on unsupported/undefined data type deducted.
   */
  protected function castValue($key, $value) {
172 173 174
    $element = FALSE;
    try {
      $element = $this->getSchemaWrapper()->get($key);
175
    }
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    catch (SchemaIncompleteException $e) {
      // @todo Consider making schema handling more strict by throwing
      // SchemaIncompleteException for all incomplete schema conditions *and*
      // throwing it forward. See https://drupal.org/node/2183983.
      // Until then, we need to handle the Undefined case below.
    }
    // Do not cast value if it is unknown or defined to be ignored.
    if ($element && ($element instanceof Undefined || $element instanceof Ignore)) {
      return $value;
    }
    if ((is_scalar($value) || $value === NULL)) {
      if ($element && $element instanceof PrimitiveInterface) {
        // Special handling for integers and floats since the configuration
        // system is primarily concerned with saving values from the Form API
        // we have to special case the meaning of an empty string for numeric
        // types. In PHP this would be casted to a 0 but for the purposes of
        // configuration we need to treat this as a NULL.
        $empty_value =  $value === '' && ($element instanceof IntegerInterface || $element instanceof FloatInterface);

        if ($value === NULL || $empty_value) {
          $value = NULL;
197 198
        }
        else {
199
          $value = $element->getCastedValue();
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        }
      }
    }
    else {
      // Throw exception on any non-scalar or non-array value.
      if (!is_array($value)) {
        throw new UnsupportedDataTypeConfigException(String::format('Invalid data type for config element @name:@key', array(
          '@name' => $this->getName(),
          '@key' => $key,
        )));
      }
      // Recurse into any nested keys.
      foreach ($value as $nested_value_key => $nested_value) {
        $value[$nested_value_key] = $this->castValue($key . '.' . $nested_value_key, $nested_value);
      }
    }
    return $value;
  }

}