Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
YumYumYamlLoader | |
0.00% |
0 / 20 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
populateCache | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
loadSchema | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
registerPath | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * @file |
4 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
5 | * @license MIT |
6 | */ |
7 | |
8 | namespace MediaWiki\Extension\WikiLambda\Validation; |
9 | |
10 | use Opis\JsonSchema\ISchemaLoader; |
11 | use Opis\JsonSchema\Schema; |
12 | |
13 | /** |
14 | * Loads Yaml files from registered directory/ies. |
15 | */ |
16 | class YumYumYamlLoader implements ISchemaLoader { |
17 | /** |
18 | * @var string[] |
19 | */ |
20 | protected $prefixToDirectory = []; |
21 | |
22 | /** |
23 | * @var (Schema|string)[] |
24 | */ |
25 | protected $schemaCache = []; |
26 | |
27 | /** |
28 | * @var string not null and not a Schema |
29 | */ |
30 | private static $sentinel = "__SENTINEL__"; |
31 | |
32 | /** |
33 | * Populates cache with Schema for the provided ZID; places a sentinel value |
34 | * in the cache if the schema spec for the ZID is not found. |
35 | * |
36 | * @param string $ZID ZID to create schema for |
37 | */ |
38 | private function populateCache( string $ZID ): void { |
39 | foreach ( $this->prefixToDirectory as $prefix => $directory ) { |
40 | $path = SchemataUtils::joinPath( $directory, $ZID . '.yaml' ); |
41 | if ( file_exists( $path ) ) { |
42 | $jsonEncoded = SchemataUtils::readYamlAsSecretJson( $path ); |
43 | $schema = Schema::fromJsonString( $jsonEncoded ); |
44 | $this->schemaCache[$ZID] = $schema; |
45 | return; |
46 | } |
47 | } |
48 | $this->schemaCache[$ZID] = self::$sentinel; |
49 | } |
50 | |
51 | /** |
52 | * @inheritDoc |
53 | */ |
54 | public function loadSchema( string $ZID ) { |
55 | if ( !isset( $this->schemaCache[$ZID] ) ) { |
56 | $this->populateCache( $ZID ); |
57 | } |
58 | |
59 | $result = $this->schemaCache[$ZID]; |
60 | if ( $result == self::$sentinel ) { |
61 | return null; |
62 | } |
63 | return $result; |
64 | } |
65 | |
66 | /** |
67 | * @param string $directory |
68 | * @param string $uri_prefix |
69 | * @return bool |
70 | */ |
71 | public function registerPath( string $directory, string $uri_prefix ): bool { |
72 | if ( !is_dir( $directory ) ) { |
73 | return false; |
74 | } |
75 | |
76 | $uri_prefix = rtrim( $uri_prefix, DIRECTORY_SEPARATOR ); |
77 | $directory = rtrim( $directory, DIRECTORY_SEPARATOR ); |
78 | |
79 | $this->prefixToDirectory[$uri_prefix] = $directory; |
80 | |
81 | return true; |
82 | } |
83 | } |