trait JsonWriterTrait in Content Synchronizer 3.x
Same name and namespace in other branches
- 8.2 src/Base/JsonWriterTrait.php \Drupal\content_synchronizer\Base\JsonWriterTrait
 - 8 src/Base/JsonWriterTrait.php \Drupal\content_synchronizer\Base\JsonWriterTrait
 
Json writer tool.
Hierarchy
- trait \Drupal\content_synchronizer\Base\JsonWriterTrait
 
3 files declare their use of JsonWriterTrait
- ExportEntityWriter.php in src/
Processors/ ExportEntityWriter.php  - FileProcessor.php in src/
Plugin/ content_synchronizer/ entity_processor/ FileProcessor.php  - ImportEntity.php in src/
Entity/ ImportEntity.php  
File
- src/
Base/ JsonWriterTrait.php, line 11  
Namespace
Drupal\content_synchronizer\BaseView source
trait JsonWriterTrait {
  /**
   * File System.
   *
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;
  /**
   * Save json in the destination file.
   */
  protected function writeJson($data, $destination) {
    // Create dir :
    $dir = explode('/', $destination);
    array_pop($dir);
    $dir = implode('/', $dir);
    $this
      ->createDirectory($dir);
    $this
      ->fileSystem()
      ->prepareDirectory($dir, FileSystem::CHMOD_DIRECTORY);
    $uri = $this
      ->fileSystem()
      ->saveData(json_encode($data), $destination, FileSystem::EXISTS_REPLACE);
    File::create([
      'uri' => $uri,
      'status' => FILE_STATUS_PERMANENT,
    ])
      ->save();
  }
  /**
   * Get json decode data from a file.
   */
  protected function getDataFromFile($path) {
    if (file_exists($path)) {
      return json_decode(file_get_contents($path), TRUE);
    }
    return NULL;
  }
  /**
   * Create a directory if not exists.
   */
  protected function createDirectory($dir) {
    if (!is_dir($dir)) {
      $this
        ->fileSystem()
        ->prepareDirectory($dir, FileSystem::CREATE_DIRECTORY);
    }
  }
  /**
   * Create a directory tree.
   */
  protected function createDirTreeForFileDest($destination, $root = '/') {
    $destinationItems = explode('/', $destination);
    $fileName = array_pop($destinationItems);
    // Create destination tree.
    foreach ($destinationItems as $dirItem) {
      $root .= '/' . $dirItem;
      $this
        ->createDirectory($root);
    }
    return $root . '/' . $fileName;
  }
  /**
   * Return file system.
   *
   * @return \Drupal\Core\File\FileSystem|mixed
   *   The file system.
   */
  public function fileSystem() {
    return \Drupal::service('file_system');
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            JsonWriterTrait:: | 
                  protected | property | File System. | |
| 
            JsonWriterTrait:: | 
                  protected | function | Create a directory if not exists. | |
| 
            JsonWriterTrait:: | 
                  protected | function | Create a directory tree. | |
| 
            JsonWriterTrait:: | 
                  public | function | Return file system. | |
| 
            JsonWriterTrait:: | 
                  protected | function | Get json decode data from a file. | |
| 
            JsonWriterTrait:: | 
                  protected | function | Save json in the destination file. |