Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
Fly.php
Go to the documentation of this file.
1 <?php
3 
7 use League\Flysystem\Adapter\Local as LocalAdapter;
8 use League\Flysystem\Filesystem;
9 use League\Flysystem\Memory\MemoryAdapter;
10 
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 
43  public function chroot($path) {
44  return new self($this->fs, $this->local_path, $path);
45  }
46 
54  private function isDirectory($path) {
55  $path = $this->getInternalPath($path);
56  return $this->fs->has($path) && $this->fs->get($path)->isDir();
57  }
58 
60  public function isFile($path) {
61  $path = $this->getInternalPath($path);
62  return $this->fs->has($path) && $this->fs->get($path)->isFile();
63  }
64 
66  public function getContents($path) {
67  return (string)$this->fs->read($this->getInternalPath($path));
68  }
69 
71  public function getFile($path) {
72  if ($this->isDirectory($path)) {
73  throw new \RuntimeException("There is already a directory at that location: $path");
74  }
75 
76  return new File($this, $path);
77  }
78 
80  public function getFiles($path = '', $recursive = true) {
81  return $this->getEntries($path, $recursive, ['file']);
82  }
83 
85  public function getDirectories($path = '', $recursive = true) {
86  return $this->getEntries($path, $recursive, ['dir']);
87  }
88 
100  protected function getEntries($path = '', $recursive = true, $types = ['file', 'dir']) {
101  $contents = $this->fs->listContents($this->getInternalPath($path), $recursive);
102  if (!$contents) {
103  $contents = [];
104  }
105 
106  $contents = array_filter($contents, function ($metadata) use ($types) {
107  return in_array($metadata['type'], $types);
108  });
109 
110  return Collection\InMemory::fromArray(array_map(function ($metadata) {
111  if ($metadata['type'] === 'file') {
112  return new File($this, $metadata['path']);
113  }
114 
115  return new self($this->fs, $this->local_path, $metadata['path']);
116  }, $contents));
117  }
118 
120  public function getPath($path = '') {
121  $path = $this->normalize($this->getInternalPath($path));
122  return "{$this->local_path}/$path";
123  }
124 
134  private function getInternalPath($path) {
135  $path = strtr($path, '\\', '//');
136  return $this->normalize("{$this->chroot}/$path");
137  }
138 
140  public function includeFile($path) {
141  return include $this->getPath($path);
142  }
143 
145  public function putContents($path, $content) {
146  $this->fs->put($this->getInternalPath($path), $content);
147  }
148 
156  public static function createLocal($path) {
157  $fs = new Filesystem(new LocalAdapter($path));
158  return new self($fs, $path);
159  }
160 
166  public static function createInMemory() {
167  $fs = new Filesystem(new MemoryAdapter());
168  return new self($fs);
169  }
170 
180  private function normalize($path) {
181 
182  $test_path = "/$path/";
183  if (strpos($test_path, '/./') !== false || strpos($test_path, '/../') !== false) {
184  throw new \InvalidArgumentException('Paths cannot contain "." or ".."');
185  }
186 
187  return trim(strtr($path, '\\', '/'), "/");
188  }
189 }
A simple directory abstraction.
Definition: Directory.php:13
putContents($path, $content)
Definition: Fly.php:145
static createInMemory()
Shorthand for generating a new in-memory-only filesystem.
Definition: Fly.php:166
if($footer) $contents
Definition: module.php:44
$metadata
Definition: entity.php:19
$path
Definition: details.php:88
A wrapper around Flysystem that implements Elgg's filesystem API.
Definition: Fly.php:18
__construct(Filesystem $filesystem, $local_path= '', $chroot= '')
Use one of the static factory functions to create an instance.
Definition: Fly.php:36
getDirectories($path= '', $recursive=true)
Definition: Fly.php:85
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:100
$content
Set robots.txt action.
Definition: set_robots.php:6
A read-only interface to a (possibly mutable) group of items.
Definition: Collection.php:33
static createLocal($path)
Shorthand for generating a new local filesystem.
Definition: Fly.php:156
getFiles($path= '', $recursive=true)
Definition: Fly.php:80