class ContentFileStorage in Default Content for D8 2.0.x
Finds, reads and writes default content files.
Hierarchy
- class \Drupal\default_content\ContentFileStorage implements ContentFileStorageInterface
Expanded class hierarchy of ContentFileStorage
1 string reference to 'ContentFileStorage'
1 service uses ContentFileStorage
File
- src/
ContentFileStorage.php, line 12
Namespace
Drupal\default_contentView source
class ContentFileStorage implements ContentFileStorageInterface {
/**
* The filesystem service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs the content file storage.
*
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The filesystem service.
*/
public function __construct(FileSystemInterface $file_system) {
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public function scan($directory) {
// Use Unix paths regardless of platform, skip dot directories, follow
// symlinks (to allow extensions to be linked from elsewhere), and return
// the RecursiveDirectoryIterator instance to have access to getSubPath(),
// since SplFileInfo does not support relative paths.
$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) {
/* @var \SplFileInfo $fileinfo */
// Skip directories and non-json files.
if ($fileinfo
->isDir() || $fileinfo
->getExtension() != 'json' && $fileinfo
->getExtension() != 'yml') {
continue;
}
// @todo Use a typed class?
$file = new \stdClass();
$file->name = $fileinfo
->getFilename();
$file->uri = $fileinfo
->getPathname();
$files[$file->uri] = $file;
}
return $files;
}
/**
* {@inheritdoc}
*/
public function writeEntity(string $folder, string $encoded, ContentEntityInterface $entity, string $filename = NULL) {
// Ensure that the folder per entity type exists.
$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);
// For files, copy the file into the same folder.
if ($entity instanceof FileInterface) {
$this->fileSystem
->copy($entity
->getFileUri(), $entity_type_folder . '/' . $entity
->getFilename(), FileSystemInterface::EXISTS_REPLACE);
}
}
/**
* Helper for ::writeDefaultContent to wrap file_put_contents.
*
* @param string $path
* Content directory + entity directory to which to write the file.
* @param string $uuid
* Entity UUID, to be used as filename.
* @param string $serialized_entity
* The serialized entity to write.
*/
protected function putFile($path, $uuid, $serialized_entity) {
file_put_contents($path . '/' . $uuid . '.yml', $serialized_entity);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentFileStorage:: |
protected | property | The filesystem service. | |
ContentFileStorage:: |
protected | function | Helper for ::writeDefaultContent to wrap file_put_contents. | |
ContentFileStorage:: |
public | function |
Returns a list of file objects. Overrides ContentFileStorageInterface:: |
|
ContentFileStorage:: |
public | function |
Writes a normalized entity to the given folder. Overrides ContentFileStorageInterface:: |
|
ContentFileStorage:: |
public | function | Constructs the content file storage. |