ContentFileStorage.php in Default Content for D8 2.0.x
File
src/ContentFileStorage.php
View source
<?php
namespace Drupal\default_content;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileInterface;
class ContentFileStorage implements ContentFileStorageInterface {
protected $fileSystem;
public function __construct(FileSystemInterface $file_system) {
$this->fileSystem = $file_system;
}
public function scan($directory) {
$flags = \FilesystemIterator::UNIX_PATHS;
$flags |= \FilesystemIterator::SKIP_DOTS;
$flags |= \FilesystemIterator::CURRENT_AS_SELF;
$directory_iterator = new \RecursiveDirectoryIterator($directory, $flags);
$iterator = new \RecursiveIteratorIterator($directory_iterator);
$files = [];
foreach ($iterator as $fileinfo) {
if ($fileinfo
->isDir() || $fileinfo
->getExtension() != 'json' && $fileinfo
->getExtension() != 'yml') {
continue;
}
$file = new \stdClass();
$file->name = $fileinfo
->getFilename();
$file->uri = $fileinfo
->getPathname();
$files[$file->uri] = $file;
}
return $files;
}
public function writeEntity(string $folder, string $encoded, ContentEntityInterface $entity, string $filename = NULL) {
$entity_type_folder = "{$folder}/" . $entity
->getEntityTypeId();
$this->fileSystem
->prepareDirectory($entity_type_folder, FileSystemInterface::CREATE_DIRECTORY);
$filename = $filename ?: $entity
->uuid() . '.yml';
file_put_contents($entity_type_folder . '/' . $filename, $encoded);
if ($entity instanceof FileInterface) {
$this->fileSystem
->copy($entity
->getFileUri(), $entity_type_folder . '/' . $entity
->getFilename(), FileSystemInterface::EXISTS_REPLACE);
}
}
protected function putFile($path, $uuid, $serialized_entity) {
file_put_contents($path . '/' . $uuid . '.yml', $serialized_entity);
}
}