Elgg  Version 1.11
GaufretteDirectory.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Filesystem;
3 
7 use Gaufrette\Filesystem as Gaufrette;
8 
16 final class GaufretteDirectory implements Directory {
17 
19  private $gaufrette;
20 
22  private $localPath;
23 
25  private $chroot;
26 
34  private function __construct(Gaufrette $gaufrette, $localPath = '', $chroot = '') {
35  $this->gaufrette = $gaufrette;
36  $this->localPath = rtrim($localPath, "/\\");
37  $this->chroot = $this->normalize($chroot);
38  }
39 
41  public function chroot($path) {
42  return new self($this->gaufrette, $this->localPath, $this->getGaufrettePath($path));
43  }
44 
52  private function isDirectory($path) {
53  $adapter = $this->gaufrette->getAdapter();
54  return $adapter->isDirectory($this->getGaufrettePath($path));
55  }
56 
58  public function isFile($path) {
59  return !$this->isDirectory($path) &&
60  $this->gaufrette->has($this->getGaufrettePath($path));
61  }
62 
64  public function getContents($path) {
65  try {
66  return $this->gaufrette->read($this->getGaufrettePath($path));
67  } catch (\Exception $e) {
68  return '';
69  }
70  }
71 
73  public function getFile($path) {
74  if ($this->isDirectory($path)) {
75  throw new \RuntimeException("There is already a directory at that location: $path");
76  }
77 
78  return new File($this, $path);
79  }
80 
82  public function getFiles($path = '') {
83  $keys = $this->gaufrette->listKeys($this->getGaufrettePath($path));
84 
85  $files = new ArrayCollection($keys['keys']);
86 
87  return $files->map(function($path) {
88  return new File($this, $path);
89  });
90  }
91 
99  private function getFullPath($path = '') {
100  $gaufrettePath = $this->normalize($this->getGaufrettePath($path));
101  return "$this->localPath/$gaufrettePath";
102  }
103 
111  private function getGaufrettePath($path) {
112  return $this->normalize("$this->chroot/$path");
113  }
114 
116  public function includeFile($path) {
117  return include $this->getFullPath($path);
118  }
119 
127  private function normalize($path) {
128  return trim($path, "/");
129  }
130 
132  public function putContents($path, $content) {
133  $this->gaufrette->write($this->getGaufrettePath($path), $content, true);
134  }
135 
143  public static function createLocal($path) {
144  return new self(new Gaufrette(new Local($path)), $path);
145  }
146 
152  public static function createInMemory() {
153  return new self(new Gaufrette(new InMemory()));
154  }
155 }
A simple directory abstraction.
Definition: Directory.php:11
$e
Definition: metadata.php:12
$files
Definition: crop.php:36
$keys
Definition: access.php:35
static createInMemory()
Shorthand for generating a new in-memory-only filesystem.
Represents a file that may or may not actually exist.
Definition: File.php:11
$content
Set robots.txt action.
Definition: set_robots.php:6
static createLocal($path)
Shorthand for generating a new local filesystem.
$path
Definition: invalid.php:17
A wrapper around Gaufrette that implements Elgg&#39;s filesystem API.