Elgg  Version 3.0
Fly.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use League\Flysystem\Adapter\Local as LocalAdapter;
11 
18 final class Fly implements Directory {
19 
21  private $fs;
22 
24  private $local_path;
25 
27  private $chroot;
28 
36  public function __construct(Filesystem $filesystem, $local_path = '', $chroot = '') {
37  $this->fs = $filesystem;
38  $this->local_path = rtrim(strtr($local_path, '\\', '/'), "/\\");
39  $this->chroot = $this->normalize($chroot);
40  }
41 
45  public function chroot($path) {
46  return new self($this->fs, $this->local_path, $path);
47  }
48 
56  private function isDirectory($path) {
57  $path = $this->getInternalPath($path);
58  return $this->fs->has($path) && $this->fs->get($path)->isDir();
59  }
60 
64  public function isFile($path) {
65  $path = $this->getInternalPath($path);
66  return $this->fs->has($path) && $this->fs->get($path)->isFile();
67  }
68 
72  public function getContents($path) {
73  return (string) $this->fs->read($this->getInternalPath($path));
74  }
75 
79  public function getFile($path) {
80  if ($this->isDirectory($path)) {
81  throw new \RuntimeException("There is already a directory at that location: $path");
82  }
83 
84  return new File($this, $path);
85  }
86 
90  public function getFiles($path = '', $recursive = true) {
91  return $this->getEntries($path, $recursive, ['file']);
92  }
93 
97  public function getDirectories($path = '', $recursive = true) {
98  return $this->getEntries($path, $recursive, ['dir']);
99  }
100 
112  protected function getEntries($path = '', $recursive = true, $types = ['file', 'dir']) {
113  $contents = $this->fs->listContents($this->getInternalPath($path), $recursive);
114  if (empty($contents)) {
115  $contents = [];
116  }
117 
118  $contents = array_filter($contents, function ($metadata) use ($types) {
119  return in_array($metadata['type'], $types);
120  });
121 
122  return Collection\InMemory::fromArray(array_map(function ($metadata) {
123  if ($metadata['type'] === 'file') {
124  return new File($this, $metadata['path']);
125  }
126 
127  return new self($this->fs, $this->local_path, $metadata['path']);
128  }, $contents));
129  }
130 
134  public function getPath($path = '') {
135  $path = $this->normalize($this->getInternalPath($path));
136  return "{$this->local_path}/$path";
137  }
138 
148  private function getInternalPath($path) {
149  $path = strtr($path, '\\', '//');
150  return $this->normalize("{$this->chroot}/$path");
151  }
152 
156  public function includeFile($path) {
157  return include $this->getPath($path);
158  }
159 
163  public function putContents($path, $content) {
164  $this->fs->put($this->getInternalPath($path), $content);
165  }
166 
174  public static function createLocal($path) {
175  $fs = new Filesystem(new LocalAdapter($path));
176  return new self($fs, $path);
177  }
178 
184  public static function createInMemory() {
185  $fs = new Filesystem(new MemoryAdapter());
186  return new self($fs);
187  }
188 
198  private function normalize($path) {
199 
200  $test_path = "/$path/";
201  if (strpos($test_path, '/./') !== false || strpos($test_path, '/../') !== false) {
202  throw new \InvalidArgumentException('Paths cannot contain "." or ".."');
203  }
204 
205  return trim(strtr($path, '\\', '/'), "/");
206  }
207 }
isFile($path)
Whether this directory has an existing file at the given location.The relative path within this direc...
Definition: Fly.php:64
A simple directory abstraction.
Definition: Directory.php:13
getPath($path= '')
Get the absolute path to the given directory-relative path.A file/directory path within this director...
Definition: Fly.php:134
putContents($path, $content)
Write a file, overwriting the contents if necessary.The path to the file. The literal text content of...
Definition: Fly.php:163
static createInMemory()
Shorthand for generating a new in-memory-only filesystem.
Definition: Fly.php:184
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:60
$path
Definition: details.php:89
A wrapper around Flysystem that implements Elgg&#39;s filesystem API.
Definition: Fly.php:18
$metadata
Outputs object metadata $vars[&#39;metadata&#39;] Metadata/menu $vars[&#39;show_entity_menu&#39;] Show the entity m...
Definition: metadata.php:10
includeFile($path)
Do a PHP include of the file and return the result.NB: This only really works with local filesystems ...
Definition: Fly.php:156
__construct(Filesystem $filesystem, $local_path= '', $chroot= '')
Use one of the static factory functions to create an instance.
Definition: Fly.php:36
getContents($path)
Read the file off the filesystem.The directory-relative path to the target file.string Empty string i...
Definition: Fly.php:72
getDirectories($path= '', $recursive=true)
List the directories in the given directory path.The subdirectory path within this directory Find dir...
Definition: Fly.php:97
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:112
chroot($path)
Returns a subdirectory with access limited to the given directory.The path relative to this directory...
Definition: Fly.php:45
$content
Set robots.txt action.
Definition: set_robots.php:6
static createLocal($path)
Shorthand for generating a new local filesystem.
Definition: Fly.php:174
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:79
getFiles($path= '', $recursive=true)
List the files in the given directory path.The subdirectory path within this directory Find files rec...
Definition: Fly.php:90