Elgg  Version 4.3
Fly.php
Go to the documentation of this file.
1 <?php
2 
4 
10 use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
13 
20 final class Fly implements Directory {
21 
25  private $fs;
26 
30  private $local_path;
31 
35  private $chroot;
36 
44  public function __construct(Filesystem $filesystem, $local_path = '', $chroot = '') {
45  $this->fs = $filesystem;
46  $this->local_path = rtrim(strtr($local_path, '\\', '/'), "/\\");
47  $this->chroot = $this->normalize($chroot);
48  }
49 
53  public function chroot($path) {
54  return new self($this->fs, $this->local_path, $path);
55  }
56 
64  private function isDirectory($path) {
65  $path = $this->getInternalPath($path);
66  return !empty($this->fs->listContents($path)->toArray());
67  }
68 
72  public function isFile($path) {
73  return $this->fs->fileExists($this->getInternalPath($path));
74  }
75 
79  public function getContents($path) {
80  return (string) $this->fs->read($this->getInternalPath($path));
81  }
82 
88  public function getFile($path) {
89  if ($this->isDirectory($path)) {
90  throw new RuntimeException("There is already a directory at that location: {$path}");
91  }
92 
93  return new File($this, $path);
94  }
95 
99  public function getFiles($path = '', $recursive = true) {
100  return $this->getEntries($path, $recursive, ['file']);
101  }
102 
106  public function getDirectories($path = '', $recursive = true) {
107  return $this->getEntries($path, $recursive, ['dir']);
108  }
109 
119  protected function getEntries($path = '', $recursive = true, $types = ['file', 'dir']) {
120  $contents = $this->fs->listContents($this->getInternalPath($path), $recursive)->toArray();
121  if (empty($contents)) {
122  $contents = [];
123  }
124 
125  $contents = array_filter($contents, function ($metadata) use ($types) {
126  return in_array($metadata['type'], $types);
127  });
128 
129  return Collection\InMemory::fromArray(array_map(function ($metadata) {
130  if ($metadata['type'] === 'file') {
131  return new File($this, $metadata['path']);
132  }
133 
134  return new self($this->fs, $this->local_path, $metadata['path']);
135  }, $contents));
136  }
137 
141  public function getPath($path = '') {
142  $path = $this->normalize($this->getInternalPath($path));
143  return "{$this->local_path}/$path";
144  }
145 
153  private function getInternalPath($path) {
154  $path = strtr($path, '\\', '//');
155  return $this->normalize("{$this->chroot}/$path");
156  }
157 
161  public function includeFile($path) {
162  return include $this->getPath($path);
163  }
164 
168  public function putContents($path, $content) {
169  $this->fs->write($this->getInternalPath($path), $content);
170  }
171 
179  public static function createLocal($path) {
180  $fs = new Filesystem(new LocalAdapter($path));
181  return new self($fs, $path);
182  }
183 
189  public static function createInMemory() {
190  $fs = new Filesystem(new InMemoryFilesystemAdapter());
191  return new self($fs);
192  }
193 
202  private function normalize($path) {
203 
204  $test_path = "/$path/";
205  if (strpos($test_path, '/./') !== false || strpos($test_path, '/../') !== false) {
206  throw new InvalidArgumentException('Paths cannot contain "." or ".."');
207  }
208 
209  return trim(strtr($path, '\\', '/'), "/");
210  }
211 }
isFile($path)
Whether this directory has an existing file at the given location.The relative path within this direc...
Definition: Fly.php:72
A simple directory abstraction.
Definition: Directory.php:14
getPath($path= '')
Get the absolute path to the given directory-relative path.A file/directory path within this director...
Definition: Fly.php:141
Exception thrown if an argument is not of the expected type.
Exception thrown if an error which can only be found on runtime occurs.
putContents($path, $content)
Write a file, overwriting the contents if necessary.The path to the file. The literal text content of...
Definition: Fly.php:168
static createInMemory()
Shorthand for generating a new in-memory-only filesystem.
Definition: Fly.php:189
A wrapper around Flysystem that implements Elgg&#39;s filesystem API.
Definition: Fly.php:20
includeFile($path)
Do a PHP include of the file and return the result.NB: This only really works with local filesystems ...
Definition: Fly.php:161
$path
Definition: details.php:68
__construct(Filesystem $filesystem, $local_path= '', $chroot= '')
Use one of the static factory functions to create an instance.
Definition: Fly.php:44
getContents($path)
Read the file off the filesystem.The directory-relative path to the target file.string Empty string i...
Definition: Fly.php:79
getDirectories($path= '', $recursive=true)
List the directories in the given directory path.The subdirectory path within this directory Find dir...
Definition: Fly.php:106
Represents a file that may or may not actually exist.
Definition: File.php:11
getEntries($path= '', $recursive=true, $types=['file', 'dir'])
List the files and directories in the given directory path.
Definition: Fly.php:119
chroot($path)
Returns a subdirectory with access limited to the given directory.The path relative to this directory...
Definition: Fly.php:53
$metadata
Output annotation metadata.
Definition: metadata.php:9
$content
Set robots.txt action.
Definition: set_robots.php:6
static createLocal($path)
Shorthand for generating a new local filesystem.
Definition: Fly.php:179
getFile($path)
A reference to the file at the given path, even if it doesn&#39;t exist yet.However, will throw an except...
Definition: Fly.php:88
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:73
getFiles($path= '', $recursive=true)
List the files in the given directory path.The subdirectory path within this directory Find files rec...
Definition: Fly.php:99