You are here

public function ModuleFileWriter::writeSingleFile in Module Builder 8.3

Writes a single file.

Parameters

string $drupal_relative_module_dir: The module folder to write to, as a path relative to Drupal root.

string $module_relative_filepath: The name of the file to write, as a path relative to the module folder, e.g. src/Plugins/Block/Foo.php.

string $file_contents: The file contents to write.

Return value

bool TRUE if writing succeeded, FALSE if it failed.

File

src/ModuleFileWriter.php, line 92

Class

ModuleFileWriter
Writes module files.

Namespace

Drupal\module_builder

Code

public function writeSingleFile($drupal_relative_module_dir, $module_relative_filepath, $file_contents) {

  // The files are keyed by a filepath relative to the future module folder,
  // e.g. src/Plugins/Block/Foo.php.
  // Extract the directory.
  $module_relative_dir = dirname($module_relative_filepath);
  $filename = basename($module_relative_filepath);
  $drupal_relative_dir = $drupal_relative_module_dir . '/' . $module_relative_dir;
  $drupal_relative_filepath = $drupal_relative_module_dir . '/' . $module_relative_filepath;
  $this->fileSystem
    ->prepareDirectory($drupal_relative_dir, FileSystemInterface::CREATE_DIRECTORY);
  $result = file_put_contents($drupal_relative_filepath, $file_contents);

  // Force the Core extension system to rescan for modules if we've written
  // a module info file, so that the reloaded form can detect the module and
  // warn for the existing files.
  if ($result !== FALSE && substr($filename, -8) == 'info.yml') {
    $this->moduleExtensionList
      ->reset();
  }
  return $result !== FALSE;
}