You are here

protected static function SplitFilter::getSecondaryStorage in Configuration Split 8

Get the Secondary config storage that the split manages.

Parameters

\Drupal\Core\Config\ImmutableConfig $config: The configuration for the split.

\Drupal\Core\Database\Connection $connection: The database connection for creating a database storage.

Return value

\Drupal\Core\Config\StorageInterface The secondary storage to split to and from.

1 call to SplitFilter::getSecondaryStorage()
SplitFilter::create in src/Plugin/ConfigFilter/SplitFilter.php
Creates an instance of the plugin.

File

src/Plugin/ConfigFilter/SplitFilter.php, line 443

Class

SplitFilter
Provides a SplitFilter.

Namespace

Drupal\config_split\Plugin\ConfigFilter

Code

protected static function getSecondaryStorage(ImmutableConfig $config, Connection $connection) {

  // Here we could determine to use relative paths etc.
  if ($directory = $config
    ->get('folder')) {
    if (!is_dir($directory)) {

      // If the directory doesn't exist, attempt to create it.
      // This might have some negative consequences but we trust the user to
      // have properly configured their site.

      /* @noinspection MkdirRaceConditionInspection */
      @mkdir($directory, 0777, TRUE);
    }

    // The following is roughly: file_save_htaccess($directory, TRUE, TRUE);
    // But we can't use global drupal functions and we want to write the
    // .htaccess file to ensure the configuration is protected and the
    // directory not empty.
    if (file_exists($directory) && is_writable($directory)) {
      $htaccess_path = rtrim($directory, '/\\') . '/.htaccess';
      if (!file_exists($htaccess_path)) {
        file_put_contents($htaccess_path, FileSecurity::htaccessLines(TRUE));
        @chmod($htaccess_path, 0444);
      }
    }
    if (file_exists($directory) || strpos($directory, 'vfs://') === 0) {

      // Allow virtual file systems even if file_exists is false.
      return new FileStorage($directory);
    }
    return NULL;
  }

  // When the folder is not set use a database.
  return new DatabaseStorage($connection, $connection
    ->escapeTable(strtr($config
    ->getName(), [
    '.' => '_',
  ])));
}