| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ExternalStoreMemory extends ExternalStoreMedium { |
| 33 |
|
| 34 |
private static $data = []; |
| 35 |
|
| 36 |
private static $nextId = 0; |
| 37 |
|
| 38 |
public function fetchFromURL( $url ) { |
| 39 |
[ $location, $id ] = self::getURLComponents( $url ); |
| 40 |
if ( $id === null ) { |
| 41 |
throw new UnexpectedValueException( "Missing ID in URL component." ); |
| 42 |
} |
| 43 |
|
| 44 |
return self::$data[$location][$this->dbDomain][$id] ?? false; |
| 45 |
} |
| 46 |
|
| 47 |
public function batchFetchFromURLs( array $urls ) { |
| 48 |
$blobs = []; |
| 49 |
foreach ( $urls as $url ) { |
| 50 |
$blob = $this->fetchFromURL( $url ); |
| 51 |
if ( $blob !== false ) { |
| 52 |
$blobs[$url] = $blob; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $blobs; |
| 57 |
} |
| 58 |
|
| 59 |
public function store( $location, $data ) { |
| 60 |
$index = ++self::$nextId; |
| 61 |
self::$data[$location][$this->dbDomain][$index] = $data; |
| 62 |
|
| 63 |
return "memory://$location/$index"; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function clear() { |
| 70 |
foreach ( self::$data as &$dataForLocation ) { |
| 71 |
unset( $dataForLocation[$this->dbDomain] ); |
| 72 |
} |
| 73 |
unset( $dataForLocation ); |
| 74 |
self::$data = array_filter( self::$data, 'count' ); |
| 75 |
self::$nextId = 0; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private function getURLComponents( $url ) { |
| 83 |
|
| 84 |
[ $proto, $path ] = explode( '://', $url, 2 ) + [ null, null ]; |
| 85 |
if ( $proto !== 'memory' ) { |
| 86 |
throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." ); |
| 87 |
} elseif ( $path === null ) { |
| 88 |
throw new UnexpectedValueException( "URL is missing path component." ); |
| 89 |
} |
| 90 |
|
| 91 |
$parts = explode( '/', $path ); |
| 92 |
if ( count( $parts ) > 2 ) { |
| 93 |
throw new UnexpectedValueException( "Too components in URL '$path'." ); |
| 94 |
} |
| 95 |
|
| 96 |
return [ $parts[0], $parts[1] ?? null ]; |
| 97 |
} |
| 98 |
} |