|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SlamCompressAndEncryptProxy; |
| 6 | + |
| 7 | +use League\Flysystem\Config; |
| 8 | +use League\Flysystem\FileAttributes; |
| 9 | +use League\Flysystem\FilesystemAdapter; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +abstract class AbstractProxyAdapter implements FilesystemAdapter |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + private FilesystemAdapter $remoteAdapter |
| 18 | + ) { |
| 19 | + } |
| 20 | + |
| 21 | + abstract public static function getRemoteFileExtension(): string; |
| 22 | + |
| 23 | + /** |
| 24 | + * {@inheritDoc} |
| 25 | + */ |
| 26 | + final public function fileExists(string $path): bool |
| 27 | + { |
| 28 | + return $this->remoteAdapter->fileExists($this->getRemotePath($path)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + final public function write(string $path, string $contents, Config $config): void |
| 35 | + { |
| 36 | + $stream = fopen('php://temp', 'w+'); |
| 37 | + fwrite($stream, $contents); |
| 38 | + rewind($stream); |
| 39 | + |
| 40 | + $this->writeStream($path, $stream, $config); |
| 41 | + |
| 42 | + fclose($stream); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritDoc} |
| 47 | + */ |
| 48 | + final public function read(string $path): string |
| 49 | + { |
| 50 | + return stream_get_contents($this->readStream($path)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritDoc} |
| 55 | + */ |
| 56 | + final public function delete(string $path): void |
| 57 | + { |
| 58 | + $this->remoteAdapter->delete($this->getRemotePath($path)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritDoc} |
| 63 | + */ |
| 64 | + final public function deleteDirectory(string $path): void |
| 65 | + { |
| 66 | + $this->remoteAdapter->deleteDirectory($path); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritDoc} |
| 71 | + */ |
| 72 | + final public function createDirectory(string $path, Config $config): void |
| 73 | + { |
| 74 | + $this->remoteAdapter->createDirectory($path, $config); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritDoc} |
| 79 | + */ |
| 80 | + final public function setVisibility(string $path, string $visibility): void |
| 81 | + { |
| 82 | + $this->remoteAdapter->setVisibility($this->getRemotePath($path), $visibility); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritDoc} |
| 87 | + */ |
| 88 | + final public function visibility(string $path): FileAttributes |
| 89 | + { |
| 90 | + return $this->remoteAdapter->visibility($this->getRemotePath($path)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * {@inheritDoc} |
| 95 | + */ |
| 96 | + final public function mimeType(string $path): FileAttributes |
| 97 | + { |
| 98 | + return $this->remoteAdapter->mimeType($this->getRemotePath($path)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * {@inheritDoc} |
| 103 | + */ |
| 104 | + final public function lastModified(string $path): FileAttributes |
| 105 | + { |
| 106 | + return $this->remoteAdapter->lastModified($this->getRemotePath($path)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * {@inheritDoc} |
| 111 | + */ |
| 112 | + final public function fileSize(string $path): FileAttributes |
| 113 | + { |
| 114 | + return $this->remoteAdapter->fileSize($this->getRemotePath($path)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritDoc} |
| 119 | + */ |
| 120 | + final public function listContents(string $path, bool $deep): iterable |
| 121 | + { |
| 122 | + $remoteList = $this->remoteAdapter->listContents($path, $deep); |
| 123 | + |
| 124 | + foreach ($remoteList as $content) { |
| 125 | + if ($content instanceof FileAttributes) { |
| 126 | + $content = new FileAttributes( |
| 127 | + substr($content->path(), 0, -\strlen(static::getRemoteFileExtension())), |
| 128 | + $content->fileSize(), |
| 129 | + $content->visibility(), |
| 130 | + $content->lastModified(), |
| 131 | + $content->mimeType(), |
| 132 | + $content->extraMetadata() |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + yield $content; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * {@inheritDoc} |
| 142 | + */ |
| 143 | + final public function move(string $source, string $destination, Config $config): void |
| 144 | + { |
| 145 | + $this->remoteAdapter->move( |
| 146 | + $this->getRemotePath($source), |
| 147 | + $this->getRemotePath($destination), |
| 148 | + $config |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * {@inheritDoc} |
| 154 | + */ |
| 155 | + final public function copy(string $source, string $destination, Config $config): void |
| 156 | + { |
| 157 | + $this->remoteAdapter->copy( |
| 158 | + $this->getRemotePath($source), |
| 159 | + $this->getRemotePath($destination), |
| 160 | + $config |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + final protected function getRemoteAdapter(): FilesystemAdapter |
| 165 | + { |
| 166 | + return $this->remoteAdapter; |
| 167 | + } |
| 168 | + |
| 169 | + final protected function getRemotePath(string $path): string |
| 170 | + { |
| 171 | + return $path.static::getRemoteFileExtension(); |
| 172 | + } |
| 173 | +} |
0 commit comments