Open Data Catalog v2.0.0
FileHelper.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\File\FileSystemInterface;
6 
12 class FileHelper implements IFileHelper {
13 
17  public function getRealPath($path) {
18  return \Drupal::service('file_system')
19  ->realpath($path);
20  }
21 
25  public function prepareDir(&$directory, $options = FileSystemInterface::CREATE_DIRECTORY) {
26  return \Drupal::service('file_system')->prepareDirectory($directory, $options);
27  }
28 
32  public function retrieveFile($url, $destination = NULL, $managed = FALSE) {
33  if (substr_count($url, "file://") > 0) {
34 
35  $src = str_replace("file://", "", $url);
36 
37  $pieces = parse_url($url);
38  $path = explode("/", $pieces['path']);
39  $filename = end($path);
40 
41  $dest = $this->getRealPath($destination) . "/{$filename}";
42 
43  copy($src, $dest);
44 
45  $url = file_create_url("{$destination}/{$filename}");
46 
47  return $url;
48  }
49  else {
50  return system_retrieve_file($url, $destination, $managed, FileSystemInterface::EXISTS_REPLACE);
51  }
52  }
53 
57  public function fileCreate($uri) {
58  return file_create_url($uri);
59  }
60 
64  public function defaultSchemeDirectory() {
65  // @todo this might not always work.
66  // Considering s3fs or others that don't live on disk
67  return $this->getRealPath(
68  \Drupal::config('system.file')
69  ->get('default_scheme') . "://"
70  );
71  }
72 
76  public function fileGetContents($path) {
77  return (is_readable($path) && is_file($path))
78  ? file_get_contents($path)
79  : NULL;
80  }
81 
85  public function filePutContents($path, $content) {
86  return file_put_contents($path, $content);
87  }
88 
92  public function fileDelete($uri) {
93  if (file_exists($uri)) {
94  unlink($uri);
95  }
96  }
97 
101  public function fileGlob($pattern, $flags = 0) {
102  return glob($pattern, $flags);
103  }
104 
105 }
Drupal\dkan_harvest\Load\FileHelper
Definition: FileHelper.php:12
Drupal\dkan_harvest\Load\FileHelper\fileCreate
fileCreate($uri)
Definition: FileHelper.php:57
Drupal\dkan_harvest\Load
Definition: Dataset.php:3
Drupal\dkan_harvest\Load\FileHelper\filePutContents
filePutContents($path, $content)
Definition: FileHelper.php:85
Drupal\dkan_harvest\Load\FileHelper\prepareDir
prepareDir(&$directory, $options=FileSystemInterface::CREATE_DIRECTORY)
Definition: FileHelper.php:25
Drupal\dkan_harvest\Load\FileHelper\fileGlob
fileGlob($pattern, $flags=0)
Definition: FileHelper.php:101
Drupal\dkan_harvest\Load\FileHelper\fileGetContents
fileGetContents($path)
Definition: FileHelper.php:76
Drupal\dkan_harvest\Load\FileHelper\fileDelete
fileDelete($uri)
Definition: FileHelper.php:92
Drupal\dkan_harvest\Load\FileHelper\retrieveFile
retrieveFile($url, $destination=NULL, $managed=FALSE)
Definition: FileHelper.php:32
Drupal\dkan_harvest\Load\FileHelper\getRealPath
getRealPath($path)
Definition: FileHelper.php:17
Drupal\dkan_harvest\Load\IFileHelper
Definition: IFileHelper.php:8
Drupal\dkan_harvest\Load\FileHelper\defaultSchemeDirectory
defaultSchemeDirectory()
Definition: FileHelper.php:64