| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace Wikimedia\FileBackend\FileIteration; |
| 23 |
|
| 24 |
use DirectoryIterator; |
| 25 |
use FilesystemIterator; |
| 26 |
use Iterator; |
| 27 |
use RecursiveDirectoryIterator; |
| 28 |
use RecursiveIteratorIterator; |
| 29 |
use UnexpectedValueException; |
| 30 |
use Wikimedia\FileBackend\FileBackendError; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
abstract class FSFileBackendList implements Iterator { |
| 40 |
|
| 41 |
protected $iter; |
| 42 |
|
| 43 |
protected $lastError; |
| 44 |
|
| 45 |
|
| 46 |
protected $suffixStart; |
| 47 |
|
| 48 |
|
| 49 |
protected $pos = 0; |
| 50 |
|
| 51 |
|
| 52 |
protected $params = []; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct( $dir, array $params ) { |
| 59 |
$path = realpath( $dir ); |
| 60 |
if ( $path === false ) { |
| 61 |
$path = $dir; |
| 62 |
} |
| 63 |
$this->suffixStart = strlen( $path ) + 1; |
| 64 |
$this->params = $params; |
| 65 |
|
| 66 |
try { |
| 67 |
$this->iter = $this->initIterator( $path ); |
| 68 |
} catch ( UnexpectedValueException $e ) { |
| 69 |
$this->iter = null; |
| 70 |
$this->lastError = $e->getMessage(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
protected function initIterator( $dir ) { |
| 82 |
if ( !empty( $this->params['topOnly'] ) ) { |
| 83 |
|
| 84 |
return new DirectoryIterator( $dir ); |
| 85 |
} else { |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
$flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS; |
| 90 |
|
| 91 |
return new RecursiveIteratorIterator( |
| 92 |
new RecursiveDirectoryIterator( $dir, $flags ), |
| 93 |
RecursiveIteratorIterator::CHILD_FIRST |
| 94 |
); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function key(): int { |
| 103 |
return $this->pos; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
#[\ReturnTypeWillChange] |
| 111 |
public function current() { |
| 112 |
return $this->getRelPath( $this->iter->current()->getPathname() ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function next(): void { |
| 120 |
try { |
| 121 |
$this->iter->next(); |
| 122 |
$this->filterViaNext(); |
| 123 |
} catch ( UnexpectedValueException $e ) { |
| 124 |
$this->lastError = $e->getMessage(); |
| 125 |
|
| 126 |
throw new FileBackendError( "File iterator gave UnexpectedValueException." ); |
| 127 |
} |
| 128 |
++$this->pos; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function rewind(): void { |
| 136 |
$this->pos = 0; |
| 137 |
try { |
| 138 |
$this->iter->rewind(); |
| 139 |
$this->filterViaNext(); |
| 140 |
} catch ( UnexpectedValueException $e ) { |
| 141 |
$this->lastError = $e->getMessage(); |
| 142 |
|
| 143 |
throw new FileBackendError( "File iterator gave UnexpectedValueException." ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function valid(): bool { |
| 152 |
return $this->iter && $this->iter->valid(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function getLastError() { |
| 159 |
return $this->lastError; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected function filterViaNext() { |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
protected function getRelPath( $dir ) { |
| 176 |
$path = realpath( $dir ); |
| 177 |
if ( $path === false ) { |
| 178 |
$path = $dir; |
| 179 |
} |
| 180 |
|
| 181 |
return strtr( substr( $path, $this->suffixStart ), '\\', '/' ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
class_alias( FSFileBackendList::class, 'FSFileBackendList' ); |