GaufretteDirectory.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Elgg\Filesystem;
  3. use Elgg\Structs\ArrayCollection;
  4. use Gaufrette\Adapter\Local;
  5. use Gaufrette\Adapter\InMemory;
  6. use Gaufrette\Filesystem as Gaufrette;
  7. /**
  8. * A wrapper around Gaufrette that implements Elgg's filesystem API.
  9. *
  10. * @since 1.10.0
  11. *
  12. * @access private
  13. */
  14. final class GaufretteDirectory implements Directory {
  15. /** @var Gaufrette */
  16. private $gaufrette;
  17. /** @var string */
  18. private $localPath;
  19. /** @var string Path relative to the gaufrette filesystem's root */
  20. private $chroot;
  21. /**
  22. * Use one of the static factory functions to create an instance.
  23. *
  24. * @param Gaufrette $gaufrette The underlying filesystem implementation.
  25. * @param string $localPath Only applicable for local filesystem.
  26. * @param string $chroot Path relative to the gaufrette filesystem's root.
  27. */
  28. private function __construct(Gaufrette $gaufrette, $localPath = '', $chroot = '') {
  29. $this->gaufrette = $gaufrette;
  30. $this->localPath = rtrim($localPath, "/\\");
  31. $this->chroot = $this->normalize($chroot);
  32. }
  33. /** @inheritDoc */
  34. public function chroot($path) {
  35. return new self($this->gaufrette, $this->localPath, $this->getGaufrettePath($path));
  36. }
  37. /**
  38. * Whether this filesystem has an existing directory at the given path.
  39. *
  40. * @param string $path The path to the directory, relative to this filesystem.
  41. *
  42. * @return boolean
  43. */
  44. private function isDirectory($path) {
  45. $adapter = $this->gaufrette->getAdapter();
  46. return $adapter->isDirectory($this->getGaufrettePath($path));
  47. }
  48. /** @inheritDoc */
  49. public function isFile($path) {
  50. return !$this->isDirectory($path) &&
  51. $this->gaufrette->has($this->getGaufrettePath($path));
  52. }
  53. /** @inheritDoc */
  54. public function getContents($path) {
  55. try {
  56. return $this->gaufrette->read($this->getGaufrettePath($path));
  57. } catch (\Exception $e) {
  58. return '';
  59. }
  60. }
  61. /** @inheritDoc */
  62. public function getFile($path) {
  63. if ($this->isDirectory($path)) {
  64. throw new \RuntimeException("There is already a directory at that location: $path");
  65. }
  66. return new File($this, $path);
  67. }
  68. /** @inheritDoc */
  69. public function getFiles($path = '') {
  70. $keys = $this->gaufrette->listKeys($this->getGaufrettePath($path));
  71. $files = new ArrayCollection($keys['keys']);
  72. return $files->map(function($path) {
  73. return new File($this, $path);
  74. });
  75. }
  76. /**
  77. * Get the absolute path to the given directory-relative path.
  78. *
  79. * @param string $path A file/directory path within this directory.
  80. *
  81. * @return string
  82. */
  83. private function getFullPath($path = '') {
  84. $gaufrettePath = $this->normalize($this->getGaufrettePath($path));
  85. return "$this->localPath/$gaufrettePath";
  86. }
  87. /**
  88. * Get a path suitable for passing to the underlying gaufrette filesystem.
  89. *
  90. * @param string $path The path relative to this directory.
  91. *
  92. * @return string
  93. */
  94. private function getGaufrettePath($path) {
  95. return $this->normalize("$this->chroot/$path");
  96. }
  97. /** @inheritDoc */
  98. public function includeFile($path) {
  99. return include $this->getFullPath($path);
  100. }
  101. /**
  102. * Get a standardized form of the given path to work with internally.
  103. *
  104. * @param string $path A relative path within this filesystem
  105. *
  106. * @return string
  107. */
  108. private function normalize($path) {
  109. return trim($path, "/");
  110. }
  111. /** @inheritDoc */
  112. public function putContents($path, $content) {
  113. $this->gaufrette->write($this->getGaufrettePath($path), $content, true);
  114. }
  115. /**
  116. * Shorthand for generating a new local filesystem.
  117. *
  118. * @param string $path absolute path to directory on local filesystem.
  119. *
  120. * @return Filesystem
  121. */
  122. public static function createLocal($path) {
  123. return new self(new Gaufrette(new Local($path)), $path);
  124. }
  125. /**
  126. * Shorthand for generating a new in-memory-only filesystem.
  127. *
  128. * @return Filesystem
  129. */
  130. public static function createInMemory() {
  131. return new self(new Gaufrette(new InMemory()));
  132. }
  133. }