StorageException example

/** * Check if the directory exists and create it if not. */
  protected function ensureStorage() {
    $dir = $this->getCollectionDirectory();
    $success = $this->getFileSystem()->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    // Only create .htaccess file in root directory.     if ($dir == $this->directory) {
      $success = $success && FileSecurity::writeHtaccess($this->directory);
    }
    if (!$success) {
      throw new StorageException('Failed to create config directory ' . $dir);
    }
    return $this;
  }

  /** * {@inheritdoc} */
  public function exists($name) {
    return file_exists($this->getFilePath($name));
  }

  

  public function getFilePath($name) {
    $folders = $this->getAllFolders();
    if (isset($folders[$name])) {
      return $folders[$name] . '/' . $name . '.' . $this->getFileExtension();
    }
    // If any code in the early installer requests a configuration object that     // does not exist anywhere as default config, then that must be mistake.     throw new StorageException("Missing configuration file: $name");
  }

  /** * {@inheritdoc} */
  public function exists($name) {
    return array_key_exists($name$this->getAllFolders());
  }

  /** * Overrides Drupal\Core\Config\FileStorage::write(). * * @throws \Drupal\Core\Config\StorageException */
public function write($name, array $data) {
    $data = $this->encode($data);
    try {
      return $this->doWrite($name$data);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.       if ($this->ensureTableExists()) {
        return $this->doWrite($name$data);
      }
      // Some other failure that we can not recover from.       throw new StorageException($e->getMessage(), 0, $e);
    }
  }

  /** * Helper method so we can re-try a write. * * @param string $name * The config name. * @param string $data * The config data, already dumped to a string. * * @return bool */
Home | Imprint | This part of the site doesn't use cookies.