Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / externalstore / ExternalStoreMemory.php
1 <?php
2 /**
3 * External storage in PHP process memory for testing.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Process memory based external objects for testing.
25 *
26 * In this system, each store "location" is separate PHP array.
27 * URLs are of the form "memory://location/id". The id/value pairs
28 * at each location are segregated by DB domain ID.
29 *
30 * @ingroup ExternalStorage
31 * @since 1.33
32 */
33 class ExternalStoreMemory extends ExternalStoreMedium {
34 /** @var array[] Map of (location => DB domain => id => value) */
35 private static $data = [];
36 /** @var int */
37 private static $nextId = 0;
38
39 public function __construct( array $params ) {
40 parent::__construct( $params );
41 }
42
43 public function fetchFromURL( $url ) {
44 list( $location, $id ) = self::getURLComponents( $url );
45 if ( $id === null ) {
46 throw new UnexpectedValueException( "Missing ID in URL component." );
47 }
48
49 return self::$data[$location][$this->dbDomain][$id] ?? false;
50 }
51
52 public function batchFetchFromURLs( array $urls ) {
53 $blobs = [];
54 foreach ( $urls as $url ) {
55 $blob = $this->fetchFromURL( $url );
56 if ( $blob !== false ) {
57 $blobs[$url] = $blob;
58 }
59 }
60
61 return $blobs;
62 }
63
64 public function store( $location, $data ) {
65 $index = ++self::$nextId;
66 self::$data[$location][$this->dbDomain][$index] = $data;
67
68 return "memory://$location/$index";
69 }
70
71 /**
72 * Remove all data from memory for this domain
73 */
74 public function clear() {
75 foreach ( self::$data as &$dataForLocation ) {
76 unset( $dataForLocation[$this->dbDomain] );
77 }
78 unset( $dataForLocation );
79 self::$data = array_filter( self::$data, 'count' );
80 self::$nextId = 0;
81 }
82
83 /**
84 * @param string $url
85 * @return array (location, ID or null)
86 */
87 private function getURLComponents( $url ) {
88 list( $proto, $path ) = explode( '://', $url, 2 ) + [ null, null ];
89 if ( $proto !== 'memory' ) {
90 throw new UnexpectedValueException( "Got URL of protocol '$proto', not 'memory'." );
91 } elseif ( $path === null ) {
92 throw new UnexpectedValueException( "URL is missing path component." );
93 }
94
95 $parts = explode( '/', $path );
96 if ( count( $parts ) > 2 ) {
97 throw new UnexpectedValueException( "Too components in URL '$path'." );
98 }
99
100 return [ $parts[0], $parts[1] ?? null ];
101 }
102 }