Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Directory | |
0.00% |
0 / 56 |
|
0.00% |
0 / 10 |
756 | |
0.00% |
0 / 1 |
| getDirectory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getFile | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFlyIterator | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
72 | |||
| getRelPath | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| exists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleteSelf | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFullPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| rename | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
56 | |||
| stripDirectoryPath | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\filesystem; |
| 23 | |
| 24 | class Directory extends FileSystemHandler implements \IteratorAggregate |
| 25 | { |
| 26 | public const ITERATOR_RECURSIVE = '1'; |
| 27 | public const ITERATOR_FILE = '2'; |
| 28 | public const ITERATOR_DIRECTORY = '4'; |
| 29 | |
| 30 | /** |
| 31 | * Get a subDirectory of $this (existing or not) |
| 32 | * |
| 33 | * @param $path |
| 34 | * @return Directory |
| 35 | */ |
| 36 | public function getDirectory($path) |
| 37 | { |
| 38 | $subDirectory = new self($this->getFileSystemId(), $this->getFullPath($path)); |
| 39 | $subDirectory->setServiceLocator($this->getServiceLocator()); |
| 40 | return $subDirectory; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get file located into $this->directory (existing or not) |
| 45 | * |
| 46 | * @param $path |
| 47 | * @return File |
| 48 | */ |
| 49 | public function getFile($path) |
| 50 | { |
| 51 | $file = new File($this->getFileSystemId(), $this->getFullPath($path)); |
| 52 | $file->setServiceLocator($this->getServiceLocator()); |
| 53 | return $file; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Method constraints by IteratorAggregator, wrapper to getFlyIterator |
| 58 | * |
| 59 | * @return \ArrayIterator |
| 60 | */ |
| 61 | public function getIterator() |
| 62 | { |
| 63 | return $this->getFlyIterator(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get an iterator of $this directory |
| 68 | * Flags are combinable like that $this->getFlyIterator(self::ITERATOR_DIRECTORY|self::ITERATOR_DIRECTORY) |
| 69 | * By default iterator is not recursive and includes directories & files |
| 70 | * |
| 71 | * @param null $flags |
| 72 | * @return \ArrayIterator |
| 73 | */ |
| 74 | public function getFlyIterator($flags = null) |
| 75 | { |
| 76 | if (is_null($flags)) { |
| 77 | $flags = self::ITERATOR_DIRECTORY | self::ITERATOR_FILE; |
| 78 | } |
| 79 | |
| 80 | $recursive = ($flags & self::ITERATOR_RECURSIVE); |
| 81 | $withDirectories = ($flags & self::ITERATOR_DIRECTORY); |
| 82 | $withFiles = ($flags & self::ITERATOR_FILE); |
| 83 | |
| 84 | $iterator = []; |
| 85 | $contents = $this->getFileSystem()->listContents($this->getPrefix(), $recursive); |
| 86 | |
| 87 | if (!empty($contents)) { |
| 88 | foreach ($contents as $content) { |
| 89 | if ($withDirectories && $content['type'] == 'dir') { |
| 90 | $iterator[] = $this->getDirectory($this->stripDirectoryPath($content['path'])); |
| 91 | } |
| 92 | |
| 93 | if ($withFiles && $content['type'] == 'file') { |
| 94 | $iterator[] = $this->getFile($this->stripDirectoryPath($content['path'])); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return new \ArrayIterator($iterator); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get relative path from $this directory to given content |
| 104 | * |
| 105 | * @param Directory|File $content |
| 106 | * @return mixed |
| 107 | * @throws \common_Exception |
| 108 | * @throws \tao_models_classes_FileNotFoundException |
| 109 | */ |
| 110 | public function getRelPath($content) |
| 111 | { |
| 112 | if (! $content instanceof File && ! $content instanceof Directory) { |
| 113 | throw new \common_Exception('Content for ' . __FUNCTION__ . ' has to be a file or directory object. ' . |
| 114 | is_object($content) ? get_class($content) : gettype($content) . ' given.'); |
| 115 | } |
| 116 | |
| 117 | return str_replace($this->getPrefix(), '', $content->getPrefix()); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Check if current directory exists |
| 122 | * |
| 123 | * @return bool |
| 124 | */ |
| 125 | public function exists() |
| 126 | { |
| 127 | return $this->getFileSystem()->directoryExists($this->getPrefix()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Delete the current directory |
| 132 | * |
| 133 | * @return bool |
| 134 | */ |
| 135 | public function deleteSelf() |
| 136 | { |
| 137 | try { |
| 138 | $this->getFileSystem()->deleteDirectory($this->getPrefix()); |
| 139 | return true; |
| 140 | } catch (FilesystemException $e) { |
| 141 | $this->logWarning($e->getMessage()); |
| 142 | } |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Return a sanitized full path from main directory |
| 149 | * |
| 150 | * @param $path |
| 151 | * @return string |
| 152 | */ |
| 153 | protected function getFullPath($path) |
| 154 | { |
| 155 | return $this->getPrefix() . '/' . $this->sanitizePath($path); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Rename |
| 160 | * |
| 161 | * Rename directory into $path |
| 162 | * |
| 163 | * @param $path |
| 164 | * @return bool |
| 165 | * @throws \common_exception_FileSystemError |
| 166 | */ |
| 167 | public function rename($path) |
| 168 | { |
| 169 | // This implementation supersedes the Flysystem's one. Indeed, while using connectors |
| 170 | // such as the Amazon S3 (v3) connector, rename on directories does not work. A custom |
| 171 | // implementation is then needed. |
| 172 | |
| 173 | $contents = $this->getFileSystem()->listContents($this->getPrefix(), true); |
| 174 | $fileSystemId = $this->getFileSystemId() . '/'; |
| 175 | // Filter files only. |
| 176 | $filePaths = []; |
| 177 | foreach ($contents as $content) { |
| 178 | if ($content['type'] === 'file') { |
| 179 | $contentPath = $content['path']; |
| 180 | if (strpos($contentPath, $fileSystemId) === 0) { |
| 181 | $contentPath = substr($contentPath, strlen($fileSystemId)); |
| 182 | } |
| 183 | |
| 184 | $filePaths[] = [ |
| 185 | 'source' => $contentPath, |
| 186 | 'destination' => str_replace($this->getPrefix(), $path, $content['path'])]; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | foreach ($filePaths as $renaming) { |
| 191 | try { |
| 192 | $this->getFileSystem()->move($renaming['source'], $renaming['destination']); |
| 193 | } catch (FilesystemException $e) { |
| 194 | throw new \common_exception_FileSystemError( |
| 195 | "Unable to rename '" . $renaming['source'] . "' into '" . $renaming['destination'] . "'." |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (!$this->deleteSelf()) { |
| 201 | throw new \common_exception_FileSystemError( |
| 202 | "Could not finalize renaming of '" . $this->getPrefix() . "' into '${path}'." |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | private function stripDirectoryPath(string $path): string |
| 210 | { |
| 211 | $strippedPath = str_replace($this->getPrefix(), '', $path); |
| 212 | return str_replace($this->getFileSystemId(), '', $strippedPath); |
| 213 | } |
| 214 | } |