| @@ 18-268 (lines=251) @@ | ||
| 15 | * @author Antoine Hérault <[email protected]> |
|
| 16 | * @author Leszek Prabucki <[email protected]> |
|
| 17 | */ |
|
| 18 | class Local implements Adapter, |
|
| 19 | StreamFactory, |
|
| 20 | ChecksumCalculator, |
|
| 21 | SizeCalculator, |
|
| 22 | MimeTypeProvider |
|
| 23 | { |
|
| 24 | protected $directory; |
|
| 25 | private $create; |
|
| 26 | private $mode; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * @param string $directory Directory where the filesystem is located |
|
| 30 | * @param bool $create Whether to create the directory if it does not |
|
| 31 | * exist (default FALSE) |
|
| 32 | * @param int $mode Mode for mkdir |
|
| 33 | * |
|
| 34 | * @throws RuntimeException if the specified directory does not exist and |
|
| 35 | * could not be created |
|
| 36 | */ |
|
| 37 | public function __construct($directory, $create = false, $mode = 0777) |
|
| 38 | { |
|
| 39 | $this->directory = Util\Path::normalize($directory); |
|
| 40 | ||
| 41 | if (is_link($this->directory)) { |
|
| 42 | $this->directory = realpath($this->directory); |
|
| 43 | } |
|
| 44 | ||
| 45 | $this->create = $create; |
|
| 46 | $this->mode = $mode; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * {@inheritdoc} |
|
| 51 | */ |
|
| 52 | public function read($key) |
|
| 53 | { |
|
| 54 | return file_get_contents($this->computePath($key)); |
|
| 55 | } |
|
| 56 | ||
| 57 | /** |
|
| 58 | * {@inheritdoc} |
|
| 59 | */ |
|
| 60 | public function write($key, $content) |
|
| 61 | { |
|
| 62 | $path = $this->computePath($key); |
|
| 63 | $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($path), true); |
|
| 64 | ||
| 65 | return file_put_contents($path, $content); |
|
| 66 | } |
|
| 67 | ||
| 68 | /** |
|
| 69 | * {@inheritdoc} |
|
| 70 | */ |
|
| 71 | public function rename($sourceKey, $targetKey) |
|
| 72 | { |
|
| 73 | $targetPath = $this->computePath($targetKey); |
|
| 74 | $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($targetPath), true); |
|
| 75 | ||
| 76 | return rename($this->computePath($sourceKey), $targetPath); |
|
| 77 | } |
|
| 78 | ||
| 79 | /** |
|
| 80 | * {@inheritdoc} |
|
| 81 | */ |
|
| 82 | public function exists($key) |
|
| 83 | { |
|
| 84 | return file_exists($this->computePath($key)); |
|
| 85 | } |
|
| 86 | ||
| 87 | /** |
|
| 88 | * {@inheritdoc} |
|
| 89 | */ |
|
| 90 | public function keys() |
|
| 91 | { |
|
| 92 | $this->ensureDirectoryExists($this->directory, $this->create); |
|
| 93 | ||
| 94 | try { |
|
| 95 | $files = new \RecursiveIteratorIterator( |
|
| 96 | new \RecursiveDirectoryIterator( |
|
| 97 | $this->directory, |
|
| 98 | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS |
|
| 99 | ), |
|
| 100 | \RecursiveIteratorIterator::CHILD_FIRST |
|
| 101 | ); |
|
| 102 | } catch (\Exception $e) { |
|
| 103 | $files = new \EmptyIterator(); |
|
| 104 | } |
|
| 105 | ||
| 106 | $keys = array(); |
|
| 107 | foreach ($files as $file) { |
|
| 108 | $keys[] = $this->computeKey($file); |
|
| 109 | } |
|
| 110 | sort($keys); |
|
| 111 | ||
| 112 | return $keys; |
|
| 113 | } |
|
| 114 | ||
| 115 | /** |
|
| 116 | * {@inheritdoc} |
|
| 117 | */ |
|
| 118 | public function mtime($key) |
|
| 119 | { |
|
| 120 | return filemtime($this->computePath($key)); |
|
| 121 | } |
|
| 122 | ||
| 123 | /** |
|
| 124 | * {@inheritdoc} |
|
| 125 | */ |
|
| 126 | public function delete($key) |
|
| 127 | { |
|
| 128 | if ($this->isDirectory($key)) { |
|
| 129 | return rmdir($this->computePath($key)); |
|
| 130 | } |
|
| 131 | ||
| 132 | return unlink($this->computePath($key)); |
|
| 133 | } |
|
| 134 | ||
| 135 | /** |
|
| 136 | * @param string $key |
|
| 137 | * |
|
| 138 | * @return bool |
|
| 139 | */ |
|
| 140 | public function isDirectory($key) |
|
| 141 | { |
|
| 142 | return is_dir($this->computePath($key)); |
|
| 143 | } |
|
| 144 | ||
| 145 | /** |
|
| 146 | * {@inheritdoc} |
|
| 147 | */ |
|
| 148 | public function createStream($key) |
|
| 149 | { |
|
| 150 | return new Stream\Local($this->computePath($key)); |
|
| 151 | } |
|
| 152 | ||
| 153 | /** |
|
| 154 | * {@inheritdoc} |
|
| 155 | */ |
|
| 156 | public function checksum($key) |
|
| 157 | { |
|
| 158 | return Util\Checksum::fromFile($this->computePath($key)); |
|
| 159 | } |
|
| 160 | ||
| 161 | /** |
|
| 162 | * {@inheritdoc} |
|
| 163 | */ |
|
| 164 | public function size($key) |
|
| 165 | { |
|
| 166 | return Util\Size::fromFile($this->computePath($key)); |
|
| 167 | } |
|
| 168 | ||
| 169 | /** |
|
| 170 | * {@inheritdoc} |
|
| 171 | */ |
|
| 172 | public function mimeType($key) |
|
| 173 | { |
|
| 174 | $fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
|
| 175 | ||
| 176 | return $fileInfo->file($this->computePath($key)); |
|
| 177 | } |
|
| 178 | ||
| 179 | /** |
|
| 180 | * Computes the key from the specified path. |
|
| 181 | * |
|
| 182 | * @param string $path |
|
| 183 | * |
|
| 184 | * return string |
|
| 185 | */ |
|
| 186 | public function computeKey($path) |
|
| 187 | { |
|
| 188 | $path = $this->normalizePath($path); |
|
| 189 | ||
| 190 | return ltrim(substr($path, strlen($this->directory)), '/'); |
|
| 191 | } |
|
| 192 | ||
| 193 | /** |
|
| 194 | * Computes the path from the specified key. |
|
| 195 | * |
|
| 196 | * @param string $key The key which for to compute the path |
|
| 197 | * |
|
| 198 | * @return string A path |
|
| 199 | * |
|
| 200 | * @throws OutOfBoundsException If the computed path is out of the |
|
| 201 | * directory |
|
| 202 | * @throws RuntimeException If directory does not exists and cannot be created |
|
| 203 | */ |
|
| 204 | protected function computePath($key) |
|
| 205 | { |
|
| 206 | $this->ensureDirectoryExists($this->directory, $this->create); |
|
| 207 | ||
| 208 | return $this->normalizePath($this->directory.'/'.$key); |
|
| 209 | } |
|
| 210 | ||
| 211 | /** |
|
| 212 | * Normalizes the given path. |
|
| 213 | * |
|
| 214 | * @param string $path |
|
| 215 | * |
|
| 216 | * @return string |
|
| 217 | */ |
|
| 218 | protected function normalizePath($path) |
|
| 219 | { |
|
| 220 | $path = Util\Path::normalize($path); |
|
| 221 | ||
| 222 | if (0 !== strpos($path, $this->directory)) { |
|
| 223 | throw new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', $path)); |
|
| 224 | } |
|
| 225 | ||
| 226 | return $path; |
|
| 227 | } |
|
| 228 | ||
| 229 | /** |
|
| 230 | * Ensures the specified directory exists, creates it if it does not. |
|
| 231 | * |
|
| 232 | * @param string $directory Path of the directory to test |
|
| 233 | * @param bool $create Whether to create the directory if it does |
|
| 234 | * not exist |
|
| 235 | * |
|
| 236 | * @throws RuntimeException if the directory does not exists and could not |
|
| 237 | * be created |
|
| 238 | */ |
|
| 239 | protected function ensureDirectoryExists($directory, $create = false) |
|
| 240 | { |
|
| 241 | if (!is_dir($directory)) { |
|
| 242 | if (!$create) { |
|
| 243 | throw new \RuntimeException(sprintf('The directory "%s" does not exist.', $directory)); |
|
| 244 | } |
|
| 245 | ||
| 246 | $this->createDirectory($directory); |
|
| 247 | } |
|
| 248 | } |
|
| 249 | ||
| 250 | /** |
|
| 251 | * Creates the specified directory and its parents. |
|
| 252 | * |
|
| 253 | * @param string $directory Path of the directory to create |
|
| 254 | * |
|
| 255 | * @throws InvalidArgumentException if the directory already exists |
|
| 256 | * @throws RuntimeException if the directory could not be created |
|
| 257 | */ |
|
| 258 | protected function createDirectory($directory) |
|
| 259 | { |
|
| 260 | $created = mkdir($directory, $this->mode, true); |
|
| 261 | ||
| 262 | if (!$created) { |
|
| 263 | if (!is_dir($directory)) { |
|
| 264 | throw new \RuntimeException(sprintf('The directory \'%s\' could not be created.', $directory)); |
|
| 265 | } |
|
| 266 | } |
|
| 267 | } |
|
| 268 | } |
|
| 269 | ||
| @@ 15-267 (lines=253) @@ | ||
| 12 | * @author Antoine Hérault <[email protected]> |
|
| 13 | * @author Leszek Prabucki <[email protected]> |
|
| 14 | */ |
|
| 15 | class Local implements Adapter, |
|
| 16 | Adapter\StreamFactory, |
|
| 17 | Adapter\ChecksumCalculator, |
|
| 18 | Adapter\SizeCalculator, |
|
| 19 | Adapter\MimeTypeProvider |
|
| 20 | { |
|
| 21 | protected $directory; |
|
| 22 | private $create; |
|
| 23 | private $mode; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @param string $directory Directory where the filesystem is located |
|
| 27 | * @param bool $create Whether to create the directory if it does not |
|
| 28 | * exist (default FALSE) |
|
| 29 | * @param int $mode Mode for mkdir |
|
| 30 | * |
|
| 31 | * @throws \RuntimeException if the specified directory does not exist and |
|
| 32 | * could not be created |
|
| 33 | */ |
|
| 34 | public function __construct($directory, $create = false, $mode = 0777) |
|
| 35 | { |
|
| 36 | // var_dump($directory); |
|
| 37 | $this->directory = Util\Path::normalize($directory); |
|
| 38 | // var_dump($this->directory); |
|
| 39 | ||
| 40 | if (is_link($this->directory)) { |
|
| 41 | $this->directory = realpath($this->directory); |
|
| 42 | } |
|
| 43 | ||
| 44 | $this->create = $create; |
|
| 45 | $this->mode = $mode; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * {@inheritdoc} |
|
| 50 | */ |
|
| 51 | public function read($key) |
|
| 52 | { |
|
| 53 | return file_get_contents($this->computePath($key)); |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * {@inheritdoc} |
|
| 58 | */ |
|
| 59 | public function write($key, $content) |
|
| 60 | { |
|
| 61 | $path = $this->computePath($key); |
|
| 62 | $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($path), true); |
|
| 63 | ||
| 64 | return file_put_contents($path, $content); |
|
| 65 | } |
|
| 66 | ||
| 67 | /** |
|
| 68 | * {@inheritdoc} |
|
| 69 | */ |
|
| 70 | public function rename($sourceKey, $targetKey) |
|
| 71 | { |
|
| 72 | $targetPath = $this->computePath($targetKey); |
|
| 73 | $this->ensureDirectoryExists(\Gaufrette\Util\Path::dirname($targetPath), true); |
|
| 74 | ||
| 75 | return rename($this->computePath($sourceKey), $targetPath); |
|
| 76 | } |
|
| 77 | ||
| 78 | /** |
|
| 79 | * {@inheritdoc} |
|
| 80 | */ |
|
| 81 | public function exists($key) |
|
| 82 | { |
|
| 83 | return file_exists($this->computePath($key)); |
|
| 84 | } |
|
| 85 | ||
| 86 | /** |
|
| 87 | * {@inheritdoc} |
|
| 88 | */ |
|
| 89 | public function keys() |
|
| 90 | { |
|
| 91 | $this->ensureDirectoryExists($this->directory, $this->create); |
|
| 92 | ||
| 93 | try { |
|
| 94 | $files = new \RecursiveIteratorIterator( |
|
| 95 | new \RecursiveDirectoryIterator( |
|
| 96 | $this->directory, |
|
| 97 | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS |
|
| 98 | ), |
|
| 99 | \RecursiveIteratorIterator::CHILD_FIRST |
|
| 100 | ); |
|
| 101 | } catch (\Exception $e) { |
|
| 102 | $files = new \EmptyIterator(); |
|
| 103 | } |
|
| 104 | ||
| 105 | $keys = array(); |
|
| 106 | foreach ($files as $file) { |
|
| 107 | $keys[] = $this->computeKey($file); |
|
| 108 | } |
|
| 109 | sort($keys); |
|
| 110 | ||
| 111 | return $keys; |
|
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * {@inheritdoc} |
|
| 116 | */ |
|
| 117 | public function mtime($key) |
|
| 118 | { |
|
| 119 | return filemtime($this->computePath($key)); |
|
| 120 | } |
|
| 121 | ||
| 122 | /** |
|
| 123 | * {@inheritdoc} |
|
| 124 | */ |
|
| 125 | public function delete($key) |
|
| 126 | { |
|
| 127 | if ($this->isDirectory($key)) { |
|
| 128 | return rmdir($this->computePath($key)); |
|
| 129 | } |
|
| 130 | ||
| 131 | return unlink($this->computePath($key)); |
|
| 132 | } |
|
| 133 | ||
| 134 | /** |
|
| 135 | * @param string $key |
|
| 136 | * |
|
| 137 | * @return bool |
|
| 138 | */ |
|
| 139 | public function isDirectory($key) |
|
| 140 | { |
|
| 141 | return is_dir($this->computePath($key)); |
|
| 142 | } |
|
| 143 | ||
| 144 | /** |
|
| 145 | * {@inheritdoc} |
|
| 146 | */ |
|
| 147 | public function createStream($key) |
|
| 148 | { |
|
| 149 | return new LocalStream($this->computePath($key)); |
|
| 150 | } |
|
| 151 | ||
| 152 | /** |
|
| 153 | * {@inheritdoc} |
|
| 154 | */ |
|
| 155 | public function checksum($key) |
|
| 156 | { |
|
| 157 | return Util\Checksum::fromFile($this->computePath($key)); |
|
| 158 | } |
|
| 159 | ||
| 160 | /** |
|
| 161 | * {@inheritdoc} |
|
| 162 | */ |
|
| 163 | public function size($key) |
|
| 164 | { |
|
| 165 | return Util\Size::fromFile($this->computePath($key)); |
|
| 166 | } |
|
| 167 | ||
| 168 | /** |
|
| 169 | * {@inheritdoc} |
|
| 170 | */ |
|
| 171 | public function mimeType($key) |
|
| 172 | { |
|
| 173 | $fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
|
| 174 | ||
| 175 | return $fileInfo->file($this->computePath($key)); |
|
| 176 | } |
|
| 177 | ||
| 178 | /** |
|
| 179 | * Computes the key from the specified path. |
|
| 180 | * |
|
| 181 | * @param string $path |
|
| 182 | * |
|
| 183 | * @return string |
|
| 184 | */ |
|
| 185 | public function computeKey($path) |
|
| 186 | { |
|
| 187 | $path = $this->normalizePath($path); |
|
| 188 | ||
| 189 | return ltrim(substr($path, strlen($this->directory)), '/'); |
|
| 190 | } |
|
| 191 | ||
| 192 | /** |
|
| 193 | * Computes the path from the specified key. |
|
| 194 | * |
|
| 195 | * @param string $key The key which for to compute the path |
|
| 196 | * |
|
| 197 | * @return string A path |
|
| 198 | * |
|
| 199 | * @throws \OutOfBoundsException If the computed path is out of the |
|
| 200 | * directory |
|
| 201 | * @throws \RuntimeException If directory does not exists and cannot be created |
|
| 202 | */ |
|
| 203 | protected function computePath($key) |
|
| 204 | { |
|
| 205 | $this->ensureDirectoryExists($this->directory, $this->create); |
|
| 206 | ||
| 207 | return $this->normalizePath($this->directory.'/'.$key); |
|
| 208 | } |
|
| 209 | ||
| 210 | /** |
|
| 211 | * Normalizes the given path. |
|
| 212 | * |
|
| 213 | * @param string $path |
|
| 214 | * |
|
| 215 | * @return string |
|
| 216 | */ |
|
| 217 | protected function normalizePath($path) |
|
| 218 | { |
|
| 219 | $path = Util\Path::normalize($path); |
|
| 220 | ||
| 221 | if (0 !== strpos($path, $this->directory)) { |
|
| 222 | throw new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', $path)); |
|
| 223 | } |
|
| 224 | ||
| 225 | return $path; |
|
| 226 | } |
|
| 227 | ||
| 228 | /** |
|
| 229 | * Ensures the specified directory exists, creates it if it does not. |
|
| 230 | * |
|
| 231 | * @param string $directory Path of the directory to test |
|
| 232 | * @param bool $create Whether to create the directory if it does |
|
| 233 | * not exist |
|
| 234 | * |
|
| 235 | * @throws \RuntimeException if the directory does not exists and could not |
|
| 236 | * be created |
|
| 237 | */ |
|
| 238 | protected function ensureDirectoryExists($directory, $create = false) |
|
| 239 | { |
|
| 240 | if (!is_dir($directory)) { |
|
| 241 | if (!$create) { |
|
| 242 | throw new \RuntimeException(sprintf('The directory "%s" does not exist.', $directory)); |
|
| 243 | } |
|
| 244 | ||
| 245 | $this->createDirectory($directory); |
|
| 246 | } |
|
| 247 | } |
|
| 248 | ||
| 249 | /** |
|
| 250 | * Creates the specified directory and its parents. |
|
| 251 | * |
|
| 252 | * @param string $directory Path of the directory to create |
|
| 253 | * |
|
| 254 | * @throws \InvalidArgumentException if the directory already exists |
|
| 255 | * @throws \RuntimeException if the directory could not be created |
|
| 256 | */ |
|
| 257 | protected function createDirectory($directory) |
|
| 258 | { |
|
| 259 | $created = mkdir($directory, $this->mode, true); |
|
| 260 | ||
| 261 | if (!$created) { |
|
| 262 | if (!is_dir($directory)) { |
|
| 263 | throw new \RuntimeException(sprintf('The directory \'%s\' could not be created.', $directory)); |
|
| 264 | } |
|
| 265 | } |
|
| 266 | } |
|
| 267 | } |
|
| 268 | ||